Subversion Repositories HelenOS

Rev

Rev 2496 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2496 Rev 2497
1
/*
1
/*
2
 * Copyright (c) 2007 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 a binary search tree with non-unique keys.
40
 * @li It is a binary search tree with non-unique keys.
41
 * @li Difference of heights of the left and the right subtree of every node is
41
 * @li Difference of heights of the left and the right subtree of every node is
42
 *     one at maximum.
42
 *     one at maximum.
43
 *
43
 *
44
 * Every node has a pointer to its parent which allows insertion of multiple
44
 * Every node has a pointer to its parent which allows insertion of multiple
45
 * identical keys into the tree.
45
 * identical keys into the tree.
46
 *
46
 *
47
 * Be careful when using this tree because of the base atribute which is added
47
 * Be careful when using this tree because of the base atribute which is added
48
 * to every inserted node key. There is no rule in which order nodes with the
48
 * to every inserted node key. There is no rule in which order nodes with the
49
 * same key are visited.
49
 * same key are visited.
50
 */
50
 */
51
 
51
 
52
#include <adt/avl.h>
52
#include <adt/avl.h>
53
#include <debug.h>
53
#include <debug.h>
54
 
54
 
55
 
55
 
56
#define LEFT    0
56
#define LEFT    0
57
#define RIGHT   1
57
#define RIGHT   1
58
 
58
 
59
 
59
 
60
/** Search for the first occurence of the given key in an AVL tree.
60
/** Search for the first occurence of the given key in an AVL tree.
61
 *
61
 *
62
 * @param t AVL tree.
62
 * @param t AVL tree.
63
 * @param key Key to be searched.
63
 * @param key Key to be searched.
64
 *
64
 *
65
 * @return Pointer to a node or NULL if there is no such key.
65
 * @return Pointer to a node or NULL if there is no such key.
66
 */
66
 */
67
avltree_node_t *avltree_search(avltree_t *t, uint64_t key)
67
avltree_node_t *avltree_search(avltree_t *t, uint64_t key)
68
{
68
{
69
    avltree_node_t *p;
69
    avltree_node_t *p;
70
   
70
   
71
    /*
71
    /*
72
     * Iteratively descend to the leaf that can contain the searched key.
72
     * Iteratively descend to the leaf that can contain the searched key.
73
     */
73
     */
74
    p = t->root;
74
    p = t->root;
75
    while (p != NULL) {
75
    while (p != NULL) {
76
        if (p->key > key)
76
        if (p->key > key)
77
            p = p->lft;
77
            p = p->lft;
78
        else if (p->key < key)
78
        else if (p->key < key)
79
            p = p->rgt;
79
            p = p->rgt;
80
        else
80
        else
81
            return p;
81
            return p;
82
    }
82
    }
83
    return NULL;
83
    return NULL;
84
}
84
}
85
 
85
 
86
 
86
 
87
/** Find the node with the smallest key in an AVL tree.
87
/** Find the node with the smallest key in an AVL tree.
88
 *
88
 *
89
 * @param t AVL tree.
89
 * @param t AVL tree.
90
 *
90
 *
91
 * @return Pointer to a node or NULL if there is no node in the tree.
91
 * @return Pointer to a node or NULL if there is no node in the tree.
92
 */
92
 */
93
avltree_node_t *avltree_find_min(avltree_t *t)
93
avltree_node_t *avltree_find_min(avltree_t *t)
94
{
94
{
95
    avltree_node_t *p = t->root;
95
    avltree_node_t *p = t->root;
96
   
96
   
97
    /*
97
    /*
98
     * Check whether the tree is empty.
98
     * Check whether the tree is empty.
99
     */
99
     */
100
    if (!p)
100
    if (!p)
101
        return NULL;
101
        return NULL;
102
   
102
   
103
    /*
103
    /*
104
     * Iteratively descend to the leftmost leaf in the tree.
104
     * Iteratively descend to the leftmost leaf in the tree.
105
     */
105
     */
106
    while (p->lft != NULL)
106
    while (p->lft != NULL)
107
        p = p->lft;
107
        p = p->lft;
108
   
108
   
109
    return p;
109
    return p;
110
}
110
}
111
 
111
 
112
/** Insert new node into AVL tree.
112
/** Insert new node into AVL tree.
113
 *
113
 *
114
 * @param t AVL tree.
114
 * @param t AVL tree.
115
 * @param newnode New node to be inserted.
115
 * @param newnode New node to be inserted.
116
 */
116
 */
117
void avltree_insert(avltree_t *t, avltree_node_t *newnode)
117
void avltree_insert(avltree_t *t, avltree_node_t *newnode)
118
{  
118
{  
119
    avltree_node_t *par;
119
    avltree_node_t *par;
120
    avltree_node_t *gpa;
120
    avltree_node_t *gpa;
121
    avltree_node_t *top;
121
    avltree_node_t *top;
122
    avltree_node_t **dpc;
122
    avltree_node_t **dpc;
123
    uint64_t key;
123
    uint64_t key;
124
 
124
 
125
    ASSERT(t);
125
    ASSERT(t);
126
    ASSERT(newnode);
126
    ASSERT(newnode);
127
 
127
 
128
    /*
128
    /*
129
     * Creating absolute key.
129
     * Creating absolute key.
130
     */
130
     */
131
    key = newnode->key + t->base;
131
    key = newnode->key + t->base;
132
   
132
   
133
    /*
133
    /*
134
     * Iteratively descend to the leaf that can contain the new node.
134
     * Iteratively descend to the leaf that can contain the new node.
135
     * 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 -
136
     * it is a place of possible inbalance.
136
     * it is a place of possible inbalance.
137
     */
137
     */
138
    dpc = &t->root;
138
    dpc = &t->root;
139
    gpa = NULL;
139
    gpa = NULL;
140
    top = t->root;
140
    top = t->root;
141
    while ((par = (*dpc)) != NULL) {
141
    while ((par = (*dpc)) != NULL) {
142
        if (par->balance != 0) {
142
        if (par->balance != 0) {
143
            top = par;
143
            top = par;
144
        }
144
        }
145
        gpa = par;
145
        gpa = par;
146
        dpc = par->key > key ? &par->lft: &par->rgt;
146
        dpc = par->key > key ? &par->lft: &par->rgt;
147
    }
147
    }
148
 
148
 
149
    /*
149
    /*
150
     * Initialize new node.
150
     * Initialize new node.
151
     */
151
     */
152
    newnode->key = key;
152
    newnode->key = key;
153
    newnode->lft = NULL;
153
    newnode->lft = NULL;
154
    newnode->rgt = NULL;
154
    newnode->rgt = NULL;
155
    newnode->par = gpa;
155
    newnode->par = gpa;
156
    newnode->balance = 0;
156
    newnode->balance = 0;
157
 
157
 
158
    /*
158
    /*
159
     * Insert first node into the empty tree.
159
     * Insert first node into the empty tree.
160
     */
160
     */
161
    if (t->root == NULL) {
161
    if (t->root == NULL) {
162
        *dpc = newnode;
162
        *dpc = newnode;
163
        return;
163
        return;
164
    }
164
    }
165
 
165
 
166
    /*
166
    /*
167
     * Insert new node into previously found leaf place.
167
     * Insert new node into previously found leaf place.
168
     */
168
     */
169
    *dpc = newnode;
169
    *dpc = newnode;
170
 
170
 
171
    /*
171
    /*
172
     * If the tree contains one node - end.
172
     * If the tree contains one node - end.
173
     */
173
     */
174
    if (top == NULL)
174
    if (top == NULL)
175
        return;
175
        return;
176
 
176
 
177
    /*
177
    /*
178
     * Store pointer of top's father which points to the node with
178
     * Store pointer of top's father which points to the node with
179
     * potentially broken balance (top).
179
     * potentially broken balance (top).
180
     */
180
     */
181
    if (top->par == NULL) {
181
    if (top->par == NULL) {
182
        dpc = &t->root;
182
        dpc = &t->root;
183
    } else {
183
    } else {
184
        if (top->par->lft == top)
184
        if (top->par->lft == top)
185
            dpc = &top->par->lft;
185
            dpc = &top->par->lft;
186
        else
186
        else
187
            dpc = &top->par->rgt;
187
            dpc = &top->par->rgt;
188
    }
188
    }
189
 
189
 
190
    /*
190
    /*
191
     * Repair all balances on the way from top node to the newly inserted
191
     * Repair all balances on the way from top node to the newly inserted
192
     * node.
192
     * node.
193
     */
193
     */
194
    par = top;
194
    par = top;
195
    while (par != newnode) {
195
    while (par != newnode) {
196
        if (par->key > key) {
196
        if (par->key > key) {
197
            par->balance--;
197
            par->balance--;
198
            par = par->lft;
198
            par = par->lft;
199
        } else {
199
        } else {
200
            par->balance++;
200
            par->balance++;
201
            par = par->rgt;
201
            par = par->rgt;
202
        }
202
        }
203
    }
203
    }
204
   
204
   
205
    /*
205
    /*
206
     * To balance the tree, we must check and balance top node.
206
     * To balance the tree, we must check and balance top node.
207
     */
207
     */
208
    if (top->balance == -2) {
208
    if (top->balance == -2) {
209
        par = top->lft;
209
        par = top->lft;
210
        if (par->balance == -1) {
210
        if (par->balance == -1) {
211
            /*
211
            /*
212
             * LL rotation.
212
             * LL rotation.
213
             */
213
             */
214
            top->lft = par->rgt;
214
            top->lft = par->rgt;
215
            if (top->lft != NULL)
215
            if (top->lft != NULL)
216
                top->lft->par = top;
216
                top->lft->par = top;
217
            par->par = top->par;
217
            par->par = top->par;
218
            top->par = par;
218
            top->par = par;
219
            par->rgt = top;
219
            par->rgt = top;
-
 
220
            par->balance = 0;
220
            par->balance = top->balance = 0;
221
            top->balance = 0;
221
            *dpc = par;
222
            *dpc = par;
222
        } else {
223
        } else {
223
            /*
224
            /*
224
             * LR rotation.
225
             * LR rotation.
225
             */
226
             */
226
            ASSERT(par->balance == 1);
227
            ASSERT(par->balance == 1);
227
           
228
           
228
            gpa = par->rgt;
229
            gpa = par->rgt;
229
            par->rgt = gpa->lft;
230
            par->rgt = gpa->lft;
230
            if (gpa->lft != NULL)
231
            if (gpa->lft != NULL)
231
                gpa->lft->par = par;
232
                gpa->lft->par = par;
232
            gpa->lft = par;
233
            gpa->lft = par;
233
            par->par = gpa;
234
            par->par = gpa;
234
            top->lft = gpa->rgt;
235
            top->lft = gpa->rgt;
235
            if (gpa->rgt != NULL)
236
            if (gpa->rgt != NULL)
236
                gpa->rgt->par = top;
237
                gpa->rgt->par = top;
237
            gpa->rgt = top;
238
            gpa->rgt = top;
238
            gpa->par = top->par;
239
            gpa->par = top->par;
239
            top->par = gpa;
240
            top->par = gpa;
240
           
241
           
241
            if (gpa->balance == -1) {
242
            if (gpa->balance == -1) {
242
                par->balance = 0;
243
                par->balance = 0;
243
                top->balance = 1;
244
                top->balance = 1;
244
            } else if (gpa->balance == 0) {
245
            } else if (gpa->balance == 0) {
-
 
246
                par->balance = 0;
245
                par->balance = top->balance = 0;
247
                top->balance = 0;
246
            } else {
248
            } else {
247
                par->balance = -1;
249
                par->balance = -1;
248
                top->balance = 0;
250
                top->balance = 0;
249
            }
251
            }
250
            gpa->balance = 0;
252
            gpa->balance = 0;
251
            *dpc = gpa;
253
            *dpc = gpa;
252
        }
254
        }
253
    } else if (top->balance == 2) {
255
    } else if (top->balance == 2) {
254
        par = top->rgt;
256
        par = top->rgt;
255
        if (par->balance == 1) {
257
        if (par->balance == 1) {
256
            /*
258
            /*
257
             * RR rotation.
259
             * RR rotation.
258
             */
260
             */
259
            top->rgt = par->lft;
261
            top->rgt = par->lft;
260
            if (top->rgt != NULL)
262
            if (top->rgt != NULL)
261
                top->rgt->par = top;
263
                top->rgt->par = top;
262
            par->par = top->par;
264
            par->par = top->par;
263
            top->par = par;
265
            top->par = par;
264
            par->lft = top;
266
            par->lft = top;
-
 
267
            par->balance = 0;
265
            par->balance = top->balance = 0;
268
            top->balance = 0;
266
            *dpc = par;
269
            *dpc = par;
267
        } else {
270
        } else {
268
            /*
271
            /*
269
             * RL rotation.
272
             * RL rotation.
270
             */
273
             */
271
            ASSERT(par->balance == -1);
274
            ASSERT(par->balance == -1);
272
           
275
           
273
            gpa = par->lft;
276
            gpa = par->lft;
274
            par->lft = gpa->rgt;
277
            par->lft = gpa->rgt;
275
            if (gpa->rgt != NULL)
278
            if (gpa->rgt != NULL)
276
                gpa->rgt->par = par;
279
                gpa->rgt->par = par;
277
            gpa->rgt = par;
280
            gpa->rgt = par;
278
            par->par = gpa;
281
            par->par = gpa;
279
            top->rgt = gpa->lft;
282
            top->rgt = gpa->lft;
280
            if (gpa->lft != NULL)
283
            if (gpa->lft != NULL)
281
                gpa->lft->par = top;
284
                gpa->lft->par = top;
282
            gpa->lft = top;
285
            gpa->lft = top;
283
            gpa->par = top->par;
286
            gpa->par = top->par;
284
            top->par = gpa;
287
            top->par = gpa;
285
 
288
 
286
            if (gpa->balance == 1) {
289
            if (gpa->balance == 1) {
287
                par->balance = 0;
290
                par->balance = 0;
288
                top->balance = -1;
291
                top->balance = -1;
289
            } else if (gpa->balance == 0) {
292
            } else if (gpa->balance == 0) {
-
 
293
                par->balance = 0;
290
                par->balance = top->balance = 0;
294
                top->balance = 0;
291
            } else {
295
            } else {
292
                par->balance = 1;
296
                par->balance = 1;
293
                top->balance = 0;
297
                top->balance = 0;
294
            }
298
            }
295
            gpa->balance = 0;
299
            gpa->balance = 0;
296
            *dpc = gpa;
300
            *dpc = gpa;
297
        }
301
        }
298
    } else {
302
    } else {
299
        /*
303
        /*
300
         * Balance is not broken, insertion is finised.
304
         * Balance is not broken, insertion is finised.
301
         */
305
         */
302
        return;
306
        return;
303
    }
307
    }
304
 
308
 
305
}
309
}
306
 
310
 
-
 
311
/** Repair the tree after reparenting node u.
-
 
312
 *
-
 
313
 * If node u has no parent, mark it as the root of the whole tree. Otherwise
-
 
314
 * node v represents stale address of one of the children of node u's parent.
-
 
315
 * Replace v with w as node u parent's child (for most uses, u and w will be the
-
 
316
 * same).
-
 
317
 *
-
 
318
 * @param t AVL tree.
-
 
319
 * @param u Node whose new parent has a stale child pointer.
-
 
320
 * @param v Stale child of node u's new parent.
-
 
321
 * @param w New child of node u's new parent.
-
 
322
 * @param dir   If not NULL, address of the variable where to store information
-
 
323
 *      about whether w replaced v in the left or the right subtree of
-
 
324
 *      u's new parent.
-
 
325
 * @param ro    Read only operation; do not modify any tree pointers. This is
-
 
326
 *      useful for tracking direction via the dir pointer.
-
 
327
 *
-
 
328
 * @return  Zero if w became the new root of the tree, otherwise return
-
 
329
 *      non-zero.
-
 
330
 */
-
 
331
static int
-
 
332
repair(avltree_t *t, avltree_node_t *u, avltree_node_t *v, avltree_node_t *w,
-
 
333
    int *dir, int ro)
-
 
334
{
-
 
335
    if (u->par == NULL) {
-
 
336
        if (!ro)
-
 
337
            t->root = w;   
-
 
338
        return 0;
-
 
339
    } else {   
-
 
340
        if (u->par->lft == v) {
-
 
341
            if (!ro)
-
 
342
                u->par->lft = w;
-
 
343
            if (dir)
-
 
344
                *dir = LEFT;
-
 
345
        } else {
-
 
346
            ASSERT(u->par->rgt == v);
-
 
347
            if (!ro)
-
 
348
                u->par->rgt = w;
-
 
349
            if (dir)
-
 
350
                *dir = RIGHT;
-
 
351
        }
-
 
352
    }
-
 
353
    return 1;
-
 
354
}
-
 
355
 
-
 
356
#define REBALANCE(DIR1, DIR2, SIGN)     \
-
 
357
    if (cur->balance == -1 * SIGN) {    \
-
 
358
        par->balance = 0;       \
-
 
359
        gpa->balance = 1 * SIGN;    \
-
 
360
        if (gpa->DIR1)          \
-
 
361
            gpa->DIR1->par = gpa;   \
-
 
362
        par->DIR2->par = par;       \
-
 
363
    } else if (cur->balance == 0) {     \
-
 
364
        par->balance = 0;       \
-
 
365
        gpa->balance = 0;       \
-
 
366
        if (gpa->DIR1)          \
-
 
367
            gpa->DIR1->par = gpa;   \
-
 
368
        if (par->DIR2)          \
-
 
369
            par->DIR2->par = par;   \
-
 
370
    } else {                \
-
 
371
        par->balance = -1 * SIGN;   \
-
 
372
        gpa->balance = 0;       \
-
 
373
        if (par->DIR2)          \
-
 
374
            par->DIR2->par = par;   \
-
 
375
        gpa->DIR1->par = gpa;       \
-
 
376
    }                   \
-
 
377
    cur->balance = 0;
-
 
378
 
-
 
379
#define REBALANCE_LR()      REBALANCE(lft, rgt, 1)
-
 
380
#define REBALANCE_RL()      REBALANCE(rgt, lft, -1)
-
 
381
 
307
/** Delete a node from the AVL tree.
382
/** Delete a node from the AVL tree.
308
 *
383
 *
309
 * Because multiple identical keys are allowed, the parent pointers are
384
 * Because multiple identical keys are allowed, the parent pointers are
310
 * essential during deletion.
385
 * essential during deletion.
311
 *
386
 *
312
 * @param t AVL tree structure.
387
 * @param t AVL tree structure.
313
 * @param node Address of the node which will be deleted.
388
 * @param node Address of the node which will be deleted.
314
 */
389
 */
315
void avltree_delete(avltree_t *t, avltree_node_t *node)
390
void avltree_delete(avltree_t *t, avltree_node_t *node)
316
{
391
{
317
    avltree_node_t *cur;
392
    avltree_node_t *cur;
318
    avltree_node_t *par;
393
    avltree_node_t *par;
319
    avltree_node_t *gpa;
394
    avltree_node_t *gpa;
320
    uint8_t dir;
395
    int dir;
321
 
396
 
322
    ASSERT(t);
397
    ASSERT(t);
323
    ASSERT(node);
398
    ASSERT(node);
324
   
399
   
325
    if (node->lft == NULL) {
400
    if (node->lft == NULL) {
326
        if (node->rgt) {
401
        if (node->rgt) {
327
            /*
402
            /*
328
             * Replace the node with its only right son.
403
             * Replace the node with its only right son.
329
             *
404
             *
330
             * Balance of the right son will be repaired in the
405
             * Balance of the right son will be repaired in the
331
             * balancing cycle.
406
             * balancing cycle.
332
             */
407
             */
333
            cur = node->rgt;
408
            cur = node->rgt;
334
            cur->par = node->par;
409
            cur->par = node->par;
335
            gpa = cur;
410
            gpa = cur;
336
            dir = RIGHT;
411
            dir = RIGHT;
337
            cur->balance = node->balance;
412
            cur->balance = node->balance;
338
        } else {
413
        } else {
339
            if (node->par == NULL) {
414
            if (node->par == NULL) {
340
                /*
415
                /*
341
                 * The tree has only one node - it will become
416
                 * The tree has only one node - it will become
342
                 * an empty tree and the balancing can end.
417
                 * an empty tree and the balancing can end.
343
                 */
418
                 */
344
                t->root = NULL;
419
                t->root = NULL;
345
                return;
420
                return;
346
            }
421
            }
347
            /*
422
            /*
348
             * The node has no child, it will be deleted with no
423
             * The node has no child, it will be deleted with no
349
             * substitution.
424
             * substitution.
350
             */
425
             */
351
            gpa = node->par;
426
            gpa = node->par;
352
            cur = NULL;
427
            cur = NULL;
353
            dir = (gpa->lft == node) ? LEFT: RIGHT;
428
            dir = (gpa->lft == node) ? LEFT: RIGHT;
354
        }
429
        }
355
    } else {
430
    } else {
356
        /*
431
        /*
357
         * The node has left and right son. Find a node with the
432
         * The node has the left son. Find a node with the smallest key
358
         * smallest key in the left subtree and replace the deleted node
433
         * in the left subtree and replace the deleted node with that
359
         * with that node.
434
         * node.
360
         */
435
         */
361
        for (cur = node->lft; cur->rgt != NULL; cur = cur->rgt)
436
        for (cur = node->lft; cur->rgt != NULL; cur = cur->rgt)
362
            ;
437
            ;
363
 
438
 
364
        if (cur != node->lft) {
439
        if (cur != node->lft) {
365
            /*
440
            /*
366
             * The rightmost node of the deleted node's left subtree
441
             * The rightmost node of the deleted node's left subtree
367
             * was found. Replace the deleted node with this node.
442
             * was found. Replace the deleted node with this node.
368
             * Cutting off of the found node has two cases that
443
             * Cutting off of the found node has two cases that
369
             * depend on its left son.
444
             * depend on its left son.
370
             */
445
             */
371
            if (cur->lft) {
446
            if (cur->lft) {
372
                /*
447
                /*
373
                 * The found node has a left son.
448
                 * The found node has a left son.
374
                 */
449
                 */
375
                gpa = cur->lft;
450
                gpa = cur->lft;
376
                gpa->par = cur->par;
451
                gpa->par = cur->par;
377
                dir = LEFT;
452
                dir = LEFT;
378
                gpa->balance = cur->balance;
453
                gpa->balance = cur->balance;
379
            } else {
454
            } else {
380
                dir = RIGHT;
455
                dir = RIGHT;
381
                gpa = cur->par;
456
                gpa = cur->par;
382
            }
457
            }
383
            cur->par->rgt = cur->lft;
458
            cur->par->rgt = cur->lft;
384
            cur->lft = node->lft;
459
            cur->lft = node->lft;
385
            cur->lft->par = cur;
460
            cur->lft->par = cur;
386
        } else {
461
        } else {
387
            /*
462
            /*
388
             * The left son of the node hasn't got a right son. The
463
             * The left son of the node hasn't got a right son. The
389
             * left son will take the deleted node's place.
464
             * left son will take the deleted node's place.
390
             */
465
             */
391
            dir = LEFT;
466
            dir = LEFT;
392
            gpa = cur;
467
            gpa = cur;
393
        }
468
        }
394
        if (node->rgt)
469
        if (node->rgt)
395
            node->rgt->par = cur;
470
            node->rgt->par = cur;
396
        cur->rgt = node->rgt;
471
        cur->rgt = node->rgt;
397
        cur->balance = node->balance;
472
        cur->balance = node->balance;
398
        cur->par = node->par;
473
        cur->par = node->par;
399
    }
474
    }
400
   
475
   
401
    /*
476
    /*
402
     * Repair the parent node's pointer which pointed previously to the
477
     * Repair the parent node's pointer which pointed previously to the
403
     * deleted node.
478
     * deleted node.
404
     */
479
     */
405
    if (node->par == NULL) {
480
    (void) repair(t, node, node, cur, NULL, false);
406
        t->root = cur;
-
 
407
    } else {
-
 
408
        if (node->par->lft == node) {
-
 
409
            node->par->lft = cur;
-
 
410
        } else {
-
 
411
            node->par->rgt = cur;
-
 
412
        }
-
 
413
    }
-
 
414
   
481
   
415
    /*
482
    /*
416
     * Repair cycle which repairs balances of nodes on the way from from the
483
     * Repair cycle which repairs balances of nodes on the way from from the
417
     * cut-off node up to the root.
484
     * cut-off node up to the root.
418
     */
485
     */
419
    for (;;) {
486
    for (;;) {
420
        if (dir == LEFT) {
487
        if (dir == LEFT) {
421
            /*
488
            /*
422
             * Deletion was made in the left subtree.
489
             * Deletion was made in the left subtree.
423
             */
490
             */
424
            gpa->balance++;
491
            gpa->balance++;
425
            if (gpa->balance == 1) {
492
            if (gpa->balance == 1) {
426
                /*
493
                /*
427
                 * Stop balancing, the tree is balanced.
494
                 * Stop balancing, the tree is balanced.
428
                 */
495
                 */
429
                break;
496
                break;
430
            } else if (gpa->balance == 2) {
497
            } else if (gpa->balance == 2) {
431
                /*
498
                /*
432
                 * Bad balance, heights of left and right
499
                 * Bad balance, heights of left and right
433
                 * subtrees differ more than by one.
500
                 * subtrees differ more than by one.
434
                 */
501
                 */
435
                par = gpa->rgt;
502
                par = gpa->rgt;
436
 
503
 
437
                if (par->balance == -1) {
504
                if (par->balance == -1) {
438
                    /*
505
                    /*
439
                     * RL rotation.
506
                     * RL rotation.
440
                     */
507
                     */
441
                   
508
                   
442
                    cur = par->lft;
509
                    cur = par->lft;
443
                    par->lft = cur->rgt;
510
                    par->lft = cur->rgt;
444
                    cur->rgt = par;
511
                    cur->rgt = par;
445
                    gpa->rgt = cur->lft;
512
                    gpa->rgt = cur->lft;
446
                    cur->lft = gpa;
513
                    cur->lft = gpa;
447
                   
514
                   
448
                    /*
515
                    /*
449
                     * Repair balances and paternity of
516
                     * Repair balances and paternity of
450
                     * children, depending on the balance
517
                     * children, depending on the balance
451
                     * factor of the grand child (cur).
518
                     * factor of the grand child (cur).
452
                     */
519
                     */
453
                    if (cur->balance == 1) {
-
 
454
                        par->balance = 0;
-
 
455
                        gpa->balance = -1;
-
 
456
                        if (gpa->rgt)
520
                    REBALANCE_RL();
457
                            gpa->rgt->par = gpa;
-
 
458
                        par->lft->par = par;
-
 
459
                    } else if (cur->balance == 0) {
-
 
460
                        par->balance = gpa->balance = 0;
-
 
461
                        if (gpa->rgt)
-
 
462
                            gpa->rgt->par = gpa;
-
 
463
                        if (par->lft)
-
 
464
                            par->lft->par = par;
-
 
465
                    } else {
-
 
466
                        par->balance = 1;
-
 
467
                        gpa->balance = 0;
-
 
468
                        if (par->lft)
-
 
469
                            par->lft->par = par;
-
 
470
                        gpa->rgt->par = gpa;
-
 
471
                    }
-
 
472
                    cur->balance = 0;
-
 
473
                   
521
                   
474
                    /*
522
                    /*
475
                     * Repair paternity.
523
                     * Repair paternity.
476
                     */
524
                     */
477
                    cur->par = gpa->par;
525
                    cur->par = gpa->par;
478
                    gpa->par = cur;
526
                    gpa->par = cur;
479
                    par->par = cur;
527
                    par->par = cur;
480
 
528
 
481
                    /*
-
 
482
                     * Repair the pointer which pointed to
529
                    if (!repair(t, cur, gpa, cur, &dir,
483
                     * the balanced node. If it was root
-
 
484
                     * then the balancing is finished.
-
 
485
                     * Otherwise continue with the next
-
 
486
                     * iteration (parent node).
-
 
487
                     */
530
                        false))
488
                    if (cur->par == NULL) {
-
 
489
                        t->root = cur; 
-
 
490
                        break;
531
                        break;
491
                    } else {
-
 
492
                        if (cur->par->lft == gpa) {
-
 
493
                            cur->par->lft = cur;
-
 
494
                            dir = LEFT;
-
 
495
                        } else {
-
 
496
                            cur->par->rgt = cur;
-
 
497
                            dir = RIGHT;
-
 
498
                        }
-
 
499
                    }
-
 
500
                    gpa = cur->par;
532
                    gpa = cur->par;
501
                } else {
533
                } else {
502
                    /*
534
                    /*
503
                     * RR rotation.
535
                     * RR rotation.
504
                     */
536
                     */
505
                   
537
                   
506
                    gpa->rgt = par->lft;
538
                    gpa->rgt = par->lft;
507
                    if (par->lft)
539
                    if (par->lft)
508
                        par->lft->par = gpa;
540
                        par->lft->par = gpa;
509
                    par->lft = gpa;
541
                    par->lft = gpa;
510
                   
542
                   
511
                    /*
543
                    /*
512
                     * Repair paternity.
544
                     * Repair paternity.
513
                     */
545
                     */
514
                    par->par = gpa->par;
546
                    par->par = gpa->par;
515
                    gpa->par = par;
547
                    gpa->par = par;
516
                   
548
                   
517
                    if (par->balance == 0) {
549
                    if (par->balance == 0) {
518
                        /*
550
                        /*
519
                         * The right child of the
551
                         * The right child of the
520
                         * balanced node is balanced,
552
                         * balanced node is balanced,
521
                         * after RR rotation is done,
553
                         * after RR rotation is done,
522
                         * the whole tree will be
554
                         * the whole tree will be
523
                         * balanced.
555
                         * balanced.
524
                         */
556
                         */
525
                        par->balance = -1;
557
                        par->balance = -1;
526
                        gpa->balance = 1;
558
                        gpa->balance = 1;
527
 
559
 
528
                        /*
-
 
529
                         * Repair the pointer which
-
 
530
                         * pointed to the balanced node
-
 
531
                         * and finish balancing.
-
 
532
                         */
-
 
533
                        if (par->par == NULL) {
-
 
534
                            t->root = par; 
-
 
535
                        } else {
-
 
536
                            if (par->par->lft ==
560
                        (void) repair(t, par, gpa, par,
537
                                gpa) {
-
 
538
                                par->par->lft =
-
 
539
                                    par;
-
 
540
                            } else {
-
 
541
                                par->par->rgt =
-
 
542
                                    par;
561
                            NULL, false);
543
                            }
-
 
544
                        }
-
 
545
                        break;
562
                        break;
546
                    } else {
563
                    } else {
547
                        par->balance = gpa->balance = 0;
564
                        par->balance = 0;
548
                        /*
-
 
549
                         * Repair the pointer which
-
 
550
                         * pointed to the balanced node.
565
                        gpa->balance = 0;
551
                         * If it was root then balancing
-
 
552
                         * is finished. Otherwise
-
 
553
                         * continue with the next
-
 
554
                         * iteration (parent node).
-
 
555
                         */
-
 
556
                        if (par->par == NULL) {
566
                        if (!repair(t, par, gpa, par,
557
                            t->root = par; 
567
                            &dir, false))
558
                            break;
568
                            break;
559
                        } else {   
-
 
560
                            if (par->par->lft ==
-
 
561
                                gpa) {
-
 
562
                                par->par->lft =
-
 
563
                                    par;
-
 
564
                                dir = LEFT;
-
 
565
                            } else {
-
 
566
                                par->par->rgt =
-
 
567
                                    par;
-
 
568
                                dir = RIGHT;
-
 
569
                            }
-
 
570
                        }
-
 
571
                    }
569
                    }
572
                    gpa = par->par;
570
                    gpa = par->par;
573
                }
571
                }
574
            } else {
572
            } else {
575
                /*
573
                /*
576
                 * Repair the pointer which pointed to the
574
                 * Repair the pointer which pointed to the
577
                 * balanced node. If it was root then balancing
575
                 * balanced node. If it was root then balancing
578
                 * is finished else continue with the next
576
                 * is finished else continue with the next
579
                 * iteration (parent node).
577
                 * iteration (parent node).
580
                 */
578
                 */
581
                if (!gpa->par)
579
                if (!repair(t, gpa, gpa, NULL, &dir, true))
582
                    break;
580
                    break;
583
 
-
 
584
                if (gpa->par->lft == gpa) {
-
 
585
                    dir = LEFT;
-
 
586
                } else {
-
 
587
                    dir = RIGHT;
-
 
588
                }
-
 
589
                gpa = gpa->par;
581
                gpa = gpa->par;
590
            }
582
            }
591
        } else {
583
        } else {
592
            /*
584
            /*
593
             * Deletion was made in the right subtree.
585
             * Deletion was made in the right subtree.
594
             */
586
             */
595
            gpa->balance--;
587
            gpa->balance--;
596
            if (gpa->balance == -1) {
588
            if (gpa->balance == -1) {
597
                /*
589
                /*
598
                 * Stop balancing, the tree is balanced.
590
                 * Stop balancing, the tree is balanced.
599
                 */
591
                 */
600
                break;
592
                break;
601
            } else if (gpa->balance == -2) {
593
            } else if (gpa->balance == -2) {
602
                /*
594
                /*
603
                 * Bad balance, heights of left and right
595
                 * Bad balance, heights of left and right
604
                 * subtrees differ more than by one.
596
                 * subtrees differ more than by one.
605
                 */
597
                 */
606
                par = gpa->lft;
598
                par = gpa->lft;
607
               
599
               
608
                if (par->balance == 1) {
600
                if (par->balance == 1) {
609
                    /*
601
                    /*
610
                     * LR rotation.
602
                     * LR rotation.
611
                     */
603
                     */
612
                   
604
                   
613
                    cur = par->rgt;
605
                    cur = par->rgt;
614
                    par->rgt = cur->lft;
606
                    par->rgt = cur->lft;
615
                    cur->lft = par;
607
                    cur->lft = par;
616
                    gpa->lft = cur->rgt;
608
                    gpa->lft = cur->rgt;
617
                    cur->rgt = gpa;
609
                    cur->rgt = gpa;
618
                   
610
                   
619
                    /*
611
                    /*
620
                     * Repair balances and paternity of
612
                     * Repair balances and paternity of
621
                     * children, depending on the balance
613
                     * children, depending on the balance
622
                     * factor of the grand child (cur).
614
                     * factor of the grand child (cur).
623
                     */
615
                     */
624
                    if (cur->balance == -1) {
-
 
625
                        par->balance = 0;
-
 
626
                        gpa->balance = 1;
-
 
627
                        if (gpa->lft)
616
                    REBALANCE_LR();
628
                            gpa->lft->par = gpa;
-
 
629
                        par->rgt->par = par;
-
 
630
                    } else if (cur->balance == 0) {
-
 
631
                        par->balance = gpa->balance = 0;
-
 
632
                        if (gpa->lft)
-
 
633
                            gpa->lft->par = gpa;
-
 
634
                        if (par->rgt)
-
 
635
                            par->rgt->par = par;
-
 
636
                    } else {
-
 
637
                        par->balance = -1;
-
 
638
                        gpa->balance = 0;
-
 
639
                        if (par->rgt)
-
 
640
                            par->rgt->par = par;
-
 
641
                        gpa->lft->par = gpa;
-
 
642
                    }
-
 
643
                    cur->balance = 0;
-
 
644
 
617
 
645
                    /*
618
                    /*
646
                     * Repair paternity.
619
                     * Repair paternity.
647
                     */
620
                     */
648
                    cur->par = gpa->par;
621
                    cur->par = gpa->par;
649
                    gpa->par = cur;
622
                    gpa->par = cur;
650
                    par->par = cur;
623
                    par->par = cur;
651
 
624
 
652
                    /*
-
 
653
                     * Repair the pointer which pointed to
625
                    if (!repair(t, cur, gpa, cur, &dir,
654
                     * the balanced node. If it was root
-
 
655
                     * then balancing is finished. Otherwise
-
 
656
                     * continue with the next iteration
-
 
657
                     * (parent node).
626
                        false))
658
                     */
-
 
659
                    if (cur->par == NULL) {
-
 
660
                        t->root = cur; 
-
 
661
                        break;
627
                        break;
662
                    } else {
-
 
663
                        if (cur->par->lft == gpa) {
-
 
664
                            cur->par->lft = cur;
-
 
665
                            dir = LEFT;
-
 
666
                        } else {
-
 
667
                            cur->par->rgt = cur;
-
 
668
                            dir = RIGHT;
-
 
669
                        }
-
 
670
                    }
-
 
671
                    gpa = cur->par;
628
                    gpa = cur->par;
672
                } else {
629
                } else {
673
                    /*
630
                    /*
674
                     * LL rotation.
631
                     * LL rotation.
675
                     */
632
                     */
676
                   
633
 
677
                    gpa->lft = par->rgt;
634
                    gpa->lft = par->rgt;
678
                    if (par->rgt)
635
                    if (par->rgt)
679
                        par->rgt->par = gpa;
636
                        par->rgt->par = gpa;
680
                    par->rgt = gpa;
637
                    par->rgt = gpa;
681
                    /*
638
                    /*
682
                     * Repair paternity.
639
                     * Repair paternity.
683
                     */
640
                     */
684
                    par->par = gpa->par;
641
                    par->par = gpa->par;
685
                    gpa->par = par;
642
                    gpa->par = par;
686
                   
643
                   
687
                    if (par->balance == 0) {
644
                    if (par->balance == 0) {
688
                        /*
645
                        /*
689
                         * The left child of the
646
                         * The left child of the
690
                         * balanced node is balanced,
647
                         * balanced node is balanced,
691
                         * after LL rotation is done,
648
                         * after LL rotation is done,
692
                         * the whole tree will be
649
                         * the whole tree will be
693
                         * balanced.
650
                         * balanced.
694
                         */
651
                         */
695
                        par->balance = 1;
652
                        par->balance = 1;
696
                        gpa->balance = -1;
653
                        gpa->balance = -1;
697
                       
654
                       
698
                        /*
-
 
699
                         * Repair the pointer which
-
 
700
                         * pointed to the balanced node
-
 
701
                         * and finish balancing.
-
 
702
                         */
-
 
703
                        if (par->par == NULL) {
-
 
704
                            t->root = par;
-
 
705
                        } else {
-
 
706
                            if (par->par->lft ==
655
                        (void) repair(t, par, gpa, par,
707
                                gpa) {
-
 
708
                                par->par->lft =
-
 
709
                                    par;
-
 
710
                            } else {
-
 
711
                                par->par->rgt =
-
 
712
                                    par;
656
                            NULL, false);
713
                            }
-
 
714
                        }
-
 
715
                        break;
657
                        break;
716
                    } else {
658
                    } else {
-
 
659
                        par->balance = 0;
717
                        par->balance = gpa->balance = 0;
660
                        gpa->balance = 0;
718
                       
661
                       
719
                        /*
-
 
720
                         * Repair the pointer which
-
 
721
                         * pointed to the balanced node.
-
 
722
                         * If it was root then balancing
-
 
723
                         * is finished. Otherwise
-
 
724
                         * continue with the next
-
 
725
                         * iteration (parent node).
-
 
726
                         */
-
 
727
                        if (par->par == NULL) {
662
                        if (!repair(t, par, gpa, par,
728
                            t->root = par;
663
                            &dir, false))
729
                            break;
664
                            break;
730
                        } else {
-
 
731
                            if (par->par->lft ==
-
 
732
                                gpa) {
-
 
733
                                par->par->lft =
-
 
734
                                    par;
-
 
735
                                dir = LEFT;
-
 
736
                            } else {
-
 
737
                                par->par->rgt =
-
 
738
                                    par;
-
 
739
                                dir = RIGHT;
-
 
740
                            }
-
 
741
                        }
-
 
742
                    }
665
                    }
743
                    gpa = par->par;
666
                    gpa = par->par;
744
                }
667
                }
745
            } else {
668
            } else {
746
                /*
669
                /*
747
                 * Repair the pointer which pointed to the
670
                 * Repair the pointer which pointed to the
748
                 * balanced node. If it was root then balancing
671
                 * balanced node. If it was root then balancing
749
                 * is finished. Otherwise continue with the next
672
                 * is finished. Otherwise continue with the next
750
                 * iteration (parent node).
673
                 * iteration (parent node).
751
                 */
674
                 */
752
                if (!gpa->par)
675
                if (!repair(t, gpa, gpa, NULL, &dir, true))
753
                    break;
676
                    break;
754
 
-
 
755
                if (gpa->par->lft == gpa) {
-
 
756
                    dir = LEFT;
-
 
757
                } else {
-
 
758
                    dir = RIGHT;
-
 
759
                }
-
 
760
                gpa = gpa->par;
677
                gpa = gpa->par;
761
            }
678
            }
762
        }
679
        }
763
    }
680
    }
764
}
681
}
765
 
682
 
766
 
683
 
767
/** Delete a node with the smallest key from the AVL tree.
684
/** Delete a node with the smallest key from the AVL tree.
768
 *
685
 *
769
 * @param t AVL tree structure.
686
 * @param t AVL tree structure.
770
 */
687
 */
771
bool avltree_delete_min(avltree_t *t)
688
bool avltree_delete_min(avltree_t *t)
772
{
689
{
773
    avltree_node_t *node;
690
    avltree_node_t *node;
774
   
691
   
775
    /*
692
    /*
776
     * Start searching for the smallest key in the tree starting in the root
693
     * Start searching for the smallest key in the tree starting in the root
777
     * node and continue in cycle to the leftmost node in the tree (which
694
     * node and continue in cycle to the leftmost node in the tree (which
778
     * must have the smallest key).
695
     * must have the smallest key).
779
     */
696
     */
780
     
697
     
781
    node = t->root;
698
    node = t->root;
782
    if (!node)
699
    if (!node)
783
        return false;
700
        return false;
784
   
701
   
785
    while (node->lft != NULL)
702
    while (node->lft != NULL)
786
        node = node->lft;
703
        node = node->lft;
787
   
704
   
788
    avltree_delete(t, node);
705
    avltree_delete(t, node);
789
 
706
 
790
    return true;
707
    return true;
791
}
708
}
792
 
709
 
793
/** @}
710
/** @}
794
 */
711
 */
-
 
712
 
795
 
713