Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below
3. Snippet (LO1, LO2, LO3 - 20%)
Given snippet code below that you are required to complete. You are not allowed to make
a new function or change any given code. Please complete each section that are marked
with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your
output should have the same result with the given output below.
Description:
a. search()
This function is built for searching whether the data has the same value as the node
in the existing Binary Search Tree. If it is found then print “found”, otherwise print
“not found”
b. insert()
This function is built for inserting a new node in the existing Binary Search Tree
c. inorder_traversal ()
This function is built for printing out the existing Binary Search Tree in inorder way.
d. preorder_traversal ()
This function is built for printing out the existing Binary Search Tree in preorder way.
e. postorder_traversal ()
This function is built for printing out the existing Binary Search Tree in postorder
way.
f. executeDeleteNode ()
This function is built for deleting a node in the existing Binary Search Tree. If the
node is either root or parent, then find replacement for the node otherwise delete
the leaf node.
g. deleteNode()
This function is built for searching the node in the existing Binary Search Tree that
want to be deleted
h. minValue()
This function is built for searching the minimum value in the existing Binary Search
Tree.
i. maxValue()
This function is built for searching the maximum value in the existing Binary Search
Tree.
j. count()
This function is built for counting how many nodes in the existing Binary Search Tree
k. height_of_binary_tree()
This function is built for knowing the height of the existing Binary Search Tree
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node {
int key;
struct node *left;
struct node *right;
} *root;
void search(struct node* curr, int find) {
//INSERT YOUR CODE HERE
}
void insert(struct node* curr, int val) {
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->key = val;
new_node->left = NULL;
new_node->right = NULL;
if (root == NULL) {
root = new_node;
}
else if (val != curr->key) {
if (val < curr->key && curr->left == NULL) {
curr->left = new_node;
}
else if (val > curr->key && curr->right == NULL) {
curr->right = new_node;
}
else if (val < curr->key && curr->left != NULL) {
insert(curr->left, val);
}
else if (val > curr->key && curr->right != NULL) {
insert(curr->right, val);
}
}
}
void inorder_traversal(struct node* curr) {
if (curr == NULL) return;
inorder_traversal(curr->left);
printf("%d ", curr->key);
inorder_traversal(curr->right);
}
void preorder_traversal(struct node* curr) {
//INSERT YOUR CODE HERE
}
void postorder_traversal(struct node* curr) {
//INSERT YOUR CODE HERE
}
void executeDeleteNode(struct node* parent, struct node* curr) {
//INSERT YOUR CODE HERE
}
void deleteNode(struct node* curr, int find) {
struct node *parent;
parent = curr;
while (curr != NULL && curr->key != find) {
if (find < curr->key) {
parent = curr;
curr = curr->left;
}
else if (find > curr->key) {
parent = curr;
curr = curr->right;
}
}
if (curr == NULL) {
printf("%d is not found");
}
else if (curr->key == find) {
executeDeleteNode(parent, curr);
}
}
int minValue(struct node* node) {
//INSERT YOUR CODE HERE
}
int maxValue(struct node* node){
//INSERT YOUR CODE HERE
}
int count(struct node* node){
//INSERT YOUR CODE HERE
}
int height_of_binary_tree(struct node *node){
//INSERT YOUR CODE HERE
}
int main() {
root = NULL;
insert(root, 54);
insert(root, 81);
insert(root, 16);
insert(root, 27);
insert(root, 9);
insert(root, 36);
insert(root, 63);
insert(root, 72);
printf("Total node in BST: %d \n",count(root));
printf("Height of binary tree : %d\n ", height_of_binary_tree(root));
printf("\nInorder: "); inorder_traversal(root);
printf("\nPreorder: "); preorder_traversal(root);
printf("\nPostorder: "); postorder_traversal(root);
puts("");
deleteNode(root, 54);
printf("\nPreorder (after del 54): "); preorder_traversal(root);
deleteNode(root, 9);
printf("\nPostorder (after del 9): "); postorder_traversal(root);
deleteNode(root, 81);
printf("\nInorder (after del 81): "); inorder_traversal(root);
puts("");
printf("Min value: %d \n", minValue(root));
printf("Max value: %d \n", maxValue(root));
puts("");
search(root, 10);
search(root, 8);
_getch();
return 0;
}
PLEASE HELP SIR, THX.
Step by step
Solved in 2 steps