aboutsummaryrefslogtreecommitdiff
path: root/avl.c
diff options
context:
space:
mode:
Diffstat (limited to 'avl.c')
-rw-r--r--avl.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/avl.c b/avl.c
index 201dc5b..13a3d5f 100644
--- a/avl.c
+++ b/avl.c
@@ -1,15 +1,18 @@
1// AVL Tree 1// AVL Tree
2#include <stdbool.h>
2#include <stdio.h> 3#include <stdio.h>
3#include <stdlib.h> 4#include <stdlib.h>
4#include <stdbool.h>
5#include <time.h> 5#include <time.h>
6 6
7/* utils */ 7/* utils */
8#include "util.h" 8#include "util.h"
9 9
10// TODO: [x] Review: http://www.inr.ac.ru/~info21/ADen/ 10// clang-format off
11// [x] Tests: https://stackoverflow.com/q/3955680
12 11
12// Review: http://www.inr.ac.ru/~info21/ADen/
13// Tests: https://stackoverflow.com/q/3955680
14
15/* --8<-- avl_type */
13typedef int T; 16typedef int T;
14 17
15struct tree_node { 18struct tree_node {
@@ -19,7 +22,9 @@ struct tree_node {
19 int count; /* collision counter */ 22 int count; /* collision counter */
20 /* ggf. weitere Felder... */ 23 /* ggf. weitere Felder... */
21}; 24};
25/* -->8-- */
22 26
27/* --8<-- avl_insert_r */
23static struct tree_node * 28static struct tree_node *
24insert_r(T x, struct tree_node *p, bool *h) 29insert_r(T x, struct tree_node *p, bool *h)
25{ 30{
@@ -109,14 +114,18 @@ insert_r(T x, struct tree_node *p, bool *h)
109 ERROR("das hier sollte niemals passieren"); 114 ERROR("das hier sollte niemals passieren");
110 return p; 115 return p;
111} 116}
117/* -->8-- */
112 118
119/* --8<-- avl_insert */
113struct tree_node * 120struct tree_node *
114insert(struct tree_node *tree, T data) 121insert(struct tree_node *tree, T data)
115{ 122{
116 bool h = false; 123 bool h = false;
117 return insert_r(data, tree, &h); 124 return insert_r(data, tree, &h);
118} 125}
126/* -->8-- */
119 127
128/* --8<-- avl_balanceL */
120static struct tree_node * 129static struct tree_node *
121balanceL(struct tree_node *p, bool *h) 130balanceL(struct tree_node *p, bool *h)
122{ 131{
@@ -157,7 +166,9 @@ balanceL(struct tree_node *p, bool *h)
157 } 166 }
158 return p; 167 return p;
159} 168}
169/* -->8-- */
160 170
171/* --8<-- avl_balanceR */
161static struct tree_node * 172static struct tree_node *
162balanceR(struct tree_node *p, bool *h) 173balanceR(struct tree_node *p, bool *h)
163{ 174{
@@ -198,7 +209,9 @@ balanceR(struct tree_node *p, bool *h)
198 } 209 }
199 return p; 210 return p;
200} 211}
212/* -->8-- */
201 213
214/* --8<-- avl_del */
202static void 215static void
203del(struct tree_node **q, struct tree_node **r, bool *h) 216del(struct tree_node **q, struct tree_node **r, bool *h)
204{ 217{
@@ -217,7 +230,9 @@ del(struct tree_node **q, struct tree_node **r, bool *h)
217 *h = true; 230 *h = true;
218 } 231 }
219} 232}
233/* -->8-- */
220 234
235/* --8<-- avl_delete_r */
221static struct tree_node * 236static struct tree_node *
222delete_r(T x, struct tree_node *p, bool *h) 237delete_r(T x, struct tree_node *p, bool *h)
223{ 238{
@@ -253,14 +268,16 @@ delete_r(T x, struct tree_node *p, bool *h)
253 } 268 }
254 return p; 269 return p;
255} 270}
271/* -->8-- */
256 272
273/* --8<-- avl_delete */
257struct tree_node * 274struct tree_node *
258delete(struct tree_node *tree, T data) 275delete(struct tree_node *tree, T data)
259{ 276{
260 bool h = false; 277 bool h = false;
261 return delete_r(data, tree, &h); 278 return delete_r(data, tree, &h);
262} 279}
263 280/* -->8-- */
264 281
265// aux display and verification routines, helpful but not essential 282// aux display and verification routines, helpful but not essential
266struct trunk { 283struct trunk {