Generic Data Type - Red Black Tree implementation in C
Chapter 14 (Red Black Trees), Introduction to Algorithms, Second edition by Cormen, Leiserson, Rivest, Stein
- Your compare function must return an interger
int mycmpr(void* a, void* b)
if mycmpr(...)<0 then a is less than b
if mycmpr(...)=0 then a is equal to b
if mycmpr(...)>0 then a is greater than b
- Initialize your red black tree with:
rbt_tree* tree = rbt_tree rbt_init(mycmpr)
- Enter data (ex intergers)
int y = 1;
rbt_node* new = rbt_newNode(&y, sizeof(int));
rbt_insert(tree, new);
you can also do:
rbt_insert(tree, rbt_newNode(&y, sizeof(int));
- Delete data (if needed)
rbt_delete(t, new);
- Free allocated memory
rbt_free(tree);