Rev 2416 | Rev 2450 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2416 | Rev 2421 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Vojtech Mencl |
2 | * Copyright (c) 2007 Vojtech Mencl |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericadt |
29 | /** @addtogroup genericadt |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief AVL tree implementation. |
35 | * @brief AVL tree implementation. |
36 | * |
36 | * |
37 | * This file implements AVL tree type and operations. |
37 | * This file implements AVL tree type and operations. |
38 | * |
38 | * |
39 | * Implemented AVL tree has the following properties: |
39 | * Implemented AVL tree has the following properties: |
40 | * @li it is binary search tree with non-unique keys |
40 | * @li it is binary search tree with non-unique keys |
41 | * @li Difference of heights of left and right subtree of every node is max 1. |
41 | * @li Difference of heights of left and right subtree of every node is max 1. |
42 | * |
42 | * |
43 | * Every node has pointer to its parent which allows insertion of multiple identical keys |
43 | * Every node has pointer to its parent which allows insertion of multiple identical keys |
44 | * into tree. |
44 | * into tree. |
45 | * |
45 | * |
46 | * Be careful of using this tree because of base atribute, which is added to every inserted |
46 | * Be careful of using this tree because of base atribute, which is added to every inserted |
47 | * node key. There is no rule in which order nodes with the same key are visited. |
47 | * node key. There is no rule in which order nodes with the same key are visited. |
48 | */ |
48 | */ |
49 | 49 | ||
50 | #include <adt/avl.h> |
50 | #include <adt/avl.h> |
51 | #include <debug.h> |
51 | #include <debug.h> |
52 | #include <panic.h> |
52 | #include <panic.h> |
53 | 53 | ||
54 | 54 | ||
55 | #define AVLTREE_LEFT 0 |
55 | #define AVLTREE_LEFT 0 |
56 | #define AVLTREE_RIGHT 1 |
56 | #define AVLTREE_RIGHT 1 |
57 | 57 | ||
58 | 58 | ||
59 | /** Search first occurence of given key in AVL tree. |
59 | /** Search first occurence of given key in AVL tree. |
60 | * |
60 | * |
61 | * @param t AVL tree. |
61 | * @param t AVL tree. |
62 | * @param key Key to be searched. |
62 | * @param key Key to be searched. |
63 | * |
63 | * |
64 | * @return Pointer to value or NULL if there is no such key. |
64 | * @return Pointer to node or NULL if there is no such key. |
65 | */ |
65 | */ |
66 | avltree_node_t *avltree_search(avltree_t *t, uint64_t key) |
66 | avltree_node_t *avltree_search(avltree_t *t, uint64_t key) |
67 | { |
67 | { |
68 | avltree_node_t *p; |
68 | avltree_node_t *p; |
69 | 69 | ||
70 | /* |
70 | /* |
71 | * Iteratively descend to the leaf that can contain the searched key. |
71 | * Iteratively descend to the leaf that can contain the searched key. |
72 | */ |
72 | */ |
73 | p = t->root; |
73 | p = t->root; |
74 | while (p != NULL) { |
74 | while (p != NULL) { |
75 | if (p->key > key) |
75 | if (p->key > key) |
76 | p = p->lft; |
76 | p = p->lft; |
77 | else if (p->key < key) |
77 | else if (p->key < key) |
78 | p = p->rgt; |
78 | p = p->rgt; |
79 | else { |
79 | else { |
80 | return p; |
80 | return p; |
81 | } |
81 | } |
82 | } |
82 | } |
83 | return NULL; |
83 | return NULL; |
84 | } |
84 | } |
85 | 85 | ||
86 | 86 | ||
- | 87 | /** Find node with smallest key in AVL tree. |
|
- | 88 | * |
|
- | 89 | * @param t AVL tree. |
|
- | 90 | * |
|
- | 91 | * @return Pointer to node or NULL if there is no node in the tree. |
|
- | 92 | */ |
|
- | 93 | avltree_node_t *avltree_find_min(avltree_t *t) |
|
- | 94 | { |
|
- | 95 | avltree_node_t *p = t->root; |
|
- | 96 | ||
- | 97 | /* |
|
- | 98 | * Check whether tree is empty. |
|
- | 99 | */ |
|
- | 100 | if (!p) return NULL; |
|
- | 101 | ||
- | 102 | /* |
|
- | 103 | * Iteratively descend to the leftmost leaf in the tree. |
|
- | 104 | */ |
|
- | 105 | while (p->lft != NULL) |
|
- | 106 | p = p->lft; |
|
- | 107 | ||
- | 108 | return p; |
|
- | 109 | } |
|
- | 110 | ||
87 | /** Insert new node into AVL tree. |
111 | /** Insert new node into AVL tree. |
88 | * |
112 | * |
89 | * @param t AVL tree. |
113 | * @param t AVL tree. |
90 | * @param newnode New node to be inserted. |
114 | * @param newnode New node to be inserted. |
91 | */ |
115 | */ |
92 | void avltree_insert(avltree_t *t, avltree_node_t *newnode) |
116 | void avltree_insert(avltree_t *t, avltree_node_t *newnode) |
93 | { |
117 | { |
94 | avltree_node_t *par; |
118 | avltree_node_t *par; |
95 | avltree_node_t *gpa; |
119 | avltree_node_t *gpa; |
96 | avltree_node_t *top; |
120 | avltree_node_t *top; |
97 | avltree_node_t **dpc; |
121 | avltree_node_t **dpc; |
98 | uint64_t key; |
122 | uint64_t key; |
99 | 123 | ||
100 | ASSERT(t); |
124 | ASSERT(t); |
101 | ASSERT(newnode); |
125 | ASSERT(newnode); |
102 | 126 | ||
103 | /* |
127 | /* |
104 | * Creating absolute key. |
128 | * Creating absolute key. |
105 | */ |
129 | */ |
106 | key = newnode->key + t->base; |
130 | key = newnode->key + t->base; |
107 | 131 | ||
108 | 132 | ||
109 | /* |
133 | /* |
110 | * Iteratively descend to the leaf that can will contain new node. |
134 | * Iteratively descend to the leaf that can will contain new node. |
111 | * Last node with non-zero balance in the way to leaf is stored as top - |
135 | * Last node with non-zero balance in the way to leaf is stored as top - |
112 | * it si place of possible balance failure. |
136 | * it si place of possible balance failure. |
113 | */ |
137 | */ |
114 | dpc = &t->root; |
138 | dpc = &t->root; |
115 | gpa = NULL; |
139 | gpa = NULL; |
116 | top = t->root; |
140 | top = t->root; |
117 | while (par = *dpc) { |
141 | while ((par = (*dpc)) != NULL) { |
118 | if (par->balance != 0) { |
142 | if (par->balance != 0) { |
119 | top = par; |
143 | top = par; |
120 | } |
144 | } |
121 | gpa = par; |
145 | gpa = par; |
122 | dpc = par->key > key? &par->lft: &par->rgt; |
146 | dpc = par->key > key? &par->lft: &par->rgt; |
123 | } |
147 | } |
124 | 148 | ||
125 | /* |
149 | /* |
126 | * Initialize new node. |
150 | * Initialize new node. |
127 | */ |
151 | */ |
128 | newnode->key = key; |
152 | newnode->key = key; |
129 | newnode->lft = NULL; |
153 | newnode->lft = NULL; |
130 | newnode->rgt = NULL; |
154 | newnode->rgt = NULL; |
131 | newnode->par = gpa; |
155 | newnode->par = gpa; |
132 | newnode->balance = 0; |
156 | newnode->balance = 0; |
133 | 157 | ||
134 | /* |
158 | /* |
135 | * Insert first node into empty tree. |
159 | * Insert first node into empty tree. |
136 | */ |
160 | */ |
137 | if (t->root == NULL) { |
161 | if (t->root == NULL) { |
138 | *dpc = newnode; |
162 | *dpc = newnode; |
139 | return; |
163 | return; |
140 | } |
164 | } |
141 | 165 | ||
142 | /* |
166 | /* |
143 | * Insert new node into previously found leaf place. |
167 | * Insert new node into previously found leaf place. |
144 | */ |
168 | */ |
145 | *dpc = newnode; |
169 | *dpc = newnode; |
146 | 170 | ||
147 | /* |
171 | /* |
148 | * If tree contains one node - end. |
172 | * If tree contains one node - end. |
149 | */ |
173 | */ |
150 | if (top == NULL) return; |
174 | if (top == NULL) return; |
151 | 175 | ||
152 | /* |
176 | /* |
153 | * Store pointer of top's father which points to the node with potentialy broken |
177 | * Store pointer of top's father which points to the node with potentialy broken |
154 | * balance (top). |
178 | * balance (top). |
155 | */ |
179 | */ |
156 | if (top->par == NULL) |
180 | if (top->par == NULL) |
157 | dpc = &t->root; |
181 | dpc = &t->root; |
158 | else |
182 | else |
159 | if (top->par->lft == top) |
183 | if (top->par->lft == top) |
160 | dpc = &(top->par->lft); |
184 | dpc = &(top->par->lft); |
161 | else |
185 | else |
162 | dpc = &(top->par->rgt); |
186 | dpc = &(top->par->rgt); |
163 | 187 | ||
164 | /* |
188 | /* |
165 | * Repair all balances on the way from top node to newly inserted node. |
189 | * Repair all balances on the way from top node to newly inserted node. |
166 | */ |
190 | */ |
167 | par = top; |
191 | par = top; |
168 | while (par != newnode) { |
192 | while (par != newnode) { |
169 | if (par->key > key) { |
193 | if (par->key > key) { |
170 | par->balance--; |
194 | par->balance--; |
171 | par = par->lft; |
195 | par = par->lft; |
172 | } else { |
196 | } else { |
173 | par->balance++; |
197 | par->balance++; |
174 | par = par->rgt; |
198 | par = par->rgt; |
175 | } |
199 | } |
176 | } |
200 | } |
177 | 201 | ||
178 | /* |
202 | /* |
179 | * To balance tree we must check and balance top node. |
203 | * To balance tree we must check and balance top node. |
180 | */ |
204 | */ |
181 | if (top->balance == -2) { |
205 | if (top->balance == -2) { |
182 | par = top->lft; |
206 | par = top->lft; |
183 | if (par->balance == -1) { |
207 | if (par->balance == -1) { |
184 | /* |
208 | /* |
185 | * LL rotation. |
209 | * LL rotation. |
186 | */ |
210 | */ |
187 | top->lft = par->rgt; |
211 | top->lft = par->rgt; |
188 | if (top->lft != NULL) top->lft->par = top; |
212 | if (top->lft != NULL) top->lft->par = top; |
189 | par->par = top->par; |
213 | par->par = top->par; |
190 | top->par = par; |
214 | top->par = par; |
191 | par->rgt = top; |
215 | par->rgt = top; |
192 | par->balance = top->balance = 0; |
216 | par->balance = top->balance = 0; |
193 | *dpc = par; |
217 | *dpc = par; |
194 | } else { |
218 | } else { |
195 | /* |
219 | /* |
196 | * LR rotation. |
220 | * LR rotation. |
197 | */ |
221 | */ |
198 | ASSERT(par->balance == 1); |
222 | ASSERT(par->balance == 1); |
199 | 223 | ||
200 | gpa = par->rgt; |
224 | gpa = par->rgt; |
201 | par->rgt = gpa->lft; |
225 | par->rgt = gpa->lft; |
202 | if (gpa->lft != NULL) gpa->lft->par = par; |
226 | if (gpa->lft != NULL) gpa->lft->par = par; |
203 | gpa->lft = par; |
227 | gpa->lft = par; |
204 | par->par = gpa; |
228 | par->par = gpa; |
205 | top->lft = gpa->rgt; |
229 | top->lft = gpa->rgt; |
206 | if (gpa->rgt != NULL) gpa->rgt->par = top; |
230 | if (gpa->rgt != NULL) gpa->rgt->par = top; |
207 | gpa->rgt = top; |
231 | gpa->rgt = top; |
208 | gpa->par = top->par; |
232 | gpa->par = top->par; |
209 | top->par = gpa; |
233 | top->par = gpa; |
210 | 234 | ||
211 | if (gpa->balance == -1) { |
235 | if (gpa->balance == -1) { |
212 | par->balance = 0; |
236 | par->balance = 0; |
213 | top->balance = 1; |
237 | top->balance = 1; |
214 | } else if (gpa->balance == 0) { |
238 | } else if (gpa->balance == 0) { |
215 | par->balance = top->balance = 0; |
239 | par->balance = top->balance = 0; |
216 | } else { |
240 | } else { |
217 | par->balance = -1; |
241 | par->balance = -1; |
218 | top->balance = 0; |
242 | top->balance = 0; |
219 | } |
243 | } |
220 | gpa->balance = 0; |
244 | gpa->balance = 0; |
221 | *dpc = gpa; |
245 | *dpc = gpa; |
222 | } |
246 | } |
223 | } else if (top->balance == 2) { |
247 | } else if (top->balance == 2) { |
224 | par = top->rgt; |
248 | par = top->rgt; |
225 | if (par->balance == 1) { |
249 | if (par->balance == 1) { |
226 | /* |
250 | /* |
227 | * RR rotation. |
251 | * RR rotation. |
228 | */ |
252 | */ |
229 | top->rgt = par->lft; |
253 | top->rgt = par->lft; |
230 | if (top->rgt != NULL) top->rgt->par = top; |
254 | if (top->rgt != NULL) top->rgt->par = top; |
231 | par->par = top->par; |
255 | par->par = top->par; |
232 | top->par = par; |
256 | top->par = par; |
233 | par->lft = top; |
257 | par->lft = top; |
234 | par->balance = top->balance = 0; |
258 | par->balance = top->balance = 0; |
235 | *dpc = par; |
259 | *dpc = par; |
236 | } else { |
260 | } else { |
237 | /* |
261 | /* |
238 | * RL rotation. |
262 | * RL rotation. |
239 | */ |
263 | */ |
240 | ASSERT(par->balance == -1); |
264 | ASSERT(par->balance == -1); |
241 | 265 | ||
242 | gpa = par->lft; |
266 | gpa = par->lft; |
243 | par->lft = gpa->rgt; |
267 | par->lft = gpa->rgt; |
244 | if (gpa->rgt != NULL) gpa->rgt->par = par; |
268 | if (gpa->rgt != NULL) gpa->rgt->par = par; |
245 | gpa->rgt = par; |
269 | gpa->rgt = par; |
246 | par->par = gpa; |
270 | par->par = gpa; |
247 | top->rgt = gpa->lft; |
271 | top->rgt = gpa->lft; |
248 | if (gpa->lft != NULL) gpa->lft->par = top; |
272 | if (gpa->lft != NULL) gpa->lft->par = top; |
249 | gpa->lft = top; |
273 | gpa->lft = top; |
250 | gpa->par = top->par; |
274 | gpa->par = top->par; |
251 | top->par = gpa; |
275 | top->par = gpa; |
252 | 276 | ||
253 | if (gpa->balance == 1) { |
277 | if (gpa->balance == 1) { |
254 | par->balance = 0; |
278 | par->balance = 0; |
255 | top->balance = -1; |
279 | top->balance = -1; |
256 | } else if (gpa->balance == 0) { |
280 | } else if (gpa->balance == 0) { |
257 | par->balance = top->balance = 0; |
281 | par->balance = top->balance = 0; |
258 | } else { |
282 | } else { |
259 | par->balance = 1; |
283 | par->balance = 1; |
260 | top->balance = 0; |
284 | top->balance = 0; |
261 | } |
285 | } |
262 | gpa->balance = 0; |
286 | gpa->balance = 0; |
263 | *dpc = gpa; |
287 | *dpc = gpa; |
264 | } |
288 | } |
265 | } else { |
289 | } else { |
266 | /* |
290 | /* |
267 | * Balance is not broken, insertion is finised. |
291 | * Balance is not broken, insertion is finised. |
268 | */ |
292 | */ |
269 | return; |
293 | return; |
270 | } |
294 | } |
271 | 295 | ||
272 | } |
296 | } |
273 | 297 | ||
274 | /** Delete node from AVL tree. |
298 | /** Delete node from AVL tree. |
275 | * |
299 | * |
276 | * Because of allowed multiple identical keys parent pointers are essential |
300 | * Because of allowed multiple identical keys parent pointers are essential |
277 | * during deletition. |
301 | * during deletition. |
278 | * |
302 | * |
279 | * @param t AVL tree structure. |
303 | * @param t AVL tree structure. |
280 | * @param node Address of node which will be deleted. |
304 | * @param node Address of node which will be deleted. |
281 | */ |
305 | */ |
282 | void avltree_delete(avltree_t *t, avltree_node_t *node) |
306 | void avltree_delete(avltree_t *t, avltree_node_t *node) |
283 | { |
307 | { |
284 | avltree_node_t *cur; |
308 | avltree_node_t *cur; |
285 | avltree_node_t *par; |
309 | avltree_node_t *par; |
286 | avltree_node_t *gpa; |
310 | avltree_node_t *gpa; |
287 | uint8_t dir; |
311 | uint8_t dir; |
288 | 312 | ||
289 | ASSERT(t); |
313 | ASSERT(t); |
290 | ASSERT(node); |
314 | ASSERT(node); |
291 | 315 | ||
292 | 316 | ||
293 | if (node->lft == NULL) { |
317 | if (node->lft == NULL) { |
294 | if (node->rgt) { |
318 | if (node->rgt) { |
295 | /* |
319 | /* |
296 | * Replace node with its only right son. |
320 | * Replace node with its only right son. |
297 | * |
321 | * |
298 | * Balance of right son will be repaired in balancing cycle. |
322 | * Balance of right son will be repaired in balancing cycle. |
299 | */ |
323 | */ |
300 | cur = node->rgt; |
324 | cur = node->rgt; |
301 | cur->par = node->par; |
325 | cur->par = node->par; |
302 | gpa = cur; |
326 | gpa = cur; |
303 | dir = AVLTREE_RIGHT; |
327 | dir = AVLTREE_RIGHT; |
304 | cur->balance = node->balance; |
328 | cur->balance = node->balance; |
305 | } else { |
329 | } else { |
306 | if (node->par == NULL) { |
330 | if (node->par == NULL) { |
307 | /* |
331 | /* |
308 | * Tree has only one node - it will be empty tree and balancing can end. |
332 | * Tree has only one node - it will be empty tree and balancing can end. |
309 | */ |
333 | */ |
310 | t->root = NULL; |
334 | t->root = NULL; |
311 | return; |
335 | return; |
312 | } |
336 | } |
313 | /* |
337 | /* |
314 | * Node has no child, will be deleted with no substitution. |
338 | * Node has no child, will be deleted with no substitution. |
315 | */ |
339 | */ |
316 | gpa = node->par; |
340 | gpa = node->par; |
317 | cur = NULL; |
341 | cur = NULL; |
318 | dir = (gpa->lft == node)? AVLTREE_LEFT: AVLTREE_RIGHT; |
342 | dir = (gpa->lft == node)? AVLTREE_LEFT: AVLTREE_RIGHT; |
319 | } |
343 | } |
320 | } else { |
344 | } else { |
321 | /* |
345 | /* |
322 | * Node has left and right son. Find a node with smallest key in left subtree |
346 | * Node has left and right son. Find a node with smallest key in left subtree |
323 | * and replace deleted node with that node. |
347 | * and replace deleted node with that node. |
324 | */ |
348 | */ |
325 | for (cur = node->lft; cur->rgt != NULL; cur = cur->rgt) |
349 | for (cur = node->lft; cur->rgt != NULL; cur = cur->rgt) |
326 | ; |
350 | ; |
327 | 351 | ||
328 | //cur obsahuje uzel, ktery vlozim misto uzlu node |
352 | //cur obsahuje uzel, ktery vlozim misto uzlu node |
329 | 353 | ||
330 | if (cur != node->lft) { |
354 | if (cur != node->lft) { |
331 | /* |
355 | /* |
332 | * The most right node of deleted node's left subtree was found. Replace |
356 | * The most right node of deleted node's left subtree was found. Replace |
333 | * deleted node with this node. Cutting founded node has two cases in |
357 | * deleted node with this node. Cutting founded node has two cases in |
334 | * dependence on its left son. |
358 | * dependence on its left son. |
335 | */ |
359 | */ |
336 | if (cur->lft) { |
360 | if (cur->lft) { |
337 | /* |
361 | /* |
338 | * Founded node has left son. |
362 | * Founded node has left son. |
339 | */ |
363 | */ |
340 | gpa = cur->lft; |
364 | gpa = cur->lft; |
341 | gpa->par = cur->par; |
365 | gpa->par = cur->par; |
342 | dir = AVLTREE_LEFT; |
366 | dir = AVLTREE_LEFT; |
343 | gpa->balance = cur->balance; |
367 | gpa->balance = cur->balance; |
344 | } else { |
368 | } else { |
345 | dir = AVLTREE_RIGHT; |
369 | dir = AVLTREE_RIGHT; |
346 | gpa = cur->par; |
370 | gpa = cur->par; |
347 | } |
371 | } |
348 | cur->par->rgt = cur->lft; |
372 | cur->par->rgt = cur->lft; |
349 | cur->lft = node->lft; |
373 | cur->lft = node->lft; |
350 | cur->lft->par = cur; |
374 | cur->lft->par = cur; |
351 | } else { |
375 | } else { |
352 | /* |
376 | /* |
353 | * Left son of node hasn't got right son. Left son will take deleted node's place. |
377 | * Left son of node hasn't got right son. Left son will take deleted node's place. |
354 | */ |
378 | */ |
355 | dir = AVLTREE_LEFT; |
379 | dir = AVLTREE_LEFT; |
356 | gpa = cur; |
380 | gpa = cur; |
357 | } |
381 | } |
358 | if (node->rgt) node->rgt->par = cur; |
382 | if (node->rgt) node->rgt->par = cur; |
359 | cur->rgt = node->rgt; |
383 | cur->rgt = node->rgt; |
360 | cur->balance = node->balance; |
384 | cur->balance = node->balance; |
361 | cur->par = node->par; |
385 | cur->par = node->par; |
362 | } |
386 | } |
363 | 387 | ||
364 | /* |
388 | /* |
365 | * Repair parent nodes pointer which pointed previously to deleted node. |
389 | * Repair parent nodes pointer which pointed previously to deleted node. |
366 | */ |
390 | */ |
367 | if (node->par == NULL) { |
391 | if (node->par == NULL) { |
368 | t->root = cur; |
392 | t->root = cur; |
369 | } else { |
393 | } else { |
370 | if (node->par->lft == node) { |
394 | if (node->par->lft == node) { |
371 | node->par->lft = cur; |
395 | node->par->lft = cur; |
372 | } else { |
396 | } else { |
373 | node->par->rgt = cur; |
397 | node->par->rgt = cur; |
374 | } |
398 | } |
375 | } |
399 | } |
376 | 400 | ||
377 | /* |
401 | /* |
378 | * Repair cycle which repairs balances of nodes in way from cutted noded down to root. |
402 | * Repair cycle which repairs balances of nodes in way from cutted noded down to root. |
379 | */ |
403 | */ |
380 | for(;;) { |
404 | for(;;) { |
381 | if (dir == AVLTREE_LEFT) { |
405 | if (dir == AVLTREE_LEFT) { |
382 | /* |
406 | /* |
383 | * Deletition was made in left subtree. |
407 | * Deletition was made in left subtree. |
384 | */ |
408 | */ |
385 | gpa->balance++; |
409 | gpa->balance++; |
386 | if (gpa->balance == +1) { |
410 | if (gpa->balance == +1) { |
387 | /* |
411 | /* |
388 | * Stop balancing, tree is balanced. |
412 | * Stop balancing, tree is balanced. |
389 | */ |
413 | */ |
390 | break; |
414 | break; |
391 | } else if (gpa->balance == +2) { |
415 | } else if (gpa->balance == +2) { |
392 | /* |
416 | /* |
393 | * Bad balance, heights of left and right subtrees differs more then is allowed. |
417 | * Bad balance, heights of left and right subtrees differs more then is allowed. |
394 | */ |
418 | */ |
395 | par = gpa->rgt; |
419 | par = gpa->rgt; |
396 | 420 | ||
397 | if (par->balance == -1) { |
421 | if (par->balance == -1) { |
398 | /* |
422 | /* |
399 | * RL rotation. |
423 | * RL rotation. |
400 | */ |
424 | */ |
401 | 425 | ||
402 | cur = par->lft; |
426 | cur = par->lft; |
403 | par->lft = cur->rgt; |
427 | par->lft = cur->rgt; |
404 | cur->rgt = par; |
428 | cur->rgt = par; |
405 | gpa->rgt = cur->lft; |
429 | gpa->rgt = cur->lft; |
406 | cur->lft = gpa; |
430 | cur->lft = gpa; |
407 | 431 | ||
408 | /* |
432 | /* |
409 | * Repair balances and paternity of childs in dependence on |
433 | * Repair balances and paternity of childs in dependence on |
410 | * balance factor of grand child (cur). |
434 | * balance factor of grand child (cur). |
411 | */ |
435 | */ |
412 | if (cur->balance == +1) { |
436 | if (cur->balance == +1) { |
413 | par->balance = 0; |
437 | par->balance = 0; |
414 | gpa->balance = -1; |
438 | gpa->balance = -1; |
415 | if (gpa->rgt) gpa->rgt->par = gpa; |
439 | if (gpa->rgt) gpa->rgt->par = gpa; |
416 | par->lft->par = par; |
440 | par->lft->par = par; |
417 | } else if (cur->balance == 0) { |
441 | } else if (cur->balance == 0) { |
418 | par->balance = gpa->balance = 0; |
442 | par->balance = gpa->balance = 0; |
419 | if (gpa->rgt) gpa->rgt->par = gpa; |
443 | if (gpa->rgt) gpa->rgt->par = gpa; |
420 | if (par->lft) par->lft->par = par; |
444 | if (par->lft) par->lft->par = par; |
421 | } else { |
445 | } else { |
422 | par->balance = +1; |
446 | par->balance = +1; |
423 | gpa->balance = 0; |
447 | gpa->balance = 0; |
424 | if (par->lft) par->lft->par = par; |
448 | if (par->lft) par->lft->par = par; |
425 | gpa->rgt->par = gpa; |
449 | gpa->rgt->par = gpa; |
426 | } |
450 | } |
427 | cur->balance = 0; |
451 | cur->balance = 0; |
428 | 452 | ||
429 | /* |
453 | /* |
430 | * Repair paternity. |
454 | * Repair paternity. |
431 | */ |
455 | */ |
432 | cur->par = gpa->par; |
456 | cur->par = gpa->par; |
433 | gpa->par = cur; |
457 | gpa->par = cur; |
434 | par->par = cur; |
458 | par->par = cur; |
435 | 459 | ||
436 | /* |
460 | /* |
437 | * Repair pointer which pointed to balanced node. If it was root then balancing |
461 | * Repair pointer which pointed to balanced node. If it was root then balancing |
438 | * is finished else continue in next iteration (parent node). |
462 | * is finished else continue in next iteration (parent node). |
439 | */ |
463 | */ |
440 | if (cur->par == NULL) { |
464 | if (cur->par == NULL) { |
441 | t->root = cur; |
465 | t->root = cur; |
442 | break; |
466 | break; |
443 | } else |
467 | } else |
444 | if (cur->par->lft == gpa) { |
468 | if (cur->par->lft == gpa) { |
445 | cur->par->lft = cur; |
469 | cur->par->lft = cur; |
446 | dir = AVLTREE_LEFT; |
470 | dir = AVLTREE_LEFT; |
447 | } else { |
471 | } else { |
448 | cur->par->rgt = cur; |
472 | cur->par->rgt = cur; |
449 | dir = AVLTREE_RIGHT; |
473 | dir = AVLTREE_RIGHT; |
450 | } |
474 | } |
451 | gpa = cur->par; |
475 | gpa = cur->par; |
452 | } else { |
476 | } else { |
453 | /* |
477 | /* |
454 | * RR rotation. |
478 | * RR rotation. |
455 | */ |
479 | */ |
456 | 480 | ||
457 | gpa->rgt = par->lft; |
481 | gpa->rgt = par->lft; |
458 | if (par->lft) par->lft->par = gpa; |
482 | if (par->lft) par->lft->par = gpa; |
459 | par->lft = gpa; |
483 | par->lft = gpa; |
460 | 484 | ||
461 | /* |
485 | /* |
462 | * Repair paternity. |
486 | * Repair paternity. |
463 | */ |
487 | */ |
464 | par->par = gpa->par; |
488 | par->par = gpa->par; |
465 | gpa->par = par; |
489 | gpa->par = par; |
466 | 490 | ||
467 | if (par->balance == 0) { |
491 | if (par->balance == 0) { |
468 | /* |
492 | /* |
469 | * Right child of balanced node is balanced, after RR rotation is done, whole |
493 | * Right child of balanced node is balanced, after RR rotation is done, whole |
470 | * tree will be balanced. |
494 | * tree will be balanced. |
471 | */ |
495 | */ |
472 | par->balance = -1; |
496 | par->balance = -1; |
473 | gpa->balance = +1; |
497 | gpa->balance = +1; |
474 | 498 | ||
475 | /* |
499 | /* |
476 | * Repair pointer which pointed to balanced node and finish balancing. |
500 | * Repair pointer which pointed to balanced node and finish balancing. |
477 | */ |
501 | */ |
478 | if (par->par == NULL) { |
502 | if (par->par == NULL) { |
479 | t->root = par; |
503 | t->root = par; |
480 | } else |
504 | } else |
481 | if (par->par->lft == gpa) { |
505 | if (par->par->lft == gpa) { |
482 | par->par->lft = par; |
506 | par->par->lft = par; |
483 | } else { |
507 | } else { |
484 | par->par->rgt = par; |
508 | par->par->rgt = par; |
485 | } |
509 | } |
486 | break; |
510 | break; |
487 | } else { |
511 | } else { |
488 | par->balance = gpa->balance = 0; |
512 | par->balance = gpa->balance = 0; |
489 | /* |
513 | /* |
490 | * Repair pointer which pointed to balanced node. If it was root then balancing |
514 | * Repair pointer which pointed to balanced node. If it was root then balancing |
491 | * is finished else continue in next iteration (parent node). |
515 | * is finished else continue in next iteration (parent node). |
492 | */ |
516 | */ |
493 | if (par->par == NULL) { |
517 | if (par->par == NULL) { |
494 | t->root = par; |
518 | t->root = par; |
495 | break; |
519 | break; |
496 | } else { |
520 | } else { |
497 | 521 | ||
498 | if (par->par->lft == gpa) { |
522 | if (par->par->lft == gpa) { |
499 | par->par->lft = par; |
523 | par->par->lft = par; |
500 | dir = AVLTREE_LEFT; |
524 | dir = AVLTREE_LEFT; |
501 | } else { |
525 | } else { |
502 | par->par->rgt = par; |
526 | par->par->rgt = par; |
503 | dir = AVLTREE_RIGHT; |
527 | dir = AVLTREE_RIGHT; |
504 | } |
528 | } |
505 | } |
529 | } |
506 | } |
530 | } |
507 | gpa = par->par; |
531 | gpa = par->par; |
508 | } |
532 | } |
509 | } else { |
533 | } else { |
510 | /* |
534 | /* |
511 | * Repair pointer which pointed to balanced node. If it was root then balancing |
535 | * Repair pointer which pointed to balanced node. If it was root then balancing |
512 | * is finished else continue in next iteration (parent node). |
536 | * is finished else continue in next iteration (parent node). |
513 | */ |
537 | */ |
514 | if (!gpa->par) break; |
538 | if (!gpa->par) break; |
515 | 539 | ||
516 | if (gpa->par->lft == gpa) { |
540 | if (gpa->par->lft == gpa) { |
517 | dir = AVLTREE_LEFT; |
541 | dir = AVLTREE_LEFT; |
518 | } else { |
542 | } else { |
519 | dir = AVLTREE_RIGHT; |
543 | dir = AVLTREE_RIGHT; |
520 | } |
544 | } |
521 | gpa = gpa->par; |
545 | gpa = gpa->par; |
522 | } |
546 | } |
523 | } else { |
547 | } else { |
524 | /* |
548 | /* |
525 | * Deletition was made in right subtree. |
549 | * Deletition was made in right subtree. |
526 | */ |
550 | */ |
527 | gpa->balance--; |
551 | gpa->balance--; |
528 | if (gpa->balance == -1) { |
552 | if (gpa->balance == -1) { |
529 | /* |
553 | /* |
530 | * Stop balancing, tree is balanced. |
554 | * Stop balancing, tree is balanced. |
531 | */ |
555 | */ |
532 | break; |
556 | break; |
533 | } else if (gpa->balance == -2) { |
557 | } else if (gpa->balance == -2) { |
534 | /* |
558 | /* |
535 | * Bad balance, heights of left and right subtrees differs more then is allowed. |
559 | * Bad balance, heights of left and right subtrees differs more then is allowed. |
536 | */ |
560 | */ |
537 | par = gpa->lft; |
561 | par = gpa->lft; |
538 | 562 | ||
539 | if (par->balance == +1) { |
563 | if (par->balance == +1) { |
540 | /* |
564 | /* |
541 | * LR rotation. |
565 | * LR rotation. |
542 | */ |
566 | */ |
543 | 567 | ||
544 | cur = par->rgt; |
568 | cur = par->rgt; |
545 | par->rgt = cur->lft; |
569 | par->rgt = cur->lft; |
546 | cur->lft = par; |
570 | cur->lft = par; |
547 | gpa->lft = cur->rgt; |
571 | gpa->lft = cur->rgt; |
548 | cur->rgt = gpa; |
572 | cur->rgt = gpa; |
549 | 573 | ||
550 | /* |
574 | /* |
551 | * Repair balances and paternity of childs in dependence on |
575 | * Repair balances and paternity of childs in dependence on |
552 | * balance factor of grand child (cur). |
576 | * balance factor of grand child (cur). |
553 | */ |
577 | */ |
554 | if (cur->balance == -1) { |
578 | if (cur->balance == -1) { |
555 | par->balance = 0; |
579 | par->balance = 0; |
556 | gpa->balance = +1; |
580 | gpa->balance = +1; |
557 | if (gpa->lft) gpa->lft->par = gpa; |
581 | if (gpa->lft) gpa->lft->par = gpa; |
558 | par->rgt->par = par; |
582 | par->rgt->par = par; |
559 | } else if (cur->balance == 0) { |
583 | } else if (cur->balance == 0) { |
560 | par->balance = gpa->balance = 0; |
584 | par->balance = gpa->balance = 0; |
561 | if (gpa->lft) gpa->lft->par = gpa; |
585 | if (gpa->lft) gpa->lft->par = gpa; |
562 | if (par->rgt) par->rgt->par = par; |
586 | if (par->rgt) par->rgt->par = par; |
563 | } else { |
587 | } else { |
564 | par->balance = -1; |
588 | par->balance = -1; |
565 | gpa->balance = 0; |
589 | gpa->balance = 0; |
566 | if (par->rgt) par->rgt->par = par; |
590 | if (par->rgt) par->rgt->par = par; |
567 | gpa->lft->par = gpa; |
591 | gpa->lft->par = gpa; |
568 | } |
592 | } |
569 | cur->balance = 0; |
593 | cur->balance = 0; |
570 | 594 | ||
571 | /* |
595 | /* |
572 | * Repair paternity. |
596 | * Repair paternity. |
573 | */ |
597 | */ |
574 | cur->par = gpa->par; |
598 | cur->par = gpa->par; |
575 | gpa->par = cur; |
599 | gpa->par = cur; |
576 | par->par = cur; |
600 | par->par = cur; |
577 | 601 | ||
578 | /* |
602 | /* |
579 | * Repair pointer which pointed to balanced node. If it was root then balancing |
603 | * Repair pointer which pointed to balanced node. If it was root then balancing |
580 | * is finished else continue in next iteration (parent node). |
604 | * is finished else continue in next iteration (parent node). |
581 | */ |
605 | */ |
582 | if (cur->par == NULL) { |
606 | if (cur->par == NULL) { |
583 | t->root = cur; |
607 | t->root = cur; |
584 | break; |
608 | break; |
585 | } else |
609 | } else |
586 | if (cur->par->lft == gpa) { |
610 | if (cur->par->lft == gpa) { |
587 | cur->par->lft = cur; |
611 | cur->par->lft = cur; |
588 | dir = AVLTREE_LEFT; |
612 | dir = AVLTREE_LEFT; |
589 | } else { |
613 | } else { |
590 | cur->par->rgt = cur; |
614 | cur->par->rgt = cur; |
591 | dir = AVLTREE_RIGHT; |
615 | dir = AVLTREE_RIGHT; |
592 | } |
616 | } |
593 | gpa = cur->par; |
617 | gpa = cur->par; |
594 | } else { |
618 | } else { |
595 | /* |
619 | /* |
596 | * LL rotation. |
620 | * LL rotation. |
597 | */ |
621 | */ |
598 | 622 | ||
599 | gpa->lft = par->rgt; |
623 | gpa->lft = par->rgt; |
600 | if (par->rgt) par->rgt->par = gpa; |
624 | if (par->rgt) par->rgt->par = gpa; |
601 | par->rgt = gpa; |
625 | par->rgt = gpa; |
602 | /* |
626 | /* |
603 | * Repair paternity. |
627 | * Repair paternity. |
604 | */ |
628 | */ |
605 | par->par = gpa->par; |
629 | par->par = gpa->par; |
606 | gpa->par = par; |
630 | gpa->par = par; |
607 | 631 | ||
608 | if (par->balance == 0) { |
632 | if (par->balance == 0) { |
609 | /* |
633 | /* |
610 | * Left child of balanced node is balanced, after RR rotation is done, whole |
634 | * Left child of balanced node is balanced, after RR rotation is done, whole |
611 | * tree will be balanced. |
635 | * tree will be balanced. |
612 | */ |
636 | */ |
613 | par->balance = +1; |
637 | par->balance = +1; |
614 | gpa->balance = -1; |
638 | gpa->balance = -1; |
615 | 639 | ||
616 | /* |
640 | /* |
617 | * Repair pointer which pointed to balanced node and finish balancing. |
641 | * Repair pointer which pointed to balanced node and finish balancing. |
618 | */ |
642 | */ |
619 | if (par->par == NULL) { |
643 | if (par->par == NULL) { |
620 | t->root = par; |
644 | t->root = par; |
621 | } else |
645 | } else |
622 | if (par->par->lft == gpa) { |
646 | if (par->par->lft == gpa) { |
623 | par->par->lft = par; |
647 | par->par->lft = par; |
624 | } else { |
648 | } else { |
625 | par->par->rgt = par; |
649 | par->par->rgt = par; |
626 | } |
650 | } |
627 | break; |
651 | break; |
628 | } else { |
652 | } else { |
629 | par->balance = gpa->balance = 0; |
653 | par->balance = gpa->balance = 0; |
630 | 654 | ||
631 | /* |
655 | /* |
632 | * Repair pointer which pointed to balanced node. If it was root then balancing |
656 | * Repair pointer which pointed to balanced node. If it was root then balancing |
633 | * is finished else continue in next iteration (parent node). |
657 | * is finished else continue in next iteration (parent node). |
634 | */ |
658 | */ |
635 | if (par->par == NULL) { |
659 | if (par->par == NULL) { |
636 | t->root = par; |
660 | t->root = par; |
637 | break; |
661 | break; |
638 | } else { |
662 | } else { |
639 | if (par->par->lft == gpa) { //v cyklu jsem ani jednou neskocil - cur nema praveho syna |
663 | if (par->par->lft == gpa) { //v cyklu jsem ani jednou neskocil - cur nema praveho syna |
640 | par->par->lft = par; |
664 | par->par->lft = par; |
641 | dir = AVLTREE_LEFT; |
665 | dir = AVLTREE_LEFT; |
642 | } else { |
666 | } else { |
643 | par->par->rgt = par; |
667 | par->par->rgt = par; |
644 | dir = AVLTREE_RIGHT; |
668 | dir = AVLTREE_RIGHT; |
645 | } |
669 | } |
646 | } |
670 | } |
647 | } |
671 | } |
648 | gpa = par->par; |
672 | gpa = par->par; |
649 | } |
673 | } |
650 | } else { |
674 | } else { |
651 | /* |
675 | /* |
652 | * Repair pointer which pointed to balanced node. If it was root then balancing |
676 | * Repair pointer which pointed to balanced node. If it was root then balancing |
653 | * is finished else continue in next iteration (parent node). |
677 | * is finished else continue in next iteration (parent node). |
654 | */ |
678 | */ |
655 | if (!gpa->par) break; |
679 | if (!gpa->par) break; |
656 | 680 | ||
657 | if (gpa->par->lft == gpa) { |
681 | if (gpa->par->lft == gpa) { |
658 | dir = AVLTREE_LEFT; |
682 | dir = AVLTREE_LEFT; |
659 | } else { |
683 | } else { |
660 | dir = AVLTREE_RIGHT; |
684 | dir = AVLTREE_RIGHT; |
661 | } |
685 | } |
662 | gpa = gpa->par; |
686 | gpa = gpa->par; |
663 | } |
687 | } |
664 | } |
688 | } |
665 | } |
689 | } |
666 | } |
690 | } |
667 | 691 | ||
668 | 692 | ||
669 | /** Delete node from AVL tree with the smallest key and set base of tree to that key. |
693 | /** Delete node from AVL tree with the smallest key and set base of tree to that key. |
670 | * |
694 | * |
671 | * @param t AVL tree structure. |
695 | * @param t AVL tree structure. |
672 | */ |
696 | */ |
673 | avltree_node_t *avltree_delete_min(avltree_t *t) |
697 | bool avltree_delete_min(avltree_t *t) |
674 | { |
698 | { |
675 | avltree_node_t *node; |
699 | avltree_node_t *node; |
676 | uint64_t key; |
700 | uint64_t key; |
677 | 701 | ||
678 | /* |
702 | /* |
679 | * Start search of smallest key in tree in the root node and |
703 | * Start search of smallest key in tree in the root node and |
680 | * continue in cycle to the most left node in the tree which |
704 | * continue in cycle to the most left node in the tree which |
681 | * must have smallest key. |
705 | * must have smallest key. |
682 | */ |
706 | */ |
683 | node = t->root; |
707 | node = t->root; |
684 | if (!node) return false; |
708 | if (!node) return false; |
685 | 709 | ||
686 | while (node->lft != NULL) |
710 | while (node->lft != NULL) |
687 | node = node->lft; |
711 | node = node->lft; |
688 | 712 | ||
689 | /* |
713 | /* |
690 | * Store key and use avltree_delete function to delete node from tree. |
714 | * Store key and use avltree_delete function to delete node from tree. |
691 | */ |
715 | */ |
692 | key = node->key; |
716 | key = node->key; |
693 | avltree_delete(t,node); |
717 | avltree_delete(t,node); |
694 | 718 | ||
695 | /* |
719 | /* |
696 | * Change base to the key of deleted node. |
720 | * Change base to the key of deleted node. |
697 | */ |
721 | */ |
698 | t->base = key; |
722 | t->base = key; |
699 | 723 | ||
700 | return true; |
724 | return true; |
701 | } |
725 | } |
702 | 726 |