Skip to content

Commit

Permalink
(refactor) changes due to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
LenaMarochkina committed Oct 15, 2022
1 parent 3b81348 commit c2ab24d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 64 deletions.
102 changes: 46 additions & 56 deletions src/bvs.c
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
#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;
}
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);
}
}

Expand All @@ -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);

}
}
37 changes: 29 additions & 8 deletions src/bvs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


0 comments on commit c2ab24d

Please sign in to comment.