| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | .\" $NetBSD: rb.3,v 1.1 2010/03/19 18:02:22 dyoung Exp $ | | 1 | .\" $NetBSD: rb.3,v 1.2 2010/03/19 19:36:27 jruoho Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" Copyright (c) 2010 The NetBSD Foundation, Inc. | | 3 | .\" Copyright (c) 2010 The NetBSD Foundation, Inc. |
4 | .\" All rights reserved. | | 4 | .\" All rights reserved. |
5 | .\" | | 5 | .\" |
6 | .\" This code is derived from software contributed to The NetBSD Foundation | | 6 | .\" This code is derived from software contributed to The NetBSD Foundation |
7 | .\" by Matt Thomas, Niels Provos, and David Young. | | 7 | .\" by Matt Thomas, Niels Provos, and David Young. |
8 | .\" | | 8 | .\" |
9 | .\" Redistribution and use in source and binary forms, with or without | | 9 | .\" Redistribution and use in source and binary forms, with or without |
10 | .\" modification, are permitted provided that the following conditions | | 10 | .\" modification, are permitted provided that the following conditions |
11 | .\" are met: | | 11 | .\" are met: |
12 | .\" 1. Redistributions of source code must retain the above copyright | | 12 | .\" 1. Redistributions of source code must retain the above copyright |
13 | .\" notice, this list of conditions and the following disclaimer. | | 13 | .\" notice, this list of conditions and the following disclaimer. |
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | | 14 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| @@ -43,60 +43,62 @@ | | | @@ -43,60 +43,62 @@ |
43 | .Fn rb_tree_find_node "struct rb_tree *" "const void *" | | 43 | .Fn rb_tree_find_node "struct rb_tree *" "const void *" |
44 | .Ft struct rb_node * | | 44 | .Ft struct rb_node * |
45 | .Fn rb_tree_find_node_geq "struct rb_tree *" "const void *" | | 45 | .Fn rb_tree_find_node_geq "struct rb_tree *" "const void *" |
46 | .Ft struct rb_node * | | 46 | .Ft struct rb_node * |
47 | .Fn rb_tree_find_node_leq "struct rb_tree *" "const void *" | | 47 | .Fn rb_tree_find_node_leq "struct rb_tree *" "const void *" |
48 | .Ft struct rb_node * | | 48 | .Ft struct rb_node * |
49 | .Fn rb_tree_iterate "struct rb_tree *" "struct rb_node *" "const unsigned int" | | 49 | .Fn rb_tree_iterate "struct rb_tree *" "struct rb_node *" "const unsigned int" |
50 | .Sh DESCRIPTION | | 50 | .Sh DESCRIPTION |
51 | .Nm | | 51 | .Nm |
52 | provides red-black trees. | | 52 | provides red-black trees. |
53 | A red-black tree is a binary search tree with the node color as an | | 53 | A red-black tree is a binary search tree with the node color as an |
54 | extra attribute. | | 54 | extra attribute. |
55 | It fulfills a set of conditions: | | 55 | It fulfills a set of conditions: |
56 | .Bl -enum -compact -offset indent | | 56 | .Bl -enum -offset indent |
57 | .It | | 57 | .It |
58 | every search path from the root to a leaf consists of the same number of | | 58 | Every search path from the root to a leaf consists of the same number of |
59 | black nodes, | | 59 | black nodes. |
60 | .It | | 60 | .It |
61 | each red node (except for the root) has a black parent, | | 61 | Each red node (except for the root) has a black parent. |
62 | .It | | 62 | .It |
63 | each leaf node is black. | | 63 | Each leaf node is black. |
64 | .El | | 64 | .El |
65 | .Pp | | 65 | .Pp |
66 | Every operation on a red-black tree is bounded as O(lg n). | | 66 | Every operation on a red-black tree is bounded as O(lg n). |
67 | The maximum height of a red-black tree is 2lg (n+1). | | 67 | The maximum height of a red-black tree is 2lg (n+1). |
68 | .Sh TYPES | | 68 | .Sh TYPES |
69 | .Bl -tag -width compact | | 69 | .Bl -tag -width compact |
70 | .It Vt struct rb_tree | | 70 | .It Vt struct rb_tree |
71 | A red-black tree. | | 71 | A red-black tree. |
72 | .It Vt typedef signed int (*const rbto_compare_nodes_fn)(const struct rb_node *, const struct rb_node *); | | 72 | .It Vt typedef signed int \ |
| | | 73 | (*const rbto_compare_nodes_fn)(const struct rb_node *, const struct rb_node *); |
73 | The node-comparison operator. | | 74 | The node-comparison operator. |
74 | Defines an ordering on nodes. | | 75 | Defines an ordering on nodes. |
75 | Returns a positive value if the first node precedes the second node. | | 76 | Returns a positive value if the first node precedes the second node. |
76 | Returns a negative value if the first node follows the second node. | | 77 | Returns a negative value if the first node follows the second node. |
77 | Returns 0 if the first node and the second are identical according | | 78 | Returns 0 if the first node and the second are identical according |
78 | to the ordering. | | 79 | to the ordering. |
79 | .It Vt typedef signed int (*const rbto_compare_key_fn)(const struct rb_node *, const void *); | | 80 | .It Vt typedef signed int \ |
| | | 81 | (*const rbto_compare_key_fn)(const struct rb_node *, const void *); |
80 | The node-key comparison operator. | | 82 | The node-key comparison operator. |
81 | Defines the order of nodes and keys. | | 83 | Defines the order of nodes and keys. |
82 | Returns a positive value if the node precedes the key. | | 84 | Returns a positive value if the node precedes the key. |
83 | Returns a negative value if the node follows the key. | | 85 | Returns a negative value if the node follows the key. |
84 | Returns 0 if the node is identical to the ey according to the ordering. | | 86 | Returns 0 if the node is identical to the key according to the ordering. |
85 | .It Vt struct rb_tree_ops | | 87 | .It Vt struct rb_tree_ops |
86 | Defines the operators for comparing two nodes in the same tree, | | 88 | Defines the operators for comparing two nodes in the same tree, |
87 | and for comparing a node in the tree with a key. | | 89 | and for comparing a node in the tree with a key. |
88 | Members of | | 90 | Members of |
89 | .Vt rb_tree_ops | | 91 | .Vt rb_tree_ops |
90 | are | | 92 | are |
91 | .Bd -literal | | 93 | .Bd -literal |
92 | rbto_compare_nodes_fn rbto_compare_nodes; | | 94 | rbto_compare_nodes_fn rbto_compare_nodes; |
93 | rbto_compare_key_fn rbto_compare_key; | | 95 | rbto_compare_key_fn rbto_compare_key; |
94 | .Ed | | 96 | .Ed |
95 | .It Vt struct rb_node | | 97 | .It Vt struct rb_node |
96 | A node in a red-black tree. | | 98 | A node in a red-black tree. |
97 | .Sh FUNCTIONS | | 99 | .Sh FUNCTIONS |
98 | .Bl -tag -width compact | | 100 | .Bl -tag -width compact |
99 | .It Fn rb_tree_init "rbt" "ops" | | 101 | .It Fn rb_tree_init "rbt" "ops" |
100 | Initialize the red-black tree | | 102 | Initialize the red-black tree |
101 | .Fa rbt . | | 103 | .Fa rbt . |
102 | Let the comparison operators given by | | 104 | Let the comparison operators given by |
| @@ -111,80 +113,80 @@ Insert the node | | | @@ -111,80 +113,80 @@ Insert the node |
111 | into the tree | | 113 | into the tree |
112 | .Fa rbt . | | 114 | .Fa rbt . |
113 | Return | | 115 | Return |
114 | .Dv true | | 116 | .Dv true |
115 | on success, | | 117 | on success, |
116 | .Dv false | | 118 | .Dv false |
117 | on failure. | | 119 | on failure. |
118 | .It Fn rb_tree_find_node "rbt" "key" | | 120 | .It Fn rb_tree_find_node "rbt" "key" |
119 | Search the tree | | 121 | Search the tree |
120 | .Fa rbt | | 122 | .Fa rbt |
121 | for a node exactly matching | | 123 | for a node exactly matching |
122 | .Fa key . | | 124 | .Fa key . |
123 | If no such node is in the tree, return | | 125 | If no such node is in the tree, return |
124 | .Dv NULL . | | 126 | .Dv NULL . |
125 | Otherwise, return the matching node. | | 127 | Otherwise, return the matching node. |
126 | .It Fn rb_tree_find_node_geq "rbt" "key" | | 128 | .It Fn rb_tree_find_node_geq "rbt" "key" |
127 | Search the tree | | 129 | Search the tree |
128 | .Fa rbt | | 130 | .Fa rbt |
129 | for a node that exactly matches | | 131 | for a node that exactly matches |
130 | .Fa key | | 132 | .Fa key |
131 | and return it. | | 133 | and return it. |
132 | If no such node is present, return the first node following | | 134 | If no such node is present, return the first node following |
133 | .Fa key | | 135 | .Fa key |
134 | or, if no such node is in the tree, return | | 136 | or, if no such node is in the tree, return |
135 | .Dv NULL . | | 137 | .Dv NULL . |
136 | .It Fn rb_tree_find_node_leq "rbt" "key" | | 138 | .It Fn rb_tree_find_node_leq "rbt" "key" |
137 | Search the tree | | 139 | Search the tree |
138 | .Fa rbt | | 140 | .Fa rbt |
139 | for a node that exactly matches | | 141 | for a node that exactly matches |
140 | .Fa key | | 142 | .Fa key |
141 | and return it. | | 143 | and return it. |
142 | If no such node is present, return the first node preceding | | 144 | If no such node is present, return the first node preceding |
143 | .Fa key | | 145 | .Fa key |
144 | or, if no such node is in the tree, return | | 146 | or, if no such node is in the tree, return |
145 | .Dv NULL . | | 147 | .Dv NULL . |
146 | .It Fn rb_tree_iterate "rbt" "rb" "direction" | | 148 | .It Fn rb_tree_iterate "rbt" "rb" "direction" |
147 | If | | 149 | If |
148 | .Fa direction | | 150 | .Fa direction |
149 | is | | 151 | is |
150 | .Dv RB_DIR_LEFT , | | 152 | .Dv RB_DIR_LEFT , |
151 | return the node in the tree | | 153 | return the node in the tree |
152 | .Fa rbt | | 154 | .Fa rbt |
153 | immediately preceding the node | | 155 | immediately preceding the node |
154 | .Fa rb | | 156 | .Fa rb |
155 | or, if | | 157 | or, if |
156 | .Fa rb | | 158 | .Fa rb |
157 | is | | 159 | is |
158 | .Dv NULL , | | 160 | .Dv NULL , |
159 | return the last node in | | 161 | return the last node in |
160 | .Fa rbt | | 162 | .Fa rbt |
161 | or, if the tree is empty, return | | 163 | or, if the tree is empty, return |
162 | .Dv NULL . | | 164 | .Dv NULL . |
163 | .Pp | | 165 | .Pp |
164 | If | | 166 | If |
165 | .Fa direction | | 167 | .Fa direction |
166 | is | | 168 | is |
167 | .Dv RB_DIR_RIGHT , | | 169 | .Dv RB_DIR_RIGHT , |
168 | return the node in the tree | | 170 | return the node in the tree |
169 | .Fa rbt | | 171 | .Fa rbt |
170 | immediately following the node | | 172 | immediately following the node |
171 | .Fa rb | | 173 | .Fa rb |
172 | or, if | | 174 | or, if |
173 | .Fa rb | | 175 | .Fa rb |
174 | is | | 176 | is |
175 | .Dv NULL , | | 177 | .Dv NULL , |
176 | return the first node in | | 178 | return the first node in |
177 | .Fa rbt | | 179 | .Fa rbt |
178 | or, if the tree is empty, return | | 180 | or, if the tree is empty, return |
179 | .Dv NULL . | | 181 | .Dv NULL . |
180 | .El | | 182 | .El |
181 | .Sh CODE REFERENCES | | 183 | .Sh CODE REFERENCES |
182 | This section describes places within the | | 184 | This section describes places within the |
183 | .Nx | | 185 | .Nx |
184 | source tree where actual code implementing | | 186 | source tree where actual code implementing |
185 | .Nm | | 187 | .Nm |
186 | can be found. | | 188 | can be found. |
187 | All pathnames are relative to | | 189 | All pathnames are relative to |
188 | .Pa /usr/src . | | 190 | .Pa /usr/src . |
189 | .Pp | | 191 | .Pp |
190 | .Nm | | 192 | .Nm |
| @@ -197,17 +199,17 @@ is implemented within the file | | | @@ -197,17 +199,17 @@ is implemented within the file |
197 | .Sh HISTORY | | 199 | .Sh HISTORY |
198 | The | | 200 | The |
199 | .Nm | | 201 | .Nm |
200 | interface first appeared in | | 202 | interface first appeared in |
201 | .Nx 6.0 . | | 203 | .Nx 6.0 . |
202 | .Sh AUTHORS | | 204 | .Sh AUTHORS |
203 | .An Matt Thomas Aq matt@NetBSD.org | | 205 | .An Matt Thomas Aq matt@NetBSD.org |
204 | wrote | | 206 | wrote |
205 | .Nm . | | 207 | .Nm . |
206 | .Pp | | 208 | .Pp |
207 | .An Niels Provos Aq provos@citi.umich.edu | | 209 | .An Niels Provos Aq provos@citi.umich.edu |
208 | wrote the | | 210 | wrote the |
209 | .Xr tree 3 | | 211 | .Xr tree 3 |
210 | manual page. Portions of this page derive from that page. | | 212 | manual page. Portions of this page derive from that page. |
211 | .\" .Sh CAVEATS | | 213 | .\" .Sh CAVEATS |
212 | .\" .Sh BUGS | | 214 | .\" .Sh BUGS |
213 | .\" .Sh SECURITY CONSIDERATIONS | | 215 | .\" .Sh SECURITY CONSIDERATIONS |