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