From c2ab24da4ce22c8207e8d9ed02738f79cc1a6962 Mon Sep 17 00:00:00 2001 From: lenamarochkina Date: Sat, 15 Oct 2022 15:07:42 +0200 Subject: [PATCH] (refactor) changes due to requests --- src/bvs.c | 102 ++++++++++++++++++++++++------------------------------ src/bvs.h | 37 +++++++++++++++----- 2 files changed, 75 insertions(+), 64 deletions(-) diff --git a/src/bvs.c b/src/bvs.c index c9a027f..8081bce 100644 --- a/src/bvs.c +++ b/src/bvs.c @@ -1,8 +1,8 @@ #include "bvs.h" -tree_node *create_node(int value){ - tree_node* result = malloc(sizeof(tree_node)); - if(result != 0){ +tree_node *create_node(int value) { + tree_node *result = (tree_node *) malloc(sizeof(tree_node)); + if (result != 0) { result->left = NULL; result->right = NULL; result->value = value; @@ -10,46 +10,44 @@ tree_node *create_node(int value){ return result; } -int comparator(tree_node *root, int value){ - if(root->value == value){ +int comparator(tree_node *root, int value) { + if (root->value == value) { return 0; } - if(root->value > value){ + if (root->value > value) { return -1; } return 1; } -bool insert_number(tree_node **rootptr, int value){ +bool insert_number(tree_node **rootptr, int value) { tree_node *root = *rootptr; - if (root == NULL){ + if (root == NULL) { //tree empty (*rootptr) = create_node(value); return true; } - if (comparator(root, value) == 0){ - return false; - } - if (comparator(root, value) == -1){ - return insert_number(&(root->left), value); - } - if (comparator(root, value) == 1){ - return insert_number(&(root->right), value); + switch (comparator(root, value)) { + case 0: + return false; + case -1: + return insert_number(&(root->left), value); + case 1: + return insert_number(&(root->right), value); } } -bool find_number(tree_node *root, int value){ +bool find_number(tree_node *root, int value) { if (root == NULL) { return false; } - if (comparator(root, value) == 0){ - return true; - } - if (comparator(root, value) == -1){ - return find_number(root->left, value); - } - if (comparator(root, value) == 1){ - return find_number(root->right, value); + switch(comparator(root, value)){ + case 0: + return true; + case -1: + return find_number(root->left, value); + case 1: + return find_number(root->right, value); } } @@ -59,41 +57,33 @@ bool delete_number(tree_node **rootptr, int value) { if (root == NULL) { return false; } - if (comparator(root, value) == -1) { - return delete_number(&root->left, value); - } - if (comparator(root, value) == 1) { - return delete_number(&root->right, value); - } - if (comparator(root, value) == 0) { - //node has no children - if (root->left == NULL && root->right == NULL) { - free(root); - *rootptr = NULL; - return true; - } - //node has one child (right) - if (root->left == NULL) { - tree_node *temp = root->right; - free(temp); - *rootptr = NULL; - return true; - } //node has one child (left) - if (root->right == NULL) { - tree_node *temp = root->left; - free(temp); - *rootptr = NULL; - return true; - } - //node has two children - if (root->left != NULL && root->right != NULL) { + switch (comparator(root, value)) { + case 0: + if (root->left == NULL && root->right == NULL) { + free(root); + *rootptr = NULL; + return true; + } + if (root->left == NULL) { + *rootptr = root->right; + free(root); + return true; + } + if (root->right == NULL) { + *rootptr = root->left; + free(root); + return true; + } tree_node *temp = root->right; while (temp->left != NULL) { temp = temp->left; } root->value = temp->value; - delete_number(&root->right, temp->value); - return true; - } + return delete_number(&(root->right), temp->value); + case -1: + return delete_number(&(root->left), value); + case 1: + return delete_number(&(root->right), value); + } } \ No newline at end of file diff --git a/src/bvs.h b/src/bvs.h index 72a0c89..8228207 100644 --- a/src/bvs.h +++ b/src/bvs.h @@ -13,22 +13,43 @@ typedef struct tree_node { struct tree_node *right; } tree_node; -//node creation +/** + * Creates tree node with value + * @param value value in the node + * @return pointer to the tree node + */ tree_node *create_node(int value); -//tree initialization -tree_node *create_node(int value); - -//comparator +/** + * Compare value with value in the node + * @param root pointer to the node + * @param value value to compare + * @return 0 if values are equal, -1 if value in the node is greater, 1 if value in the node is smaller + */ int comparator(tree_node *root, int value); -//insert numbers into the node +/** + * Inserts value into the tree + * @param rootptr pointer to the pointer to the root of the tree + * @param value value to insert + * @return true if value was inserted, false if value already exists in the tree + */ bool insert_number(tree_node **rootptr, int value); -//find numbers into the node +/** + * Finds value in the tree + * @param root pointer to the root of the tree + * @param value value to find + * @return true if value was found, false if value was not found + */ bool find_number(tree_node *root, int value); -//delete numbers and reorganise tree +/** + * Deletes value from the tree + * @param rootptr pointer to the pointer to the root of the tree + * @param value value to delete + * @return true if value was deleted, false if value was not found + */ bool delete_number(tree_node **rootptr, int value);