red_black_tree.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**********************************************************
  2. * See the LICENSE file for copyright infomation. *
  3. **********************************************************/
  4. #pragma once
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /* CONVENTIONS: All data structures for red-black trees have the prefix */
  9. /* "rb_" to prevent name conflicts. */
  10. /* */
  11. /* Function names: Each word in a function name begins with */
  12. /* a capital letter. An example funcntion name is */
  13. /* CreateRedTree(a,b,c). Furthermore, each function name */
  14. /* should begin with a capital letter to easily distinguish */
  15. /* them from variables. */
  16. /* */
  17. /* Variable names: Each word in a variable name begins with */
  18. /* a capital letter EXCEPT the first letter of the variable */
  19. /* name. For example, int newLongInt. Global variables have */
  20. /* names beginning with "g". An example of a global */
  21. /* variable name is gNewtonsConstant. */
  22. typedef struct rb_red_blk_node {
  23. void* key;
  24. int red; /* if red=0 then the node is black */
  25. struct rb_red_blk_node* left;
  26. struct rb_red_blk_node* right;
  27. struct rb_red_blk_node* parent;
  28. } rb_red_blk_node;
  29. /* Compare(a,b) should return 1 if *a > *b, -1 if *a < *b, and 0 otherwise */
  30. /* Destroy(a) takes a pointer to whatever key might be and frees it accordingly */
  31. typedef struct rb_red_blk_tree {
  32. int (*Compare)(const void* a, const void* b);
  33. void (*DestroyKey)(void* a);
  34. /* A sentinel is used for root and for nil. These sentinels are */
  35. /* created when RBTreeCreate is caled. root->left should always */
  36. /* point to the node which is the root of the tree. nil points to a */
  37. /* node which should always be black but has aribtrary children and */
  38. /* parent and no key. The point of using these sentinels is so */
  39. /* that the root and nil nodes do not require special cases in the code */
  40. rb_red_blk_node* root;
  41. rb_red_blk_node* nil;
  42. } rb_red_blk_tree;
  43. rb_red_blk_tree* RBTreeCreate(int (*CompFunc)(const void*, const void*),
  44. void (*DestFunc)(void *));
  45. rb_red_blk_node *RBTreeInsert(rb_red_blk_tree *, void *key);
  46. void RBDelete(rb_red_blk_tree* , rb_red_blk_node* );
  47. void RBTreeDestroy(rb_red_blk_tree*);
  48. rb_red_blk_node* TreePredecessor(rb_red_blk_tree*,rb_red_blk_node*);
  49. rb_red_blk_node* TreeSuccessor(rb_red_blk_tree*,rb_red_blk_node*);
  50. rb_red_blk_node* RBExactQuery(rb_red_blk_tree*, void*);
  51. #ifdef __cplusplus
  52. }
  53. #endif