diff options
| author | Thomas Schmucker <ts@its1.de> | 2021-12-29 10:49:19 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2021-12-29 10:49:19 +0100 |
| commit | 5e696c74c9649b0e54133d46a3ae92e5ce0632fb (patch) | |
| tree | ef9d4e6bcf9797b74e13ddbf943f424ac1d49664 /tree.c | |
| parent | a9fecebe13be05c15a5adf5a6fcefacb73d0d035 (diff) | |
| parent | 6ced2a50056acbf7caec404a1edfa3b07fa1ac5d (diff) | |
| download | data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.tar.gz data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.tar.bz2 data-structures-5e696c74c9649b0e54133d46a3ae92e5ce0632fb.zip | |
Merge branch 'feat/remove-null-checks'
Diffstat (limited to 'tree.c')
| -rw-r--r-- | tree.c | 48 |
1 files changed, 24 insertions, 24 deletions
| @@ -45,7 +45,7 @@ tree_insert(struct tree_node *tree, T key) | |||
| 45 | { | 45 | { |
| 46 | if ( tree == NULL ) { | 46 | if ( tree == NULL ) { |
| 47 | tree = malloc(sizeof *tree); | 47 | tree = malloc(sizeof *tree); |
| 48 | if ( tree != NULL ) { | 48 | if ( tree ) { |
| 49 | tree->key = key; | 49 | tree->key = key; |
| 50 | tree->count = 1; | 50 | tree->count = 1; |
| 51 | tree->left = NULL; | 51 | tree->left = NULL; |
| @@ -71,7 +71,7 @@ tree_insert_it(struct tree_node *tree, T key) | |||
| 71 | { | 71 | { |
| 72 | struct tree_node *parent = NULL; | 72 | struct tree_node *parent = NULL; |
| 73 | 73 | ||
| 74 | for ( struct tree_node *curr = tree; curr != NULL; ) { | 74 | for ( struct tree_node *curr = tree; curr; ) { |
| 75 | parent = curr; | 75 | parent = curr; |
| 76 | 76 | ||
| 77 | if ( key < curr->key ) { | 77 | if ( key < curr->key ) { |
| @@ -89,7 +89,7 @@ tree_insert_it(struct tree_node *tree, T key) | |||
| 89 | struct tree_node *new_node; | 89 | struct tree_node *new_node; |
| 90 | 90 | ||
| 91 | new_node = malloc(sizeof *new_node); | 91 | new_node = malloc(sizeof *new_node); |
| 92 | if ( new_node != NULL ) { | 92 | if ( new_node ) { |
| 93 | new_node->key = key; | 93 | new_node->key = key; |
| 94 | new_node->count = 1; | 94 | new_node->count = 1; |
| 95 | new_node->left = NULL; | 95 | new_node->left = NULL; |
| @@ -121,7 +121,7 @@ tree_detach_min(struct tree_node **ptree) | |||
| 121 | 121 | ||
| 122 | if ( tree == NULL ) | 122 | if ( tree == NULL ) |
| 123 | return NULL; | 123 | return NULL; |
| 124 | else if ( tree->left != NULL ) | 124 | else if ( tree->left ) |
| 125 | return tree_detach_min(&tree->left); | 125 | return tree_detach_min(&tree->left); |
| 126 | else { | 126 | else { |
| 127 | *ptree = tree->right; | 127 | *ptree = tree->right; |
| @@ -253,7 +253,7 @@ tree_copy(struct tree_node *tree) | |||
| 253 | struct tree_node *new_node; | 253 | struct tree_node *new_node; |
| 254 | 254 | ||
| 255 | new_node = malloc(sizeof *new_node); | 255 | new_node = malloc(sizeof *new_node); |
| 256 | if ( new_node != NULL ) { | 256 | if ( new_node ) { |
| 257 | new_node->key = tree->key; | 257 | new_node->key = tree->key; |
| 258 | new_node->count = tree->count; | 258 | new_node->count = tree->count; |
| 259 | new_node->left = tree_copy(tree->left); | 259 | new_node->left = tree_copy(tree->left); |
| @@ -340,7 +340,7 @@ stack_push(struct stack *stack, struct tree_node *data) | |||
| 340 | { | 340 | { |
| 341 | struct stack_item *new_item; | 341 | struct stack_item *new_item; |
| 342 | 342 | ||
| 343 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { | 343 | if ( (new_item = malloc(sizeof(*new_item))) ) { |
| 344 | new_item->data = data; | 344 | new_item->data = data; |
| 345 | new_item->next = stack->head; | 345 | new_item->next = stack->head; |
| 346 | stack->head = new_item; | 346 | stack->head = new_item; |
| @@ -354,7 +354,7 @@ stack_push(struct stack *stack, struct tree_node *data) | |||
| 354 | static bool | 354 | static bool |
| 355 | stack_pop(struct stack *stack, struct tree_node **data) | 355 | stack_pop(struct stack *stack, struct tree_node **data) |
| 356 | { | 356 | { |
| 357 | if ( stack->head != NULL ) { | 357 | if ( stack->head ) { |
| 358 | struct stack_item *next; | 358 | struct stack_item *next; |
| 359 | 359 | ||
| 360 | next = stack->head->next; | 360 | next = stack->head->next; |
| @@ -374,7 +374,7 @@ stack_pop(struct stack *stack, struct tree_node **data) | |||
| 374 | static struct tree_node * | 374 | static struct tree_node * |
| 375 | stack_peek(struct stack *stack) | 375 | stack_peek(struct stack *stack) |
| 376 | { | 376 | { |
| 377 | return (stack->head != NULL) ? stack->head->data : NULL; | 377 | return (stack->head) ? stack->head->data : NULL; |
| 378 | } | 378 | } |
| 379 | /* -->8-- */ | 379 | /* -->8-- */ |
| 380 | 380 | ||
| @@ -426,7 +426,7 @@ queue_put(struct queue *queue, struct tree_node *data) | |||
| 426 | { | 426 | { |
| 427 | struct queue_item *new_item; | 427 | struct queue_item *new_item; |
| 428 | 428 | ||
| 429 | if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { | 429 | if ( (new_item = malloc(sizeof(*new_item))) ) { |
| 430 | struct queue_item *tail; | 430 | struct queue_item *tail; |
| 431 | 431 | ||
| 432 | tail = queue->tail; | 432 | tail = queue->tail; |
| @@ -448,7 +448,7 @@ queue_put(struct queue *queue, struct tree_node *data) | |||
| 448 | bool | 448 | bool |
| 449 | queue_get(struct queue *queue, struct tree_node **data) | 449 | queue_get(struct queue *queue, struct tree_node **data) |
| 450 | { | 450 | { |
| 451 | if ( queue->head != NULL ) { | 451 | if ( queue->head ) { |
| 452 | struct queue_item *next; | 452 | struct queue_item *next; |
| 453 | 453 | ||
| 454 | next = queue->head->next; | 454 | next = queue->head->next; |
| @@ -491,7 +491,7 @@ queue_free(struct queue *queue) | |||
| 491 | void | 491 | void |
| 492 | tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | 492 | tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) |
| 493 | { | 493 | { |
| 494 | if ( tree != NULL ) { | 494 | if ( tree ) { |
| 495 | struct stack stack; | 495 | struct stack stack; |
| 496 | 496 | ||
| 497 | stack_init(&stack); | 497 | stack_init(&stack); |
| @@ -501,9 +501,9 @@ tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), v | |||
| 501 | while ( stack_pop(&stack, &tree) ) { | 501 | while ( stack_pop(&stack, &tree) ) { |
| 502 | visit(tree->key, cl); | 502 | visit(tree->key, cl); |
| 503 | 503 | ||
| 504 | if ( tree->right != NULL ) | 504 | if ( tree->right ) |
| 505 | stack_push(&stack, tree->right); | 505 | stack_push(&stack, tree->right); |
| 506 | if ( tree->left != NULL ) | 506 | if ( tree->left ) |
| 507 | stack_push(&stack, tree->left); | 507 | stack_push(&stack, tree->left); |
| 508 | } | 508 | } |
| 509 | 509 | ||
| @@ -516,13 +516,13 @@ tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), v | |||
| 516 | void | 516 | void |
| 517 | tree_apply_inorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | 517 | tree_apply_inorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) |
| 518 | { | 518 | { |
| 519 | if ( tree != NULL ) { | 519 | if ( tree ) { |
| 520 | struct stack stack; | 520 | struct stack stack; |
| 521 | 521 | ||
| 522 | stack_init(&stack); | 522 | stack_init(&stack); |
| 523 | 523 | ||
| 524 | while ( !stack_empty(&stack) || tree != NULL ) { | 524 | while ( !stack_empty(&stack) || tree ) { |
| 525 | if ( tree != NULL ) { | 525 | if ( tree ) { |
| 526 | stack_push(&stack, tree); | 526 | stack_push(&stack, tree); |
| 527 | tree = tree->left; | 527 | tree = tree->left; |
| 528 | } | 528 | } |
| @@ -545,7 +545,7 @@ tree_apply_inorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), vo | |||
| 545 | void | 545 | void |
| 546 | tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | 546 | tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) |
| 547 | { | 547 | { |
| 548 | if ( tree != NULL ) { | 548 | if ( tree ) { |
| 549 | struct stack stack; | 549 | struct stack stack; |
| 550 | 550 | ||
| 551 | stack_init(&stack); | 551 | stack_init(&stack); |
| @@ -564,10 +564,10 @@ tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), | |||
| 564 | tree = next; | 564 | tree = next; |
| 565 | } | 565 | } |
| 566 | else { | 566 | else { |
| 567 | if ( next->right != NULL ) { | 567 | if ( next->right ) { |
| 568 | stack_push(&stack, next->right); | 568 | stack_push(&stack, next->right); |
| 569 | } | 569 | } |
| 570 | if ( next->left != NULL ) { | 570 | if ( next->left ) { |
| 571 | stack_push(&stack, next->left); | 571 | stack_push(&stack, next->left); |
| 572 | } | 572 | } |
| 573 | } | 573 | } |
| @@ -581,7 +581,7 @@ tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), | |||
| 581 | void | 581 | void |
| 582 | tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) | 582 | tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) |
| 583 | { | 583 | { |
| 584 | if ( tree != NULL ) { | 584 | if ( tree ) { |
| 585 | struct queue queue; | 585 | struct queue queue; |
| 586 | 586 | ||
| 587 | queue_init(&queue); | 587 | queue_init(&queue); |
| @@ -591,9 +591,9 @@ tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), | |||
| 591 | while ( queue_get(&queue, &tree) ) { | 591 | while ( queue_get(&queue, &tree) ) { |
| 592 | visit(tree->key, cl); | 592 | visit(tree->key, cl); |
| 593 | 593 | ||
| 594 | if ( tree->left != NULL ) | 594 | if ( tree->left ) |
| 595 | queue_put(&queue, tree->left); | 595 | queue_put(&queue, tree->left); |
| 596 | if ( tree->right != NULL ) | 596 | if ( tree->right ) |
| 597 | queue_put(&queue, tree->right); | 597 | queue_put(&queue, tree->right); |
| 598 | } | 598 | } |
| 599 | 599 | ||
| @@ -661,10 +661,10 @@ tree_preorder_iterator_next(struct tree_iterator *it) | |||
| 661 | struct tree_node *node = NULL; | 661 | struct tree_node *node = NULL; |
| 662 | 662 | ||
| 663 | if ( stack_pop(&it->stack, &node) ) { | 663 | if ( stack_pop(&it->stack, &node) ) { |
| 664 | if ( node->right != NULL ) { | 664 | if ( node->right ) { |
| 665 | stack_push(&it->stack, node->right); | 665 | stack_push(&it->stack, node->right); |
| 666 | } | 666 | } |
| 667 | if ( node->left != NULL ) { | 667 | if ( node->left ) { |
| 668 | stack_push(&it->stack, node->left); | 668 | stack_push(&it->stack, node->left); |
| 669 | } | 669 | } |
| 670 | } | 670 | } |
