data_structures
rbt.h
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* rbt.h :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/16 20:41:50 by unite #+# #+# */
10 /* Updated: 2020/09/07 23:35:56 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #ifndef RBT_H
15 
16 # define RBT_H
17 
18 # include <errno.h>
19 # include <sys/types.h>
20 # include "types.h"
21 # include "queue.h"
22 # include "utils.h"
23 
29 typedef enum e_rbt_node_color
30 {
31  RBT_RED, RBT_BLACK
32 } t_rbt_node_color;
33 
51 typedef struct s_rbt_node
52 {
53  void *key;
54  void *val;
55  struct s_rbt_node *left;
56  struct s_rbt_node *right;
57  t_rbt_node_color color;
58  size_t count;
59 } t_rbt_node;
60 
72 typedef struct s_rbt
73 {
75  const t_type *key_type;
76  const t_type *val_type;
77 } t_rbt;
78 
89 t_rbt *rbt_new(const t_type *key_type, const t_type *val_type);
90 
97 void rbt_put(t_rbt *rbt, const void *key, const void *val);
98 
106 void *rbt_get(const t_rbt *rbt, const void *key);
107 
113 size_t rbt_size(const t_rbt *rbt);
114 
120 void rbt_delete(t_rbt *rbt);
121 
128 size_t rbt_height(const t_rbt *rbt);
129 
136 void *rbt_min(const t_rbt *rbt);
137 
144 void *rbt_max(const t_rbt *rbt);
145 
153 void *rbt_floor(const t_rbt *rbt, const void *key);
154 
162 void *rbt_ceil(const t_rbt *rbt, const void *key);
163 
170 size_t rbt_rank(const t_rbt *rbt, const void *key);
171 
177 t_queue *rbt_keys(const t_rbt *rbt);
178 
186 t_queue *rbt_vals(const t_rbt *rbt);
187 
194 int rbt_contains(const t_rbt *rbt, const void *key);
195 
202 void *rbt_nth(const t_rbt *rbt, size_t n);
203 
204 #endif
rbt_delete
void rbt_delete(t_rbt *rbt)
Deletes this tree and free all its items and the associated data.
Definition: rbt_delete.c:34
types.h
rbt_min
void * rbt_min(const t_rbt *rbt)
Returns the smallest key in the tree.
Definition: rbt_min.c:16
rbt_get
void * rbt_get(const t_rbt *rbt, const void *key)
Returns the value associated with a specified key.
Definition: rbt_get.c:16
s_rbt
A left-leaning red-black binary search tree.
Definition: rbt.h:72
rbt_max
void * rbt_max(const t_rbt *rbt)
Returns the largest key in the tree.
Definition: rbt_max.c:16
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
rbt_size
size_t rbt_size(const t_rbt *rbt)
Returns the number of elements in this tree.
Definition: rbt_size.c:17
rbt_put
void rbt_put(t_rbt *rbt, const void *key, const void *val)
Adds a new item to the tree or overwrites an existing one.
Definition: rbt_put.c:45
s_rbt_node::right
struct s_rbt_node * right
The right child.
Definition: rbt.h:56
utils.h
rbt_height
size_t rbt_height(const t_rbt *rbt)
Returns the number of tiers in the tree.
Definition: rbt_height.c:28
rbt_floor
void * rbt_floor(const t_rbt *rbt, const void *key)
Returns the largest key less than or equal to the specified key.
Definition: rbt_floor.c:33
s_list
Doubly-linked list of generic items.
Definition: list.h:54
rbt_contains
int rbt_contains(const t_rbt *rbt, const void *key)
Does the tree contain the specified key?
Definition: rbt_contains.c:16
s_rbt_node::count
size_t count
The number of this node's children.
Definition: rbt.h:58
queue.h
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
rbt_keys
t_queue * rbt_keys(const t_rbt *rbt)
Returns a queue with all the keys in the tree, sorted in the ascending order.
Definition: rbt_keys.c:26
s_rbt_node::left
struct s_rbt_node * left
The left child.
Definition: rbt.h:55
s_rbt::val_type
const t_type * val_type
The type of values in the ree.
Definition: rbt.h:76
s_rbt_node::key
void * key
The key.
Definition: rbt.h:53
rbt_new
t_rbt * rbt_new(const t_type *key_type, const t_type *val_type)
Initializes a new empty tree.
Definition: rbt_new.c:16
e_rbt_node_color
e_rbt_node_color
The color of a node in a red-black tree.
Definition: rbt.h:29
s_rbt::key_type
const t_type * key_type
The type of keys in the tree.
Definition: rbt.h:75
s_rbt_node::color
t_rbt_node_color color
The color.
Definition: rbt.h:57
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
rbt_ceil
void * rbt_ceil(const t_rbt *rbt, const void *key)
Returns the smallest key greater than or equal to the specified key.
Definition: rbt_ceil.c:33
rbt_vals
t_queue * rbt_vals(const t_rbt *rbt)
Returns a queue with all the value in the tree, sorted by the associated keys in the ascending order.
Definition: rbt_vals.c:26
s_rbt_node::val
void * val
The value.
Definition: rbt.h:54
rbt_nth
void * rbt_nth(const t_rbt *rbt, size_t n)
Returns the key of the nth element in the tree.
Definition: rbt_nth.c:30