data_structures
rbt_rank.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* rbt_rank.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/19 00:35:28 by unite #+# #+# */
10 /* Updated: 2020/07/19 00:45:00 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "rbt.h"
15 #include "rbt_utils.h"
16 
17 static size_t rbt_rank_recur(const t_rbt *rbt, const t_rbt_node *node,
18  const void *key)
19 {
20  int cmp;
21 
22  if (node == NULL)
23  return (0);
24  cmp = rbt->key_type->cmp(key, node->key);
25  if (cmp < 0)
26  return (rbt_rank_recur(rbt, node->left, key));
27  if (cmp > 0)
28  return (1 +
29  rbt_size_subtree(node->left) +
30  rbt_rank_recur(rbt, node->right, key));
31  return (rbt_size_subtree(node->left));
32 }
33 
34 size_t rbt_rank(const t_rbt *rbt, const void *key)
35 {
36  return (rbt_rank_recur(rbt, rbt->root, key));
37 }
rbt_rank
size_t rbt_rank(const t_rbt *rbt, const void *key)
Returns the number of keys in the tree strictly less than the specified key.
Definition: rbt_rank.c:34
s_rbt
A left-leaning red-black binary search tree.
Definition: rbt.h:72
s_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
s_rbt_node::right
struct s_rbt_node * right
The right child.
Definition: rbt.h:56
s_rbt_node::left
struct s_rbt_node * left
The left child.
Definition: rbt.h:55
s_rbt_node::key
void * key
The key.
Definition: rbt.h:53
s_rbt::key_type
const t_type * key_type
The type of keys in the tree.
Definition: rbt.h:75
rbt.h
rbt_utils.h
s_rbt_node
A node in a red-black tree.
Definition: rbt.h:51
s_rbt::root
t_rbt_node * root
The root of the tree.
Definition: rbt.h:74