usermode/library/common/red_black_tree.h

00001 #ifdef DMALLOC
00002 #include <dmalloc.h>
00003 #endif
00004 #include "stack.h"
00005 
00006 /*  CONVENTIONS:  All data structures for red-black trees have the prefix */
00007 /*                "rb_" to prevent name conflicts. */
00008 /*                                                                      */
00009 /*                Function names: Each word in a function name begins with */
00010 /*                a capital letter.  An example funcntion name is  */
00011 /*                CreateRedTree(a,b,c). Furthermore, each function name */
00012 /*                should begin with a capital letter to easily distinguish */
00013 /*                them from variables. */
00014 /*                                                                     */
00015 /*                Variable names: Each word in a variable name begins with */
00016 /*                a capital letter EXCEPT the first letter of the variable */
00017 /*                name.  For example, int newLongInt.  Global variables have */
00018 /*                names beginning with "g".  An example of a global */
00019 /*                variable name is gNewtonsConstant. */
00020 
00021 /* comment out the line below to remove all the debugging assertion */
00022 /* checks from the compiled code.  */
00023 #define DEBUG_ASSERT 1
00024 
00025 typedef struct rb_red_blk_node {
00026   void* key;
00027   void* info;
00028   int red; /* if red=0 then the node is black */
00029   struct rb_red_blk_node* left;
00030   struct rb_red_blk_node* right;
00031   struct rb_red_blk_node* parent;
00032 } rb_red_blk_node;
00033 
00034 
00035 /* Compare(a,b) should return 1 if *a > *b, -1 if *a < *b, and 0 otherwise */
00036 /* Destroy(a) takes a pointer to whatever key might be and frees it accordingly */
00037 typedef struct rb_red_blk_tree {
00038   int (*Compare)(const void* a, const void* b); 
00039   void (*DestroyKey)(void* a);
00040   void (*DestroyInfo)(void* a);
00041   void (*PrintKey)(const void* a);
00042   void (*PrintInfo)(void* a);
00043   /*  A sentinel is used for root and for nil.  These sentinels are */
00044   /*  created when RBTreeCreate is caled.  root->left should always */
00045   /*  point to the node which is the root of the tree.  nil points to a */
00046   /*  node which should always be black but has aribtrary children and */
00047   /*  parent and no key or info.  The point of using these sentinels is so */
00048   /*  that the root and nil nodes do not require special cases in the code */
00049   rb_red_blk_node* root;             
00050   rb_red_blk_node* nil;              
00051 } rb_red_blk_tree;
00052 
00053 rb_red_blk_tree* RBTreeCreate(int  (*CompFunc)(const void*, const void*),
00054                              void (*DestFunc)(void*), 
00055                              void (*InfoDestFunc)(void*), 
00056                              void (*PrintFunc)(const void*),
00057                              void (*PrintInfo)(void*));
00058 rb_red_blk_node * RBTreeInsert(rb_red_blk_tree*, void* key, void* info);
00059 void RBTreePrint(rb_red_blk_tree*);
00060 void RBDelete(rb_red_blk_tree* , rb_red_blk_node* );
00061 void RBTreeDestroy(rb_red_blk_tree*);
00062 rb_red_blk_node* TreePredecessor(rb_red_blk_tree*,rb_red_blk_node*);
00063 rb_red_blk_node* TreeSuccessor(rb_red_blk_tree*,rb_red_blk_node*);
00064 rb_red_blk_node* RBExactQuery(rb_red_blk_tree*, void*);
00065 stk_stack * RBEnumerate(rb_red_blk_tree* tree,void* low, void* high);
00066 rb_red_blk_node* RBQueryLargestSmaller(rb_red_blk_tree* tree, void* q);
00067 void NullFunction(void*);

Generated on Sat Apr 23 11:43:35 2011 for Mnemosyne by  doxygen 1.4.7