avl.h
Go to the documentation of this file.
1 #pragma once
2 
12 
13 #include <stddef.h>
14 #include <stdbool.h>
15 
16 #include <crater/container.h>
17 #include <crater/sla.h>
18 
29 typedef struct cr8r_avl_node cr8r_avl_node;
39  signed char balance;
41  char data[];
42 };
43 
48 typedef struct{
53  int (*cmp)(const cr8r_base_ft*, const void*, const void*);
59  int (*add)(cr8r_base_ft*, void*, void*);
62  void *(*alloc)(cr8r_base_ft*);
64  void (*free)(cr8r_base_ft*, void*);
65 } cr8r_avl_ft;
66 
68 typedef enum{
69  CR8R_AVL_FAILED = 0,
70  CR8R_AVL_INSERTED = 1,
71  CR8R_AVL_UPDATED = 2
73 
89  void *data, uint64_t size,
90  int (*cmp)(const cr8r_base_ft*, const void*, const void*),
91  int (*add)(cr8r_base_ft*, void*, void*),
92  void *(*alloc)(cr8r_base_ft*),
93  void (*free)(cr8r_base_ft*, void*)
94 );
95 
115  cr8r_sla *sla, uint64_t size, uint64_t reserve,
116  int (*cmp)(const cr8r_base_ft*, const void*, const void*),
117  int (*add)(cr8r_base_ft*, void*, void*)
118 );
119 
127 cr8r_avl_node *cr8r_avl_new(void *key, cr8r_avl_node *left, cr8r_avl_node *right, cr8r_avl_node *parent, char balance, cr8r_avl_ft*);
128 
135 
146 
153 
160 
169 
177 
184 
191 
204 
211 
218 
224 
231 
237 
249 
257 
262 
272 
282 
290 
296 
303 
311 
318 
325 
327 #define CR8R_AVL_DATA(T, n) CR8R_FLA_CAST(T, (char*)((cr8r_avl_node*)(n))->data)
328 
cr8r_avl_node * cr8r_avl_new(void *key, cr8r_avl_node *left, cr8r_avl_node *right, cr8r_avl_node *parent, char balance, cr8r_avl_ft *)
Allocate a new avl node and initialize it with given data.
bool cr8r_avl_ft_init(cr8r_avl_ft *, void *data, uint64_t size, int(*cmp)(const cr8r_base_ft *, const void *, const void *), int(*add)(cr8r_base_ft *, void *, void *), void *(*alloc)(cr8r_base_ft *), void(*free)(cr8r_base_ft *, void *))
Convenience function to initialize a cr8r_avl_ft.
int cr8r_avl_insert_update(cr8r_avl_node **r, void *key, cr8r_avl_ft *)
Create a new node in an avl tree or modify an existing one with a given value.
cr8r_avl_node * cr8r_avl_attach_exclusive(cr8r_avl_node *r, cr8r_avl_node *n, cr8r_avl_ft *)
Add a given node to an existing tree.
cr8r_avl_node * cr8r_avl_heappop_node(cr8r_avl_node **r, cr8r_avl_ft *)
Treat the avl tree as a max heap and remove the top element.
void cr8r_avl_reorder(cr8r_avl_node *r, cr8r_avl_ft *)
Reorder the tree so that it is sorted according to a new ordering function.
void cr8r_avl_heapify(cr8r_avl_node *r, cr8r_avl_ft *)
Convert the avl tree into a max heap in place.
cr8r_avl_node * cr8r_avl_last(cr8r_avl_node *n)
Find the node in the tree with the greatest key.
int cr8r_avl_remove(cr8r_avl_node **r, void *key, cr8r_avl_ft *)
Remove the node in an avl tree with a given value.
cr8r_avl_node * cr8r_avl_get(cr8r_avl_node *r, void *key, cr8r_avl_ft *)
Find the node matching a given element in a tree.
cr8r_avl_node * cr8r_avl_increase(cr8r_avl_node *n, cr8r_avl_ft *, int *is_duplicate)
Restore the binary search tree invariant after increasing the key for a single node.
cr8r_avl_node * cr8r_avl_root(cr8r_avl_node *n)
Find the root of the tree containing a given node.
cr8r_avl_node * cr8r_avl_remove_node(cr8r_avl_node *n, cr8r_avl_ft *)
Remove a given node from the tree containing it.
cr8r_avl_node * cr8r_avl_next(cr8r_avl_node *n)
Find the inorder successor of a node.
int cr8r_avl_insert(cr8r_avl_node **r, void *key, cr8r_avl_ft *)
Create a new node in an avl tree with a given value.
cr8r_avl_node * cr8r_avl_search(cr8r_avl_node *r, void *key, cr8r_avl_ft *)
Find the deepest node on the search path for a given key If there is a node in the tree with the give...
cr8r_avl_node * cr8r_avl_first_post(cr8r_avl_node *r)
Find the first node in a postorder traversal of the tree.
cr8r_avl_node * cr8r_avl_detach(cr8r_avl_node *n, cr8r_avl_ft *)
Remove a given node from the tree containing it but do not free it.
void cr8r_avl_delete(cr8r_avl_node *r, cr8r_avl_ft *)
Delete an entire avl tree, freeing all nodes in the process.
cr8r_avl_node * cr8r_avl_decrease(cr8r_avl_node *n, cr8r_avl_ft *, int *is_duplicate)
Restore the binary search tree invariant after decreasing the key for a single node.
cr8r_avl_node * cr8r_avl_attach(cr8r_avl_node *r, cr8r_avl_node *n, cr8r_avl_ft *, int *is_duplicate)
Add a given node to an existing tree.
void cr8r_avl_sift_down(cr8r_avl_node *r, cr8r_avl_ft *)
Treat the avl tree as a max heap and sift down a given node.
cr8r_avl_node * cr8r_avl_next_post(cr8r_avl_node *n)
Find the postorder successor of a given node.
cr8r_avl_node * cr8r_avl_lower_bound(cr8r_avl_node *r, void *key, cr8r_avl_ft *)
Find the greatest element l in the tree so that l <= key.
cr8r_avl_node * cr8r_avl_prev(cr8r_avl_node *n)
Find the inorder predecessor of a node.
cr8r_map_insert_result
Constants to test map like data structure insertion against where applicable.
Definition: avl.h:68
cr8r_avl_node * cr8r_avl_first(cr8r_avl_node *r)
Find the node in the tree with the lowest key.
cr8r_avl_node * cr8r_avl_upper_bound(cr8r_avl_node *r, void *key, cr8r_avl_ft *)
Find the lowest element u in the tree so that key < u.
bool cr8r_avl_ft_initsla(cr8r_avl_ft *, cr8r_sla *sla, uint64_t size, uint64_t reserve, int(*cmp)(const cr8r_base_ft *, const void *, const void *), int(*add)(cr8r_base_ft *, void *, void *))
Convenience function to initialize a cr8r_avl_ft and associated slab allocator.
Function table for avl tree.
Definition: avl.h:48
cr8r_base_ft base
Base function table values (data and size)
Definition: avl.h:50
An avl tree node, also used to store an entire tree by synecdoche.
Definition: avl.h:30
char data[]
element data
Definition: avl.h:41
cr8r_avl_node * parent
Pointers to other nodes (can be NULL)
Definition: avl.h:36
cr8r_avl_node * right
Pointers to other nodes (can be NULL)
Definition: avl.h:34
cr8r_avl_node * left
Pointers to other nodes (can be NULL)
Definition: avl.h:32
signed char balance
avl balance value; the height of the right subtree minus the height of the left subtree.
Definition: avl.h:39
Base function table for all Crater containers.
Definition: container.h:22
Slab allocator.
Definition: sla.h:22