Sun, 17 May 2015 18:32:41 +0200
finalized AVL tree interface + added implementation skeleton + fixed ucx_ptrcmp()
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2015 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
30 /**
31 * @file avl.h
32 *
33 * AVL tree implementation.
34 *
35 * This binary search tree implementation allows average O(1) insertion and
36 * removal of elements.
37 *
38 * @author Mike Becker
39 * @author Olaf Wintermann
40 */
42 #ifndef UCX_AVL_H
43 #define UCX_AVL_H
45 #include "ucx.h"
46 #include "allocator.h"
47 #include <stdint.h>
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
53 /**
54 * UCX AVL Node type.
55 *
56 * @see UcxAVLNode
57 */
58 typedef struct UcxAVLNode UcxAVLNode;
60 /**
61 * UCX AVL Node.
62 */
63 struct UcxAVLNode {
64 /**
65 * The key for this node.
66 */
67 intptr_t key;
68 /**
69 * Data contained by this node.
70 */
71 void *value;
72 /**
73 * The height of this (sub)-tree.
74 */
75 size_t height;
76 /**
77 * Parent node.
78 */
79 UcxAVLNode *parent;
80 /**
81 * Root node of left subtree.
82 */
83 UcxAVLNode *left;
84 /**
85 * Root node of right subtree.
86 */
87 UcxAVLNode *right;
88 };
90 /**
91 * UCX AVL Tree.
92 */
93 typedef struct {
94 /**
95 * The UcxAllocator that shall be used to manage the memory for node data.
96 */
97 UcxAllocator *allocator;
98 /**
99 * Root node of the tree.
100 */
101 UcxAVLNode *root;
102 /**
103 * Compare function that shall be used to compare the UcxAVLNode keys.
104 * @see UcxAVLNode.key
105 */
106 cmp_func cmpfunc;
107 /**
108 * Custom user data.
109 * This data will also be provided to the cmpfunc.
110 */
111 void *userdata;
112 } UcxAVLTree;
114 /**
115 * Initializes a new UcxAVLTree with a default allocator.
116 *
117 * @param cmpfunc the compare function that shall be used
118 * @return a new UcxAVLTree object
119 * @see ucx_avl_new_a()
120 */
121 UcxAVLTree *ucx_avl_new(cmp_func cmpfunc);
123 /**
124 * Initializes a new UcxAVLTree with the specified allocator.
125 *
126 * The cmpfunc should be capable of comparing two keys within this AVL tree.
127 * So if you want to use null terminated strings as keys, you could use the
128 * ucx_strcmp() function here.
129 *
130 * @param cmpfunc the compare function that shall be used
131 * @param allocator the UcxAllocator that shall be used
132 * @return a new UcxAVLTree object
133 */
134 UcxAVLTree *ucx_avl_new_a(cmp_func cmpfunc, UcxAllocator *allocator);
136 /**
137 * Macro for initializing a new UcxAVLTree with the default allocator and a
138 * ucx_ptrcmp() compare function.
139 *
140 * @return a new default UcxAVLTree object
141 */
142 #define ucx_avl_default_new() ucx_avl_new_a(ucx_ptrcmp, ucx_default_allocator())
144 /**
145 * Gets the value from the tree, that is associated with the specified key.
146 * @param tree the UcxAVLTree
147 * @param key the key
148 * @return the value (or <code>NULL</code>, if the key is not present)
149 */
150 void *ucx_avl_get(UcxAVLTree *tree, intptr_t key);
152 /**
153 * Puts a key/value pair into the tree.
154 * @param tree the UcxAVLTree
155 * @param key the key
156 * @param value the new value
157 * @return the replaced value (or <code>NULL</code>, if the key is new to the
158 * tree)
159 */
160 void* ucx_avl_put(UcxAVLTree *tree, intptr_t key, void *value);
162 /**
163 * Removes an element from the AVL tree.
164 * @param tree the UcxAVLTree
165 * @param key the key
166 * @return the removed value (or <code>NULL</code>, if the key was not present)
167 */
168 void* ucx_avl_remove(UcxAVLTree *tree, intptr_t key);
172 #ifdef __cplusplus
173 }
174 #endif
176 #endif /* UCX_AVL_H */