From 6ced2a50056acbf7caec404a1edfa3b07fa1ac5d Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Wed, 29 Dec 2021 10:48:03 +0100 Subject: feat: remove NULL-checks --- tree.c | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'tree.c') diff --git a/tree.c b/tree.c index fb38c89..e6ee86e 100644 --- a/tree.c +++ b/tree.c @@ -45,7 +45,7 @@ tree_insert(struct tree_node *tree, T key) { if ( tree == NULL ) { tree = malloc(sizeof *tree); - if ( tree != NULL ) { + if ( tree ) { tree->key = key; tree->count = 1; tree->left = NULL; @@ -71,7 +71,7 @@ tree_insert_it(struct tree_node *tree, T key) { struct tree_node *parent = NULL; - for ( struct tree_node *curr = tree; curr != NULL; ) { + for ( struct tree_node *curr = tree; curr; ) { parent = curr; if ( key < curr->key ) { @@ -89,7 +89,7 @@ tree_insert_it(struct tree_node *tree, T key) struct tree_node *new_node; new_node = malloc(sizeof *new_node); - if ( new_node != NULL ) { + if ( new_node ) { new_node->key = key; new_node->count = 1; new_node->left = NULL; @@ -121,7 +121,7 @@ tree_detach_min(struct tree_node **ptree) if ( tree == NULL ) return NULL; - else if ( tree->left != NULL ) + else if ( tree->left ) return tree_detach_min(&tree->left); else { *ptree = tree->right; @@ -253,7 +253,7 @@ tree_copy(struct tree_node *tree) struct tree_node *new_node; new_node = malloc(sizeof *new_node); - if ( new_node != NULL ) { + if ( new_node ) { new_node->key = tree->key; new_node->count = tree->count; new_node->left = tree_copy(tree->left); @@ -340,7 +340,7 @@ stack_push(struct stack *stack, struct tree_node *data) { struct stack_item *new_item; - if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { + if ( (new_item = malloc(sizeof(*new_item))) ) { new_item->data = data; new_item->next = stack->head; stack->head = new_item; @@ -354,7 +354,7 @@ stack_push(struct stack *stack, struct tree_node *data) static bool stack_pop(struct stack *stack, struct tree_node **data) { - if ( stack->head != NULL ) { + if ( stack->head ) { struct stack_item *next; next = stack->head->next; @@ -374,7 +374,7 @@ stack_pop(struct stack *stack, struct tree_node **data) static struct tree_node * stack_peek(struct stack *stack) { - return (stack->head != NULL) ? stack->head->data : NULL; + return (stack->head) ? stack->head->data : NULL; } /* -->8-- */ @@ -426,7 +426,7 @@ queue_put(struct queue *queue, struct tree_node *data) { struct queue_item *new_item; - if ( (new_item = malloc(sizeof(*new_item))) != NULL ) { + if ( (new_item = malloc(sizeof(*new_item))) ) { struct queue_item *tail; tail = queue->tail; @@ -448,7 +448,7 @@ queue_put(struct queue *queue, struct tree_node *data) bool queue_get(struct queue *queue, struct tree_node **data) { - if ( queue->head != NULL ) { + if ( queue->head ) { struct queue_item *next; next = queue->head->next; @@ -491,7 +491,7 @@ queue_free(struct queue *queue) void tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) { - if ( tree != NULL ) { + if ( tree ) { struct stack stack; stack_init(&stack); @@ -501,9 +501,9 @@ tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), v while ( stack_pop(&stack, &tree) ) { visit(tree->key, cl); - if ( tree->right != NULL ) + if ( tree->right ) stack_push(&stack, tree->right); - if ( tree->left != NULL ) + if ( tree->left ) stack_push(&stack, tree->left); } @@ -516,13 +516,13 @@ tree_apply_preorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), v void tree_apply_inorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) { - if ( tree != NULL ) { + if ( tree ) { struct stack stack; stack_init(&stack); - while ( !stack_empty(&stack) || tree != NULL ) { - if ( tree != NULL ) { + while ( !stack_empty(&stack) || tree ) { + if ( tree ) { stack_push(&stack, tree); tree = tree->left; } @@ -545,7 +545,7 @@ tree_apply_inorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), vo void tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) { - if ( tree != NULL ) { + if ( tree ) { struct stack stack; stack_init(&stack); @@ -564,10 +564,10 @@ tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), tree = next; } else { - if ( next->right != NULL ) { + if ( next->right ) { stack_push(&stack, next->right); } - if ( next->left != NULL ) { + if ( next->left ) { stack_push(&stack, next->left); } } @@ -581,7 +581,7 @@ tree_apply_postorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), void *cl) { - if ( tree != NULL ) { + if ( tree ) { struct queue queue; queue_init(&queue); @@ -591,9 +591,9 @@ tree_apply_levelorder_it(struct tree_node *tree, void (*visit)(T key, void *cl), while ( queue_get(&queue, &tree) ) { visit(tree->key, cl); - if ( tree->left != NULL ) + if ( tree->left ) queue_put(&queue, tree->left); - if ( tree->right != NULL ) + if ( tree->right ) queue_put(&queue, tree->right); } @@ -661,10 +661,10 @@ tree_preorder_iterator_next(struct tree_iterator *it) struct tree_node *node = NULL; if ( stack_pop(&it->stack, &node) ) { - if ( node->right != NULL ) { + if ( node->right ) { stack_push(&it->stack, node->right); } - if ( node->left != NULL ) { + if ( node->left ) { stack_push(&it->stack, node->left); } } -- cgit v1.3