2020-07-22 15:37:28 +03:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief A basic unbalanced binary search tree implementation in C.
|
|
|
|
* @details The implementation has the following functionalities implemented:
|
|
|
|
* - Insertion
|
|
|
|
* - Deletion
|
|
|
|
* - Search by key value
|
|
|
|
* - Listing of node keys in order of value (from left to right)
|
|
|
|
*/
|
2017-10-14 09:31:40 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Node, the basic data structure in the tree */
|
2020-05-29 23:23:24 +03:00
|
|
|
typedef struct node
|
|
|
|
{
|
2020-07-22 15:37:28 +03:00
|
|
|
struct node *left; /**< left child */
|
|
|
|
struct node *right; /**< right child */
|
|
|
|
int data; /**< data of the node */
|
2017-10-14 09:31:40 +03:00
|
|
|
} node;
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** The node constructor, which receives the key value input and returns a node
|
|
|
|
* pointer
|
|
|
|
* @param data data to store in a new node
|
|
|
|
* @returns new node with the provided data
|
|
|
|
* @note the node must be deleted before program terminates to avoid memory
|
|
|
|
* leaks
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
node *newNode(int data)
|
|
|
|
{
|
2018-03-18 23:49:50 +03:00
|
|
|
// creates a slug
|
2020-05-29 23:23:24 +03:00
|
|
|
node *tmp = (node *)malloc(sizeof(node));
|
2018-03-18 23:49:50 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
// initializes the slug
|
|
|
|
tmp->data = data;
|
|
|
|
tmp->left = NULL;
|
|
|
|
tmp->right = NULL;
|
2017-10-14 09:31:40 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
return tmp;
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Insertion procedure, which inserts the input key in a new node in the tree
|
|
|
|
* @param root pointer to parent node
|
|
|
|
* @param data value to store int he new node
|
|
|
|
* @returns pointer to parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
node *insert(node *root, int data)
|
|
|
|
{
|
|
|
|
// If the root of the subtree is null, insert key here
|
|
|
|
if (root == NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
root = newNode(data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data > root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
|
|
|
// If it isn't null and the input key is greater than the root key,
|
|
|
|
// insert in the right leaf
|
2020-05-29 23:23:24 +03:00
|
|
|
root->right = insert(root->right, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data < root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // If it isn't null and the input key is lower than the root key, insert
|
|
|
|
// in the left leaf
|
2020-05-29 23:23:24 +03:00
|
|
|
root->left = insert(root->left, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
// Returns the modified tree
|
|
|
|
return root;
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Utilitary procedure to find the greatest key in the left subtree
|
|
|
|
* @param root pointer to parent node
|
|
|
|
* @returns pointer to parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
node *getMax(node *root)
|
|
|
|
{
|
|
|
|
// If there's no leaf to the right, then this is the maximum key value
|
2020-07-22 15:37:28 +03:00
|
|
|
if (root->right != NULL)
|
|
|
|
{
|
|
|
|
return getMax(root->right);
|
|
|
|
}
|
|
|
|
return root;
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Deletion procedure, which searches for the input key in the tree and removes
|
|
|
|
* it if present
|
|
|
|
* @param root pointer to parent node
|
|
|
|
* @param data value to search for int the node
|
|
|
|
* @returns pointer to parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
node *delete (node *root, int data)
|
|
|
|
{
|
|
|
|
// If the root is null, nothing to be done
|
|
|
|
if (root == NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
return root;
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data > root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // If the input key is greater than the root's, search in the right
|
|
|
|
// subtree
|
2020-05-29 23:23:24 +03:00
|
|
|
root->right = delete (root->right, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data < root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // If the input key is lower than the root's, search in the left subtree
|
2020-05-29 23:23:24 +03:00
|
|
|
root->left = delete (root->left, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data == root->data)
|
|
|
|
{
|
2020-07-22 15:37:28 +03:00
|
|
|
// If the input key matches the root's, check the following cases
|
|
|
|
// termination condition
|
2020-05-29 23:23:24 +03:00
|
|
|
if ((root->left == NULL) && (root->right == NULL))
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // Case 1: the root has no leaves, remove the node
|
2020-05-29 23:23:24 +03:00
|
|
|
free(root);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if (root->left == NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // Case 2: the root has one leaf, make the leaf the new root and
|
|
|
|
// remove
|
|
|
|
// the old root
|
2020-05-29 23:23:24 +03:00
|
|
|
node *tmp = root;
|
|
|
|
root = root->right;
|
|
|
|
free(tmp);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
else if (root->right == NULL)
|
|
|
|
{
|
|
|
|
node *tmp = root;
|
|
|
|
root = root->left;
|
|
|
|
free(tmp);
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
else
|
2020-07-22 15:37:28 +03:00
|
|
|
{ // Case 3: the root has 2 leaves, find the greatest key in the left
|
|
|
|
// subtree and switch with the root's
|
|
|
|
|
2018-03-18 23:49:50 +03:00
|
|
|
// finds the biggest node in the left branch.
|
2020-05-29 23:23:24 +03:00
|
|
|
node *tmp = getMax(root->left);
|
|
|
|
|
|
|
|
// sets the data of this node equal to the data of the biggest node
|
|
|
|
// (lefts)
|
|
|
|
root->data = tmp->data;
|
|
|
|
root->left = delete (root->left, tmp->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Search procedure, which looks for the input key in the tree and returns 1 if
|
|
|
|
* it's present or 0 if it's not in the tree
|
|
|
|
* @param root pointer to parent node
|
|
|
|
* @param data value to store int he new node
|
|
|
|
* @returns 0 if value not found in the nodes
|
|
|
|
* @returns 1 if value was found
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
int find(node *root, int data)
|
|
|
|
{
|
|
|
|
// If the root is null, the key's not present
|
|
|
|
if (root == NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
return 0;
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data > root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
|
|
|
// If the input key is greater than the root's, search in the right
|
|
|
|
// subtree
|
2020-05-29 23:23:24 +03:00
|
|
|
return find(root->right, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data < root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
|
|
|
// If the input key is lower than the root's, search in the left subtree
|
2020-05-29 23:23:24 +03:00
|
|
|
return find(root->left, data);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else if (data == root->data)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
|
|
|
// If the input and the root key match, return 1
|
2020-05-29 23:23:24 +03:00
|
|
|
return 1;
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // unknown result!!
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Utilitary procedure to measure the height of the binary tree
|
|
|
|
* @param root pointer to parent node
|
|
|
|
* @param data value to store int he new node
|
|
|
|
* @returns 0 if value not found in the nodes
|
|
|
|
* @returns height of nodes to get to data from parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
int height(node *root)
|
|
|
|
{
|
|
|
|
// If the root is null, this is the bottom of the tree (height 0)
|
|
|
|
if (root == NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
return 0;
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Get the height from both left and right subtrees to check which is
|
|
|
|
// the greatest
|
|
|
|
int right_h = height(root->right);
|
|
|
|
int left_h = height(root->left);
|
|
|
|
|
|
|
|
// The final height is the height of the greatest subtree(left or right)
|
|
|
|
// plus 1(which is the root's level)
|
|
|
|
if (right_h > left_h)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
return (right_h + 1);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
else
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
return (left_h + 1);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
}
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Utilitary procedure to free all nodes in a tree
|
|
|
|
* @param root pointer to parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
void purge(node *root)
|
|
|
|
{
|
|
|
|
if (root != NULL)
|
|
|
|
{
|
|
|
|
if (root->left != NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
purge(root->left);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
if (root->right != NULL)
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
purge(root->right);
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
free(root);
|
2020-07-22 15:37:28 +03:00
|
|
|
root = NULL; // reset pointer
|
2020-05-29 23:23:24 +03:00
|
|
|
}
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Traversal procedure to list the current keys in the tree in order of value
|
|
|
|
* (from the left to the right)
|
|
|
|
* @param root pointer to parent node
|
|
|
|
*/
|
2020-05-29 23:23:24 +03:00
|
|
|
void inOrder(node *root)
|
|
|
|
{
|
|
|
|
if (root != NULL)
|
|
|
|
{
|
|
|
|
inOrder(root->left);
|
|
|
|
printf("\t[ %d ]\t", root->data);
|
|
|
|
inOrder(root->right);
|
|
|
|
}
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|
|
|
|
|
2020-07-22 15:37:28 +03:00
|
|
|
/** Main funcion */
|
|
|
|
int main()
|
2020-05-29 23:23:24 +03:00
|
|
|
{
|
2018-03-18 23:49:50 +03:00
|
|
|
// this reference don't change.
|
|
|
|
// only the tree changes.
|
2020-05-29 23:23:24 +03:00
|
|
|
node *root = NULL;
|
|
|
|
int opt = -1;
|
|
|
|
int data = 0;
|
2017-10-14 09:31:40 +03:00
|
|
|
|
2018-03-18 23:49:50 +03:00
|
|
|
// event-loop.
|
2020-05-29 23:23:24 +03:00
|
|
|
while (opt != 0)
|
|
|
|
{
|
2020-06-28 18:25:37 +03:00
|
|
|
printf(
|
|
|
|
"\n\n[1] Insert Node\n[2] Delete Node\n[3] Find a Node\n[4] Get "
|
|
|
|
"current Height\n[5] Print Tree in Crescent Order\n[0] Quit\n");
|
|
|
|
scanf("%d", &opt); // reads the choice of the user
|
2017-10-14 09:31:40 +03:00
|
|
|
|
2018-03-18 23:49:50 +03:00
|
|
|
// processes the choice
|
2020-05-29 23:23:24 +03:00
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
printf("Enter the new node's value:\n");
|
|
|
|
scanf("%d", &data);
|
|
|
|
root = insert(root, data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
printf("Enter the value to be removed:\n");
|
|
|
|
if (root != NULL)
|
|
|
|
{
|
|
|
|
scanf("%d", &data);
|
|
|
|
root = delete (root, data);
|
|
|
|
}
|
|
|
|
else
|
2020-07-22 15:37:28 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Tree is already empty!\n");
|
2020-07-22 15:37:28 +03:00
|
|
|
}
|
2020-05-29 23:23:24 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
printf("Enter the searched value:\n");
|
|
|
|
scanf("%d", &data);
|
|
|
|
find(root, data) ? printf("The value is in the tree.\n")
|
|
|
|
: printf("The value is not in the tree.\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
printf("Current height of the tree is: %d\n", height(root));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
inOrder(root);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-14 09:31:40 +03:00
|
|
|
|
2018-03-18 23:49:50 +03:00
|
|
|
// deletes the tree from the heap.
|
2020-05-29 23:23:24 +03:00
|
|
|
purge(root);
|
2020-07-22 15:37:28 +03:00
|
|
|
|
|
|
|
return 0;
|
2017-10-14 09:31:40 +03:00
|
|
|
}
|