diff options
| author | Thomas Schmucker <ts@its1.de> | 2020-08-06 09:10:55 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2020-08-06 09:10:55 +0200 |
| commit | 4e3ae6fb8703853b21997e4cf60ecc6b141b048d (patch) | |
| tree | 184cf07c6e4f00b18dc560ae6ccbec4228cefdc4 | |
| parent | 0dbae9bb1846ea279bc1c97f03f4499d5ac4490e (diff) | |
| download | data-structures-4e3ae6fb8703853b21997e4cf60ecc6b141b048d.tar.gz data-structures-4e3ae6fb8703853b21997e4cf60ecc6b141b048d.tar.bz2 data-structures-4e3ae6fb8703853b21997e4cf60ecc6b141b048d.zip | |
add: levelorder-iteration für Trees
| -rw-r--r-- | tree.c | 102 |
1 files changed, 102 insertions, 0 deletions
| @@ -325,6 +325,79 @@ stack_free(struct stack *stack) | |||
| 325 | } | 325 | } |
| 326 | } | 326 | } |
| 327 | 327 | ||
| 328 | /* ===== */ | ||
| 329 | |||
| 330 | struct queue_item { | ||
| 331 | struct queue_item *next; | ||
| 332 | struct tree_node * data; | ||
| 333 | }; | ||
| 334 | |||
| 335 | struct queue { | ||
| 336 | struct queue_item *head, *tail; | ||
| 337 | }; | ||
| 338 | |||
| 339 | void | ||
| 340 | queue_init(struct queue *queue) | ||
| 341 | { | ||
| 342 | queue->head = NULL; | ||
| 343 | } | ||
| 344 | |||
| 345 | void | ||
| 346 | queue_put(struct queue *queue, struct tree_node *data) | ||
| 347 | { | ||
| 348 | struct queue_item *new_item; | ||
| 349 | |||
| 350 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { | ||
| 351 | struct queue_item *tmp = queue->tail; | ||
| 352 | new_item->data = data; | ||
| 353 | new_item->next = NULL; | ||
| 354 | queue->tail = new_item; | ||
| 355 | if ( queue->head == NULL ) | ||
| 356 | queue->head = queue->tail; | ||
| 357 | else | ||
| 358 | tmp->next = queue->tail; | ||
| 359 | } | ||
| 360 | else | ||
| 361 | ERROR("out of memory"); | ||
| 362 | } | ||
| 363 | |||
| 364 | bool | ||
| 365 | queue_get(struct queue *queue, struct tree_node **data) | ||
| 366 | { | ||
| 367 | if ( queue->head != NULL ) { | ||
| 368 | struct queue_item *next; | ||
| 369 | |||
| 370 | next = queue->head->next; | ||
| 371 | *data = queue->head->data; | ||
| 372 | |||
| 373 | free(queue->head); | ||
| 374 | queue->head = next; | ||
| 375 | |||
| 376 | return true; | ||
| 377 | } | ||
| 378 | else | ||
| 379 | return false; | ||
| 380 | } | ||
| 381 | |||
| 382 | bool | ||
| 383 | queue_empty(struct queue *queue) | ||
| 384 | { | ||
| 385 | return queue->head == NULL; | ||
| 386 | } | ||
| 387 | |||
| 388 | void | ||
| 389 | queue_free(struct queue *queue) | ||
| 390 | { | ||
| 391 | struct queue_item *item, *next; | ||
| 392 | |||
| 393 | for ( item = queue->head; item; item = next ) { | ||
| 394 | next = item->next; | ||
| 395 | free(item); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | /* ===== */ | ||
| 400 | |||
| 328 | void | 401 | void |
| 329 | tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | 402 | tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) |
| 330 | { | 403 | { |
| @@ -410,6 +483,29 @@ tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), | |||
| 410 | } | 483 | } |
| 411 | } | 484 | } |
| 412 | 485 | ||
| 486 | void | ||
| 487 | tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | ||
| 488 | { | ||
| 489 | if ( tree != NULL ) { | ||
| 490 | struct queue queue; | ||
| 491 | |||
| 492 | queue_init(&queue); | ||
| 493 | |||
| 494 | queue_put(&queue, tree); | ||
| 495 | |||
| 496 | while ( queue_get(&queue, &tree) ) { | ||
| 497 | visit(tree->key, cl); | ||
| 498 | |||
| 499 | if ( tree->left != NULL ) | ||
| 500 | queue_put(&queue, tree->left); | ||
| 501 | if ( tree->right != NULL ) | ||
| 502 | queue_put(&queue, tree->right); | ||
| 503 | } | ||
| 504 | |||
| 505 | queue_free(&queue); | ||
| 506 | } | ||
| 507 | } | ||
| 508 | |||
| 413 | /* ======================== */ | 509 | /* ======================== */ |
| 414 | 510 | ||
| 415 | struct tree_iterator { | 511 | struct tree_iterator { |
| @@ -541,11 +637,17 @@ main(void) | |||
| 541 | tree = tree_insert_it(tree, 15); | 637 | tree = tree_insert_it(tree, 15); |
| 542 | tree = tree_insert_it(tree, 18); | 638 | tree = tree_insert_it(tree, 18); |
| 543 | 639 | ||
| 640 | #if 0 | ||
| 544 | tree_apply_preorder(tree, print, NULL); | 641 | tree_apply_preorder(tree, print, NULL); |
| 545 | puts("postorder:"); | 642 | puts("postorder:"); |
| 546 | tree_apply_postorder(tree, print, NULL); | 643 | tree_apply_postorder(tree, print, NULL); |
| 547 | puts("postorder_it:"); | 644 | puts("postorder_it:"); |
| 548 | tree_apply_postorder_it(tree, print, NULL); | 645 | tree_apply_postorder_it(tree, print, NULL); |
| 646 | #endif | ||
| 647 | show_tree(tree, 0, 0); | ||
| 648 | |||
| 649 | puts("levelorder_it:"); | ||
| 650 | tree_apply_levelorder_it(tree, print, NULL); | ||
| 549 | 651 | ||
| 550 | show_tree(tree, 0, 0); | 652 | show_tree(tree, 0, 0); |
| 551 | 653 | ||
