diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-10-04 14:36:07 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-10-04 14:36:07 +0200 |
| commit | 6fb072f62c2f50118dd5cb377d10c76ece51e5fb (patch) | |
| tree | d4572ba16b2ab45a7601b70cdb5811023ac3b390 /avl.c | |
| parent | ace33ff2c18c732e7ac30c49d95f7c35e06d7cee (diff) | |
| download | data-structures-6fb072f62c2f50118dd5cb377d10c76ece51e5fb.tar.gz data-structures-6fb072f62c2f50118dd5cb377d10c76ece51e5fb.tar.bz2 data-structures-6fb072f62c2f50118dd5cb377d10c76ece51e5fb.zip | |
Setze srcut-Marker...
Diffstat (limited to 'avl.c')
| -rw-r--r-- | avl.c | 25 |
1 files changed, 21 insertions, 4 deletions
| @@ -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 */ | ||
| 13 | typedef int T; | 16 | typedef int T; |
| 14 | 17 | ||
| 15 | struct tree_node { | 18 | struct 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 */ | ||
| 23 | static struct tree_node * | 28 | static struct tree_node * |
| 24 | insert_r(T x, struct tree_node *p, bool *h) | 29 | insert_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 */ | ||
| 113 | struct tree_node * | 120 | struct tree_node * |
| 114 | insert(struct tree_node *tree, T data) | 121 | insert(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 */ | ||
| 120 | static struct tree_node * | 129 | static struct tree_node * |
| 121 | balanceL(struct tree_node *p, bool *h) | 130 | balanceL(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 */ | ||
| 161 | static struct tree_node * | 172 | static struct tree_node * |
| 162 | balanceR(struct tree_node *p, bool *h) | 173 | balanceR(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 */ | ||
| 202 | static void | 215 | static void |
| 203 | del(struct tree_node **q, struct tree_node **r, bool *h) | 216 | del(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 */ | ||
| 221 | static struct tree_node * | 236 | static struct tree_node * |
| 222 | delete_r(T x, struct tree_node *p, bool *h) | 237 | delete_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 */ | ||
| 257 | struct tree_node * | 274 | struct tree_node * |
| 258 | delete(struct tree_node *tree, T data) | 275 | delete(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 |
| 266 | struct trunk { | 283 | struct trunk { |
