Rev 2416 | Rev 2431 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2416 | Rev 2421 | ||
---|---|---|---|
Line 52... | Line 52... | ||
52 | static avltree_node_t *first_free_node = NULL; |
52 | static avltree_node_t *first_free_node = NULL; |
53 | 53 | ||
54 | 54 | ||
55 | 55 | ||
56 | static int test_tree_balance(avltree_node_t *node); |
56 | static int test_tree_balance(avltree_node_t *node); |
57 | static avltree_node_t *tree_test_parents(avltree_node_t *node); |
57 | static avltree_node_t *test_tree_parents(avltree_node_t *node); |
58 | static void print_tree_structure_flat (avltree_node_t *node, int level); |
58 | static void print_tree_structure_flat (avltree_node_t *node, int level); |
59 | static avltree_node_t *alloc_avltree_node(void); |
59 | static avltree_node_t *alloc_avltree_node(void); |
60 | 60 | ||
61 | 61 | ||
62 | 62 | ||
Line 96... | Line 96... | ||
96 | } |
96 | } |
97 | 97 | ||
98 | /** |
98 | /** |
99 | * Prints the structure of node, which is level levels from the top of the tree. |
99 | * Prints the structure of node, which is level levels from the top of the tree. |
100 | */ |
100 | */ |
101 | static void print_tree_structure_flat (const avltree_node_t *node, int level) |
101 | static void print_tree_structure_flat (avltree_node_t *node, int level) |
102 | { |
102 | { |
103 | /* You can set the maximum level as high as you like. |
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, |
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. */ |
105 | so that a large level indicates a loop, which is a bug. */ |
106 | if (level > 16) |
106 | if (level > 16) |
Line 113... | Line 113... | ||
113 | return; |
113 | return; |
114 | 114 | ||
115 | printf ("%d[%d]", node->key,node->balance); |
115 | printf ("%d[%d]", node->key,node->balance); |
116 | if (node->lft != NULL || node->rgt != NULL) |
116 | if (node->lft != NULL || node->rgt != NULL) |
117 | { |
117 | { |
118 | putchar ('('); |
118 | printf("("); |
119 | 119 | ||
120 | print_tree_structure_flat (node->lft, level + 1); |
120 | print_tree_structure_flat (node->lft, level + 1); |
121 | if (node->rgt != NULL) |
121 | if (node->rgt != NULL) |
122 | { |
122 | { |
123 | putchar (','); |
123 | printf(","); |
124 | print_tree_structure_flat (node->rgt, level + 1); |
124 | print_tree_structure_flat (node->rgt, level + 1); |
125 | } |
125 | } |
126 | 126 | ||
127 | putchar (')'); |
127 | printf(")"); |
128 | } |
128 | } |
129 | } |
129 | } |
130 | 130 | ||
131 | 131 | ||
132 | //**************************************************************** |
132 | //**************************************************************** |
133 | static void alloc_avltree_node_prepare(void) |
133 | static void alloc_avltree_node_prepare(void) |
134 | { |
134 | { |
135 | int i; |
135 | int i; |
136 | 136 | ||
137 | for (i = 0; i < NODE_COUNT - 1; i++) { |
137 | for (i = 0; i < NODE_COUNT - 1; i++) { |
138 | avltree_nodes[i].n = &(avltree_nodes[i+1]); |
138 | avltree_nodes[i].par = &(avltree_nodes[i+1]); |
139 | } |
139 | } |
140 | /* |
140 | /* |
141 | * Node keys which will be used for insertion. Up to NODE_COUNT size of array. |
141 | * Node keys which will be used for insertion. Up to NODE_COUNT size of array. |
142 | */ |
142 | */ |
143 | 143 | ||
Line 170... | Line 170... | ||
170 | avltree_nodes[20].key = 600; |
170 | avltree_nodes[20].key = 600; |
171 | 171 | ||
172 | for (i = 21; i < NODE_COUNT; i++) |
172 | for (i = 21; i < NODE_COUNT; i++) |
173 | avltree_nodes[i].key = i * 3; |
173 | avltree_nodes[i].key = i * 3; |
174 | 174 | ||
175 | avltree_nodes[i].n = NULL; |
175 | avltree_nodes[i].par = NULL; |
176 | first_free_node = &(avltree_nodes[0]); |
176 | first_free_node = &(avltree_nodes[0]); |
177 | } |
177 | } |
178 | 178 | ||
179 | static avltree_node_t *alloc_avltree_node(void) |
179 | static avltree_node_t *alloc_avltree_node(void) |
180 | { |
180 | { |
181 | avltree_node_t *node; |
181 | avltree_node_t *node; |
182 | 182 | ||
183 | node = first_free_node; |
183 | node = first_free_node; |
184 | first_free_node = first_free_node->n; |
184 | first_free_node = first_free_node->par; |
185 | 185 | ||
186 | return node; |
186 | return node; |
187 | } |
187 | } |
188 | //**************************************************************** |
188 | //**************************************************************** |
189 | 189 | ||
Line 195... | Line 195... | ||
195 | /* |
195 | /* |
196 | * Initialize tree before using. |
196 | * Initialize tree before using. |
197 | */ |
197 | */ |
198 | avltree_create(tree); |
198 | avltree_create(tree); |
199 | 199 | ||
200 | if (!quiet) printf("\n\nInserting %d nodes ...\n", node_count); |
200 | if (!quiet) printf("\nInserting %d nodes ...\n", node_count); |
201 | 201 | ||
202 | for (i = 0; i < node_count; i++) { |
202 | for (i = 0; i < node_count; i++) { |
203 | newnode = alloc_avltree_node(); |
203 | newnode = alloc_avltree_node(); |
204 | //if (!quiet) printf("[[[%d]]]\n",newnode->key); |
204 | //if (!quiet) printf("[[[%d]]]\n",newnode->key); |
205 | 205 | ||
Line 211... | Line 211... | ||
211 | } |
211 | } |
212 | 212 | ||
213 | if (!quiet) printf("Inserting was finished\n"); |
213 | if (!quiet) printf("Inserting was finished\n"); |
214 | } |
214 | } |
215 | 215 | ||
216 | /* |
- | |
217 | static avltree_node_t *tree_random_delete_node(avltree_t *tree, int node_count, int r, bool quiet) |
- | |
218 | { |
- | |
219 | avltree_node_t *delnode; |
- | |
220 | int i; |
- | |
221 | - | ||
222 | for (i = 0,delnode = tree->head.n; i < (r-1); i++) |
- | |
223 | delnode = delnode->n; |
- | |
224 | |
- | |
225 | if (delnode == &tree->head) { |
- | |
226 | if (!quiet) printf("Try to delete head! Node count: %d, number of deleted node: %d\n",node_count,r); |
- | |
227 | return NULL; |
- | |
228 | } |
- | |
229 | - | ||
230 | avltree_delete(tree, delnode); |
- | |
231 | - | ||
232 | return delnode; |
- | |
233 | } |
- | |
234 | */ |
- | |
235 | 216 | ||
236 | static void test_tree_delete(avltree_t *tree, int node_count, int node_position, bool quiet) |
217 | static void test_tree_delete(avltree_t *tree, int node_count, int node_position, bool quiet) |
237 | { |
218 | { |
238 | avltree_node_t *delnode; |
219 | avltree_node_t *delnode; |
239 | unsigned int i; |
220 | unsigned int i; |
Line 241... | Line 222... | ||
241 | //aktualni pocet tiku: |
222 | //aktualni pocet tiku: |
242 | if (!quiet) printf("Deleting tree...\n"); |
223 | if (!quiet) printf("Deleting tree...\n"); |
243 | 224 | ||
244 | switch(node_position) { |
225 | switch(node_position) { |
245 | case 0: //mazani vzdy korene |
226 | case 0: //mazani vzdy korene |
246 | if (!quiet) printf("\n\nDelete root nodes\n"); |
227 | if (!quiet) printf("\nDelete root nodes\n"); |
247 | while(tree->root != NULL) { |
228 | while(tree->root != NULL) { |
248 | delnode = tree->root; |
229 | delnode = tree->root; |
249 | avltree_delete(tree,delnode); |
230 | avltree_delete(tree,delnode); |
250 | if (!quiet) { |
231 | if (!quiet) { |
251 | test_tree_parents(tree->root); |
232 | test_tree_parents(tree->root); |
Line 253... | Line 234... | ||
253 | } |
234 | } |
254 | } |
235 | } |
255 | 236 | ||
256 | break; |
237 | break; |
257 | case 1: |
238 | case 1: |
258 | if (!quiet) printf("\n\nDelete nodes according to their time of origin\n"); |
239 | if (!quiet) printf("\nDelete nodes according to their time of origin\n"); |
259 | for (i = 0; i < node_count; i++) { |
240 | for (i = 0; i < node_count; i++) { |
260 | avltree_delete(tree,&(avltree_nodes[i])); |
241 | avltree_delete(tree,&(avltree_nodes[i])); |
261 | if (!quiet) { |
242 | if (!quiet) { |
262 | test_tree_parents(tree->root); |
243 | test_tree_parents(tree->root); |
263 | test_tree_balance(tree->root); |
244 | test_tree_balance(tree->root); |
Line 272... | Line 253... | ||
272 | 253 | ||
273 | static void timeout_tree(avltree_t *tree, int node_count, bool quiet) |
254 | static void timeout_tree(avltree_t *tree, int node_count, bool quiet) |
274 | { |
255 | { |
275 | int i = 0; |
256 | int i = 0; |
276 | 257 | ||
277 | if (!quiet) printf("Timeout tree ...\n"); |
258 | if (!quiet) printf("\nTimeout tree ...\n"); |
278 | 259 | ||
279 | while(tree->head.n != &(tree->head)) { |
260 | while(tree->root != NULL) { |
280 | i++; |
261 | i++; |
281 | avltree_delete_min(tree); |
262 | avltree_delete_min(tree); |
282 | if (!quiet) { |
263 | if (!quiet) { |
283 | test_tree_parents(tree->root); |
264 | test_tree_parents(tree->root); |
284 | test_tree_balance(tree->root); |
265 | test_tree_balance(tree->root); |
285 | } |
266 | } |
286 | } |
267 | } |
287 | 268 | ||
288 | if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!"); |
269 | if (!quiet && (i != node_count)) printf("Bad node count. Some nodes have been lost!\n"); |
289 | 270 | ||
290 | if (!quiet) printf("Timeout tree finished\n"); |
271 | if (!quiet) printf("Timeout tree finished\n"); |
291 | } |
272 | } |
292 | 273 | ||
293 | /* |
- | |
294 | void timeout_tree_run(avltree_t *tree, int operation_count, int verbal) |
- | |
295 | { |
- | |
296 | int i; |
- | |
297 | avltree_node_t *node; |
- | |
298 | int r; |
- | |
299 | int count; |
- | |
300 | |
- | |
301 | //inicializace stromu: |
- | |
302 | avltree_create(tree); |
- | |
303 | |
- | |
304 | for(i = 0, count = 0; i < operation_count; i++) { |
- | |
305 | if (tree->count && ((rand() % NODE_COUNT) <= tree->count)) { |
- | |
306 | if ((r = rand()) % DELETE_PROB == 1) { //mazu nahodne |
- | |
307 | node = tree_random_delete_node(tree,(r % tree->count)); |
- | |
308 | //printf("DELETE key: %d, number: %d,address: %p\n",node->key,r % (tree->count+1),node); |
- | |
309 | node->n = first_free_node; |
- | |
310 | first_free_node = node; |
- | |
311 | } else { |
- | |
312 | node = tree->head.n; |
- | |
313 | avltree_delete_min(tree); |
- | |
314 | //printf("TIMEOUT key: %d, address: %p\n",node->key,node); |
- | |
315 | node->n = first_free_node; |
- | |
316 | first_free_node = node; |
- | |
317 | } |
- | |
318 | } else { |
- | |
319 | node = alloc_avltree_node_random(); |
- | |
320 | //printf("INSERT key: %d, address: %p\n",node->key + tree->basetime,node); |
- | |
321 | avltree_insert(tree, node); |
- | |
322 | } |
- | |
323 | //test_tree_height(tree->root,1); |
- | |
324 | //tree_test_parents(tree->root); |
- | |
325 | //print_tree_link(tree->count); |
- | |
326 | //print_tree_structure_flat(tree->root,0); putchar('\n'); putchar('\n'); |
- | |
327 | } |
- | |
328 | } |
- | |
329 | */ |
- | |
330 | - | ||
331 | char * test_avltree1(bool quiet) |
274 | char * test_avltree1(bool quiet) |
332 | { |
275 | { |
333 | alloc_avltree_node_prepare(); |
276 | alloc_avltree_node_prepare(); |
334 | test_tree_insert(&tree, NODE_COUNT, quiet); |
277 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
335 | test_tree_delete(&tree, NODE_COUNT, 0, quiet); |
278 | test_tree_delete(&avltree, NODE_COUNT, 0, quiet); |
336 | 279 | ||
337 | alloc_avltree_node_prepare(); |
280 | alloc_avltree_node_prepare(); |
338 | test_tree_insert(&tree, NODE_COUNT, quiet); |
281 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
339 | test_tree_delete(&tree, NODE_COUNT, 1, quiet); |
282 | test_tree_delete(&avltree, NODE_COUNT, 1, quiet); |
340 | 283 | ||
341 | alloc_avltree_node_prepare(); |
284 | alloc_avltree_node_prepare(); |
342 | test_tree_insert(&tree, NODE_COUNT, quiet); |
285 | test_tree_insert(&avltree, NODE_COUNT, quiet); |
343 | timeout_tree(&tree, NODE_COUNT, quiet); |
286 | timeout_tree(&avltree, NODE_COUNT, quiet); |
344 | 287 | ||
345 | return NULL; |
288 | return NULL; |
346 | } |
289 | } |