Rev 2416 | Rev 2431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2416 | mencl | 1 | /* |
| 2 | * Copyright (c) 2007 Vojtech Mencl |
||
| 3 | * All rights reserved. |
||
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 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 |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 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 |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 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 |
||
| 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 |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 29 | #include <test.h> |
||
| 30 | #include <print.h> |
||
| 31 | #include <adt/avl.h> |
||
| 32 | #include <debug.h> |
||
| 33 | |||
| 34 | #include <panic.h> |
||
| 35 | |||
| 36 | |||
| 37 | #define NODE_COUNT 100 |
||
| 38 | |||
| 39 | /* |
||
| 40 | * wrapper structure with a pointer to avl tree root |
||
| 41 | */ |
||
| 42 | static avltree_t avltree; |
||
| 43 | |||
| 44 | /* |
||
| 45 | * avl tree nodes in array for faster allocating |
||
| 46 | */ |
||
| 47 | static avltree_node_t avltree_nodes[NODE_COUNT]; |
||
| 48 | |||
| 49 | /* |
||
| 50 | * head of free nodes' list: |
||
| 51 | */ |
||
| 52 | static avltree_node_t *first_free_node = NULL; |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | static int test_tree_balance(avltree_node_t *node); |
||
| 2421 | mencl | 57 | static avltree_node_t *test_tree_parents(avltree_node_t *node); |
| 2416 | mencl | 58 | static void print_tree_structure_flat (avltree_node_t *node, int level); |
| 59 | static avltree_node_t *alloc_avltree_node(void); |
||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | static avltree_node_t *test_tree_parents(avltree_node_t *node) |
||
| 64 | { |
||
| 65 | avltree_node_t *tmp; |
||
| 66 | |||
| 67 | if (!node) return NULL; |
||
| 68 | |||
| 69 | if (node->lft) { |
||
| 70 | tmp = test_tree_parents(node->lft); |
||
| 71 | if (tmp != node) { |
||
| 72 | printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->lft); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | if (node->rgt) { |
||
| 76 | tmp = test_tree_parents(node->rgt); |
||
| 77 | if (tmp != node) { |
||
| 78 | printf("Bad parent pointer key: %d, address: %p\n",tmp->key,node->rgt); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | return node->par; |
||
| 82 | } |
||
| 83 | |||
| 84 | int test_tree_balance(avltree_node_t *node) |
||
| 85 | { |
||
| 86 | int h1, h2, diff; |
||
| 87 | |||
| 88 | if (!node) return 0; |
||
| 89 | h1 = test_tree_balance(node->lft); |
||
| 90 | h2 = test_tree_balance(node->rgt); |
||
| 91 | diff = h2 - h1; |
||
| 92 | if (diff != node->balance || (diff != -1 && diff != 0 && diff != 1)) { |
||
| 93 | printf("Bad balance\n"); |
||
| 94 | } |
||
| 95 | return h1 > h2 ? h1 + 1 : h2 + 1; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Prints the structure of node, which is level levels from the top of the tree. |
||
| 100 | */ |
||
| 2421 | mencl | 101 | static void print_tree_structure_flat (avltree_node_t *node, int level) |
| 2416 | mencl | 102 | { |
| 103 | /* You can set the maximum level as high as you like. |
||
| 104 | Most of the time, you'll want to debug code using small trees, |
||
| 105 | so that a large level indicates a loop, which is a bug. */ |
||
| 106 | if (level > 16) |
||
| 107 | { |
||
| 108 | printf ("[...]"); |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (node == NULL) |
||
| 113 | return; |
||
| 114 | |||
| 115 | printf ("%d[%d]", node->key,node->balance); |
||
| 116 | if (node->lft != NULL || node->rgt != NULL) |
||
| 117 | { |
||
| 2421 | mencl | 118 | printf("("); |
| 2416 | mencl | 119 | |
| 120 | print_tree_structure_flat (node->lft, level + 1); |
||
| 121 | if (node->rgt != NULL) |
||
| 122 | { |
||
| 2421 | mencl | 123 | printf(","); |
| 2416 | mencl | 124 | print_tree_structure_flat (node->rgt, level + 1); |
| 125 | } |
||
| 126 | |||
| 2421 | mencl | 127 | printf(")"); |
| 2416 | mencl | 128 | } |
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | //**************************************************************** |
||
| 133 | static void alloc_avltree_node_prepare(void) |
||
| 134 | { |
||
| 135 | int i; |
||
| 136 | |||
| 137 | for (i = 0; i < NODE_COUNT - 1; i++) { |
||
| 2421 | mencl | 138 | avltree_nodes[i].par = &(avltree_nodes[i+1]); |
| 2416 | mencl | 139 | } |
| 140 | /* |
||
| 141 | * Node keys which will be used for insertion. Up to NODE_COUNT size of array. |
||
| 142 | */ |
||
| 143 | |||
| 144 | // First tree node and same key |
||
| 145 | avltree_nodes[0].key = 60; |
||
| 146 | avltree_nodes[1].key = 60; |
||
| 147 | avltree_nodes[2].key = 60; |
||
| 148 | //LL rotation |
||
| 149 | avltree_nodes[3].key = 50; |
||
| 150 | avltree_nodes[4].key = 40; |
||
| 151 | avltree_nodes[5].key = 30; |
||
| 152 | //LR rotation |
||
| 153 | avltree_nodes[6].key = 20; |
||
| 154 | avltree_nodes[7].key = 20; |
||
| 155 | avltree_nodes[8].key = 25; |
||
| 156 | avltree_nodes[9].key = 25; |
||
| 157 | //LL rotation in lower floor |
||
| 158 | avltree_nodes[10].key = 35; |
||
| 159 | //RR rotation |
||
| 160 | avltree_nodes[11].key = 70; |
||
| 161 | avltree_nodes[12].key = 80; |
||
| 162 | //RL rotation |
||
| 163 | avltree_nodes[13].key = 90; |
||
| 164 | avltree_nodes[14].key = 85; |
||
| 165 | avltree_nodes[15].key = 100; |
||
| 166 | avltree_nodes[16].key = 200; |
||
| 167 | avltree_nodes[17].key = 300; |
||
| 168 | avltree_nodes[18].key = 400; |
||
| 169 | avltree_nodes[19].key = 500; |
||
| 170 | avltree_nodes[20].key = 600; |
||
| 171 | |||
| 172 | for (i = 21; i < NODE_COUNT; i++) |
||
| 173 | avltree_nodes[i].key = i * 3; |
||
| 174 | |||
| 2421 | mencl | 175 | avltree_nodes[i].par = NULL; |
| 2416 | mencl | 176 | first_free_node = &(avltree_nodes[0]); |
| 177 | } |
||
| 178 | |||
| 179 | static avltree_node_t *alloc_avltree_node(void) |
||
| 180 | { |
||
| 181 | avltree_node_t *node; |
||
| 182 | |||
| 183 | node = first_free_node; |
||
| 2421 | mencl | 184 | first_free_node = first_free_node->par; |
| 2416 | mencl | 185 | |
| 186 | return node; |
||
| 187 | } |
||
| 188 | //**************************************************************** |
||
| 189 | |||
| 190 | static void test_tree_insert(avltree_t *tree, unsigned int node_count, int quiet) |
||
| 191 | { |
||
| 192 | unsigned int i; |
||
| 193 | avltree_node_t *newnode; |
||
| 194 | |||
| 195 | /* |
||
| 196 | * Initialize tree before using. |
||
| 197 | */ |
||
| 198 | avltree_create(tree); |
||
| 199 | |||
| 2421 | mencl | 200 | if (!quiet) printf("\nInserting %d nodes ...\n", node_count); |
| 2416 | mencl | 201 | |
| 202 | for (i = 0; i < node_count; i++) { |
||
| 203 | newnode = alloc_avltree_node(); |
||
| 204 | //if (!quiet) printf("[[[%d]]]\n",newnode->key); |
||
| 205 | |||
| 206 | avltree_insert(tree, newnode); |
||
| 207 | if (!quiet) { |
||
| 208 | test_tree_parents(tree->root); |
||
| 209 | test_tree_balance(tree->root); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | if (!quiet) printf("Inserting was finished\n"); |
||
| 214 | } |
||
| 215 | |||
| 216 | |||
| 217 | static void test_tree_delete(avltree_t *tree, int node_count, int node_position, bool quiet) |
||
| 218 | { |
||
| 219 | avltree_node_t *delnode; |
||
| 220 | unsigned int i; |
||
| 221 | |||
| 222 | //aktualni pocet tiku: |
||
| 223 | if (!quiet) printf("Deleting tree...\n"); |
||
| 224 | |||
| 225 | switch(node_position) { |
||
| 226 | case 0: //mazani vzdy korene |
||
| 2421 | mencl | 227 | if (!quiet) printf("\nDelete root nodes\n"); |
| 2416 | mencl | 228 | while(tree->root != NULL) { |
| 229 | delnode = tree->root; |
||
| 230 | avltree_delete(tree,delnode); |
||
| 231 | if (!quiet) { |
||
| 232 | test_tree_parents(tree->root); |
||
| 233 | test_tree_balance(tree->root); |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | break; |
||
| 238 | case 1: |
||
| 2421 | mencl | 239 | if (!quiet) printf("\nDelete nodes according to their time of origin\n"); |
| 2416 | mencl | 240 | for (i = 0; i < node_count; i++) { |
| 241 | avltree_delete(tree,&(avltree_nodes[i])); |
||
| 242 | if (!quiet) { |
||
| 243 | test_tree_parents(tree->root); |
||
| 244 | test_tree_balance(tree->root); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | break; |
||
| 249 | } |
||
| 250 | |||
| 251 | if (!quiet) printf("Deletion was finished\n"); |
||
| 252 | } |
||
| 253 | |||
| 254 | static void timeout_tree(avltree_t *tree, int node_count, bool quiet) |
||
| 255 | { |
||
| 256 | int i = 0; |
||
| 257 | |||
| 2421 | mencl | 258 | if (!quiet) printf("\nTimeout tree ...\n"); |
| 2416 | mencl | 259 | |
| 2421 | mencl | 260 | while(tree->root != NULL) { |
| 2416 | mencl | 261 | i++; |
| 262 | avltree_delete_min(tree); |
||
| 263 | if (!quiet) { |
||
| 264 | test_tree_parents(tree->root); |
||
| 265 | test_tree_balance(tree->root); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 2421 | mencl | 269 | if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!\n"); |
| 2416 | mencl | 270 | |
| 271 | if (!quiet) printf("Timeout tree finished\n"); |
||
| 272 | } |
||
| 273 | |||
| 274 | char * test_avltree1(bool quiet) |
||
| 275 | { |
||
| 276 | alloc_avltree_node_prepare(); |
||
| 2421 | mencl | 277 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
| 278 | test_tree_delete(&avltree, NODE_COUNT, 0, quiet); |
||
| 2416 | mencl | 279 | |
| 280 | alloc_avltree_node_prepare(); |
||
| 2421 | mencl | 281 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
| 282 | test_tree_delete(&avltree, NODE_COUNT, 1, quiet); |
||
| 2416 | mencl | 283 | |
| 284 | alloc_avltree_node_prepare(); |
||
| 2421 | mencl | 285 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
| 286 | timeout_tree(&avltree, NODE_COUNT, quiet); |
||
| 2416 | mencl | 287 | |
| 288 | return NULL; |
||
| 289 | } |