100 lines
2.3 KiB
C
100 lines
2.3 KiB
C
#ifndef UC_RBTREE_H_
|
|
#define UC_RBTREE_H_
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
typedef enum RBColor RBColor;
|
|
enum RBColor {
|
|
Red,
|
|
Black
|
|
};
|
|
|
|
typedef struct RBNode RBNode;
|
|
struct RBNode {
|
|
unsigned char color; //Red/Black
|
|
RBNode *parent;
|
|
RBNode *right;
|
|
RBNode *left;
|
|
void *data;
|
|
};
|
|
|
|
typedef struct RBTree RBTree;
|
|
struct RBTree {
|
|
RBNode *node;
|
|
int (*compare)(const RBNode *a, const RBNode *b);
|
|
};
|
|
|
|
/** Remove a node from the tree.
|
|
*
|
|
* @param root Root of the tree
|
|
* @param node Node to remove, the node must exist in the tree
|
|
*/
|
|
void uc_rb_remove(RBTree *root, RBNode *node);
|
|
|
|
/** Find a node in the tree.
|
|
*
|
|
* The value to find must be of the same type as the
|
|
* other nodes inserted into a tree. This will normally
|
|
* be a user defined struct with the RBNode as one of its members
|
|
* function of the tree is used to find the matching item.
|
|
*
|
|
* @param root Root of the tree
|
|
* @param val The value to find.
|
|
* @return the found node
|
|
*/
|
|
RBNode *uc_rb_find(RBTree *root, const RBNode *val);
|
|
|
|
/** insert a new node
|
|
*
|
|
* The actual value to insert must be in node->data.
|
|
* Of node->data matches an existing item in the tree,
|
|
* the existing node will be replaced and returned.
|
|
*
|
|
* @param root Root of the tree
|
|
* @param child The node to insert
|
|
* @return NULL, or an existing node if the new node replaced
|
|
* an existing node.
|
|
*/
|
|
RBNode *uc_rb_insert(RBTree *root, RBNode *child);
|
|
|
|
/** get the first node in the tree.
|
|
* the tree keeps the items in a sorted order, this
|
|
* will be the node where the compare function determins as the
|
|
* smallest node.
|
|
*
|
|
* @param root Root of the tree
|
|
* @return the first node, or null if the tree is empty
|
|
*/
|
|
RBNode *uc_rb_first(const RBTree *root);
|
|
|
|
/** Get the next node in the tree.
|
|
* the tree keeps the items in a sorted order, this
|
|
* will be the node after, based on the compare function ,
|
|
* the given node
|
|
*
|
|
* @param node The previous node
|
|
* @return the node after the passed in node, or NULL if the
|
|
* passed in node was the last.
|
|
*/
|
|
RBNode *uc_rb_next(RBNode *node);
|
|
|
|
/** Get the last node in the tree.
|
|
* the tree keeps the items in a sorted order, this
|
|
* will be the node where the compare function determins as the
|
|
* greatest node.
|
|
*
|
|
* @param root Root of the tree
|
|
* @return the last node, or null if the tree is empty
|
|
*/
|
|
RBNode *uc_rb_last(const RBTree *root);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
|
|
#endif
|
|
|