#ifndef _CPP_JRB_H_ #define _CPP_JRB_H_ /* Most everything here is the same as the analogous calls in jrb.h. The big difference is that there is a difference between the tree and the nodes. */ #include "Jrb.h" #include using namespace std; template class rbTree { public: rbTree(); // This allows you to specify a comparison function for inserting keys // into the tree rbTree(int (*compare_val)(Key &,Key &)); // This allows you to specify a deletion function when you allocate the // tree rbTree(void (*del_key_val)(Key &,Jval)); // This allows you to specify both a comparison and a deletion function rbTree(int (*compare_val)(Key &,Key &), void (*del_key_val)(Key &,Jval)); ~rbTree(); void insert(Key &, Jval); /* insert the key into the rbtree */ void deleteNode(); /* delete the node pointed to by the cursor */ bool find(Key &); /* position the cursor to the node associated with the key. If no such node exists then the cursor will point to the first node whose key is lexicographically greater than the key we were trying to find. */ Key getKey(); /* return the key associated with the node pointed to by the cursor */ Jval getVal(); /* return the value associated with the node pointed to by the cursor */ void setVal(Jval newval); /* set the value field of the node pointed to by the cursor to the indicated value */ void first(); /* set the cursor to the first node in lexicographic order in the tree */ void last(); /* set the cursor to the last node in lexicographic order in the tree */ void next(); /* move the cursor to the next node in lexicograhic order in the tree */ void prev(); /* move the cursor to the previous node in lexicographic order in the tree */ /* indicates if we have exhausted all the nodes when traversing the tree in either lexicographic or reverse lexicographic order */ bool endOfList() { return cursor == head; } bool empty(); /* indicates if the tree is empty */ protected: rbNode *make_jrb(); /* Creates a new rb-tree */ /* Creates a node with key key and val val and inserts it into the tree. */ rbNode *jrb_insert_b(rbNode *n, Key &key, Jval val); rbNode *jrb_insert(rbNode *tree, Key &key, Jval val); rbNode *jrb_insert_gen(rbNode *tree, Key &key, Jval val, int (*func)(Key &, Key &)); /* returns an external node in t whose value is equal k. Returns NULL if there is no such node in the tree */ rbNode *jrb_find(rbNode *root, Key &key); rbNode *jrb_find_gen(rbNode *root, Key &, int (*func)(Key &, Key &)); /* returns an external node in t whose value is equal k or whose value is the smallest value greater than k. Sets found to true if the key was found, and false otherwise. */ rbNode *jrb_find_gte(rbNode *root, Key & key, bool *found); rbNode *jrb_find_gte_gen(rbNode *root, Key & key, int (*func)(Key &, Key &), bool *found); /* Creates a node with key key and val val and inserts it into the tree before/after node nd. Does not check to ensure that you are keeping the correct order */ void jrb_delete_node(rbNode *node); /* Deletes and frees a node (but not the key or val) */ void jrb_free_tree(rbNode *root); /* Deletes and frees an entire tree */ void mk_new_int(rbNode *l, rbNode *r, rbNode *p, int il); rbNode *lprev(rbNode *n); rbNode *rprev(rbNode *n); void recolor(rbNode *n); void single_rotate(rbNode *y, int l); /* Inserts to the end of a list */ void insert_into_list(rbNode *item, rbNode *list); /* Deletes an arbitrary item */ void delete_item(rbNode *item); protected: rbNode *head; rbNode *cursor; void (*del_key_val)(Key &,Jval); int (*compare_key_val)(Key &,Key &); }; /* Traverse, where tree is a (rbTree *) */ #define jrb_traverse(tree) \ for((tree)->first(); !(tree)->endOfList(); (tree)->next()) #define jrb_rtraverse(tree) \ for((tree)->last(); !(tree)->endOfList(); (tree)->prev()) #include "Jrb.cpp" #include "rbTree.cpp" #endif