Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) binary search tree addition #2

Merged
merged 7 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/bvs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Implementace překladače imperativního jazyka IFJ22.
* @authors
* xmaroc00, Elena Marochkina
*
* @file bvs.c
* @brief Binary search tree
* @date 21.10.2022
*/

#include "bvs.h"

tree_node *create_node(int value) {
tree_node *result = (tree_node *) malloc(sizeof(tree_node));
if (result == 0) {
INTERNAL_ERROR("Malloc for BST failed");
}
result->left = NULL;
result->right = NULL;
result->value = value;

return result;
}

int comparator(tree_node *root, int value) {
if (root->value == value) {
return 0;
}
if (root->value > value) {
return -1;
}
return 1;
}

bool insert_number(tree_node **rootptr, int value) {
tree_node *root = *rootptr;
if (root == NULL) {
//tree empty
(*rootptr) = create_node(value);
return true;
}
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) {
if (root == NULL) {
return false;
}
switch (comparator(root, value)) {
case 0:
return true;
case -1:
return find_number(root->left, value);
case 1:
return find_number(root->right, value);
}
}

bool delete_number(tree_node **rootptr, int value) {
tree_node *root = *rootptr;

if (root == NULL) {
return false;
}
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;
return delete_number(&(root->right), temp->value);
case -1:
return delete_number(&(root->left), value);
case 1:
return delete_number(&(root->right), value);

}
}
65 changes: 65 additions & 0 deletions src/bvs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Implementace překladače imperativního jazyka IFJ22.
* @authors
* xmaroc00, Elena Marochkina
*
* @file bvs.h
* @brief Binary search tree
* @date 21.10.2022
*/

#ifndef IFJ_PROJ_2022_BVS_H
#define IFJ_PROJ_2022_BVS_H

#endif //IFJ_PROJ_2022_BVS_H

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct tree_node {
int value;
struct tree_node *left;
struct tree_node *right;
} tree_node;

/**
* Creates tree node with value
* @param value value in the node
* @return pointer to the tree node
*/
tree_node *create_node(int value);

/**
* 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);

/**
* 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);

/**
* 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);

/**
* 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);