Mon, 18 May 2015 19:49:03 +0200
added ucx_avl_count
192
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2015 Olaf Wintermann. All rights reserved. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | */ |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | #include "avl.h" |
1e51558b9d09
updated copyright notice + added files for upcoming AVL tree implementation
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
31 | #define ptrcast(ptr) ((void*)(ptr)) |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
32 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
33 | static void ucx_avl_connect(UcxAVLTree *tree, |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
34 | UcxAVLNode *node, UcxAVLNode *child, intptr_t nullkey) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
35 | if (child) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
36 | child->parent = node; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
37 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
38 | // if child is NULL, nullkey decides if left or right pointer is cleared |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
39 | if (tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
40 | ptrcast(child ? child->key : nullkey), |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
41 | ptrcast(node->key), tree->userdata) > 0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
42 | node->right = child; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
43 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
44 | node->left = child; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
45 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
46 | size_t lh = node->left ? node->left->height : 0; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
47 | size_t rh = node->right ? node->right->height : 0; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
48 | node->height = 1 + (lh > rh ? lh : rh); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
49 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
50 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
51 | #define avlheight(node) ((node) ? (node)->height : 0) |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
52 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
53 | static UcxAVLNode* avl_rotright(UcxAVLTree *tree, UcxAVLNode *l0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
54 | UcxAVLNode *p = l0->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
55 | UcxAVLNode *l1 = l0->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
56 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
57 | ucx_avl_connect(tree, p, l1, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
58 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
59 | l1->parent = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
60 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
61 | ucx_avl_connect(tree, l0, l1->right, l1->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
62 | ucx_avl_connect(tree, l1, l0, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
63 | return l1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
64 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
65 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
66 | static UcxAVLNode* avl_rotleft(UcxAVLTree *tree, UcxAVLNode *l0) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
67 | UcxAVLNode *p = l0->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
68 | UcxAVLNode *l1 = l0->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
69 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
70 | ucx_avl_connect(tree, p, l1, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
71 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
72 | l1->parent = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
73 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
74 | ucx_avl_connect(tree, l0, l1->left, l1->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
75 | ucx_avl_connect(tree, l1, l0, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
76 | return l1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
77 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
78 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
79 | static void ucx_avl_balance(UcxAVLTree *tree, UcxAVLNode *n) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
80 | int lh = avlheight(n->left); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
81 | int rh = avlheight(n->right); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
82 | n->height = 1 + (lh > rh ? lh : rh); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
83 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
84 | if (lh - rh == 2) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
85 | UcxAVLNode *c = n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
86 | if (avlheight(c->right) - avlheight(c->left) == 1) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
87 | avl_rotleft(tree, c); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
88 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
89 | n = avl_rotright(tree, n); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
90 | } else if (rh - lh == 2) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
91 | UcxAVLNode *c = n->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
92 | if (avlheight(c->left) - avlheight(c->right) == 1) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
93 | avl_rotright(tree, c); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
94 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
95 | n = avl_rotleft(tree, n); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
96 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
97 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
98 | if (n->parent) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
99 | ucx_avl_balance(tree, n->parent); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
100 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
101 | tree->root = n; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
102 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
103 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
104 | |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
105 | UcxAVLTree *ucx_avl_new(cmp_func cmpfunc) { |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
106 | return ucx_avl_new_a(cmpfunc, ucx_default_allocator()); |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
107 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
108 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
109 | UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator) { |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
110 | UcxAVLTree *tree = almalloc(allocator, sizeof(UcxAVLTree)); |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
111 | if (tree) { |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
112 | tree->allocator = allocator; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
113 | tree->cmpfunc = cmpfunc; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
114 | tree->root = NULL; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
115 | tree->userdata = NULL; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
116 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
117 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
118 | return tree; |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
119 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
120 | |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
121 | static void ucx_avl_free_node(UcxAllocator *al, UcxAVLNode *node) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
122 | if (node) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
123 | ucx_avl_free_node(al, node->left); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
124 | ucx_avl_free_node(al, node->right); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
125 | alfree(al, node); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
126 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
127 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
128 | |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
129 | void ucx_avl_free(UcxAVLTree *tree) { |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
130 | UcxAllocator *al = tree->allocator; |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
131 | ucx_avl_free_node(al, tree->root); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
132 | alfree(al, tree); |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
133 | } |
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
134 | |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
135 | void *ucx_avl_get(UcxAVLTree *tree, intptr_t key) { |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
136 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
137 | int cmpresult; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
138 | while (n && (cmpresult = tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
139 | ptrcast(key), ptrcast(n->key), tree->userdata))) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
140 | n = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
141 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
142 | return n ? n->value : NULL; |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
143 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
144 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
145 | void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value) { |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
146 | if (tree->root) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
147 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
148 | int cmpresult; |
197 | 149 | while ((cmpresult = tree->cmpfunc( |
150 | ptrcast(key), ptrcast(n->key), tree->userdata))) { | |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
151 | UcxAVLNode *m = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
152 | if (m) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
153 | n = m; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
154 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
155 | break; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
156 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
157 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
158 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
159 | if (cmpresult) { |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
160 | UcxAVLNode *e = almalloc(tree->allocator, sizeof(UcxAVLNode)); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
161 | e->key = key; e->value = value; e->height = 1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
162 | e->parent = e->left = e->right = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
163 | ucx_avl_connect(tree, n, e, 0); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
164 | ucx_avl_balance(tree, n); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
165 | return NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
166 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
167 | void *prevval = n->value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
168 | n->value = value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
169 | return prevval; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
170 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
171 | } else { |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
172 | tree->root = almalloc(tree->allocator, sizeof(UcxAVLNode)); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
173 | tree->root->key = key; tree->root->value = value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
174 | tree->root->height = 1; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
175 | tree->root->parent = tree->root->left = tree->root->right = NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
176 | return NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
177 | } |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
178 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
179 | |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
180 | void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key) { |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
181 | UcxAVLNode *n = tree->root; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
182 | int cmpresult; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
183 | while (n && (cmpresult = tree->cmpfunc( |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
184 | ptrcast(key), ptrcast(n->key), tree->userdata))) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
185 | n = cmpresult > 0 ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
186 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
187 | if (n) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
188 | void *prevval = n->value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
189 | UcxAVLNode *p = n->parent; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
190 | if (n->left && n->right) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
191 | UcxAVLNode *s = n->right; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
192 | while (s->left) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
193 | s = s->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
194 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
195 | ucx_avl_connect(tree, s->parent, s->right, s->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
196 | n->key = s->key; n->value = s->value; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
197 | p = s->parent; |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
198 | alfree(tree->allocator, s); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
199 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
200 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
201 | ucx_avl_connect(tree, p, n->right ? n->right:n->left, n->key); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
202 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
203 | tree->root = n->right ? n->right : n->left; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
204 | } |
196
1b4cdafef2eb
added free() to AVL tree implementation + use UcxAllocator
Mike Becker <universe@uap-core.de>
parents:
195
diff
changeset
|
205 | alfree(tree->allocator, n); |
195
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
206 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
207 | |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
208 | if (p) { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
209 | ucx_avl_balance(tree, p); |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
210 | } |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
211 | return prevval; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
212 | } else { |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
213 | return NULL; |
bec61f067ea0
added AVL tree implementation - TODO: free memory + test cases
Mike Becker <universe@uap-core.de>
parents:
194
diff
changeset
|
214 | } |
194
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
215 | } |
0c1b7676e74c
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
Mike Becker <universe@uap-core.de>
parents:
192
diff
changeset
|
216 | |
199 | 217 | static size_t ucx_avl_countn(UcxAVLNode *node) { |
218 | if (node) { | |
219 | return 1 + ucx_avl_countn(node->left) + ucx_avl_countn(node->right); | |
220 | } else { | |
221 | return 0; | |
222 | } | |
223 | } | |
224 | ||
225 | size_t ucx_avl_count(UcxAVLTree *tree) { | |
226 | return ucx_avl_countn(tree->root); | |
227 | } |