blob: 8b22f5d3a53fbb45667182520696d8b3bec3156e [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_RBTREE_H__
20#define __PJ_RBTREE_H__
21
22/**
23 * @file rbtree.h
24 * @brief Red/Black Tree
25 */
26
27#include <pj/types.h>
28
29PJ_BEGIN_DECL
30
31/**
32 * @defgroup PJ_RBTREE Red/Black Balanced Tree
33 * @ingroup PJ_DS
34 * @brief
35 * Red/Black tree is the variant of balanced tree, where the search, insert,
36 * and delete operation is \b guaranteed to take at most \a O( lg(n) ).
37 * @{
38 */
39/**
40 * Color type for Red-Black tree.
41 */
42typedef enum pj_rbcolor_t
43{
44 PJ_RBCOLOR_BLACK,
45 PJ_RBCOLOR_RED
46} pj_rbcolor_t;
47
48/**
49 * The type of the node of the R/B Tree.
50 */
51typedef struct pj_rbtree_node
52{
53 /** Pointers to the node's parent, and left and right siblings. */
54 struct pj_rbtree_node *parent, *left, *right;
55
56 /** Key associated with the node. */
57 const void *key;
58
59 /** User data associated with the node. */
60 void *user_data;
61
62 /** The R/B Tree node color. */
63 pj_rbcolor_t color;
64
65} pj_rbtree_node;
66
67
68/**
69 * The type of function use to compare key value of tree node.
70 * @return
71 * 0 if the keys are equal
72 * <0 if key1 is lower than key2
73 * >0 if key1 is greater than key2.
74 */
75typedef int pj_rbtree_comp(const void *key1, const void *key2);
76
77
78/**
79 * Declaration of a red-black tree. All elements in the tree must have UNIQUE
80 * key.
81 * A red black tree always maintains the balance of the tree, so that the
82 * tree height will not be greater than lg(N). Insert, search, and delete
83 * operation will take lg(N) on the worst case. But for insert and delete,
84 * there is additional time needed to maintain the balance of the tree.
85 */
86typedef struct pj_rbtree
87{
88 pj_rbtree_node null_node; /**< Constant to indicate NULL node. */
89 pj_rbtree_node *null; /**< Constant to indicate NULL node. */
90 pj_rbtree_node *root; /**< Root tree node. */
91 unsigned size; /**< Number of elements in the tree. */
92 pj_rbtree_comp *comp; /**< Key comparison function. */
93} pj_rbtree;
94
95
96/**
97 * Guidance on how much memory required for each of the node.
98 */
99#define PJ_RBTREE_NODE_SIZE (sizeof(pj_rbtree_node))
100
101
102/**
103 * Guidance on memory required for the tree.
104 */
105#define PJ_RBTREE_SIZE (sizeof(pj_rbtree))
106
107
108/**
109 * Initialize the tree.
110 * @param tree the tree to be initialized.
111 * @param comp key comparison function to be used for this tree.
112 */
113PJ_DECL(void) pj_rbtree_init( pj_rbtree *tree, pj_rbtree_comp *comp);
114
115/**
116 * Get the first element in the tree.
117 * The first element always has the least value for the key, according to
118 * the comparison function.
119 * @param tree the tree.
120 * @return the tree node, or NULL if the tree has no element.
121 */
122PJ_DECL(pj_rbtree_node*) pj_rbtree_first( pj_rbtree *tree );
123
124/**
125 * Get the last element in the tree.
126 * The last element always has the greatest key value, according to the
127 * comparison function defined for the tree.
128 * @param tree the tree.
129 * @return the tree node, or NULL if the tree has no element.
130 */
131PJ_DECL(pj_rbtree_node*) pj_rbtree_last( pj_rbtree *tree );
132
133/**
134 * Get the successive element for the specified node.
135 * The successive element is an element with greater key value.
136 * @param tree the tree.
137 * @param node the node.
138 * @return the successive node, or NULL if the node has no successor.
139 */
140PJ_DECL(pj_rbtree_node*) pj_rbtree_next( pj_rbtree *tree,
141 pj_rbtree_node *node );
142
143/**
144 * The the previous node for the specified node.
145 * The previous node is an element with less key value.
146 * @param tree the tree.
147 * @param node the node.
148 * @return the previous node, or NULL if the node has no previous node.
149 */
150PJ_DECL(pj_rbtree_node*) pj_rbtree_prev( pj_rbtree *tree,
151 pj_rbtree_node *node );
152
153/**
154 * Insert a new node.
155 * The node will be inserted at sorted location. The key of the node must
156 * be UNIQUE, i.e. it hasn't existed in the tree.
157 * @param tree the tree.
158 * @param node the node to be inserted.
159 * @return zero on success, or -1 if the key already exist.
160 */
161PJ_DECL(int) pj_rbtree_insert( pj_rbtree *tree,
162 pj_rbtree_node *node );
163
164/**
165 * Find a node which has the specified key.
166 * @param tree the tree.
167 * @param key the key to search.
168 * @return the tree node with the specified key, or NULL if the key can not
169 * be found.
170 */
171PJ_DECL(pj_rbtree_node*) pj_rbtree_find( pj_rbtree *tree,
172 const void *key );
173
174/**
175 * Erase a node from the tree.
176 * @param tree the tree.
177 * @param node the node to be erased.
178 * @return the tree node itself.
179 */
180PJ_DECL(pj_rbtree_node*) pj_rbtree_erase( pj_rbtree *tree,
181 pj_rbtree_node *node );
182
183/**
184 * Get the maximum tree height from the specified node.
185 * @param tree the tree.
186 * @param node the node, or NULL to get the root of the tree.
187 * @return the maximum height, which should be at most lg(N)
188 */
189PJ_DECL(unsigned) pj_rbtree_max_height( pj_rbtree *tree,
190 pj_rbtree_node *node );
191
192/**
193 * Get the minumum tree height from the specified node.
194 * @param tree the tree.
195 * @param node the node, or NULL to get the root of the tree.
196 * @return the height
197 */
198PJ_DECL(unsigned) pj_rbtree_min_height( pj_rbtree *tree,
199 pj_rbtree_node *node );
200
201
202/**
203 * @}
204 */
205
206PJ_END_DECL
207
208#endif /* __PJ_RBTREE_H__ */
209