Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Build a program that will take the following values and store them in a binary s

ID: 3780275 • Letter: B

Question

Build a program that will take the following values and store them in a binary search tree. 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49 Have your program display the binary search tree in a tree form. Have your program display the nodes of the binary tree in preorder, postorder and inorder. There is also an example of this in the binary tree example program. Use the same data above to build a heap. Have your program display the heap in a tree form just as it did for the binary search tree.

Explanation / Answer

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

typedef struct BST {          
int data;
struct BST *left_child, *right_child;
} node;

void insert(node *, node *);               //each function declarations
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);

void main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
clrscr();

printf(" Program For Binary Search Tree ");
do {
printf(" 1.Create BST");
printf(" 2.Recursive Traversals");
printf(" 3.Exit");
printf(" Enter your choice :");
scanf("%d", &choice);

switch (choice) {
case 1:                           //creating node
do {
new_node = get_node();
printf(" Enter The Element ");
scanf("%d", &new_node->data);           //prompting user to enter input value

if (root == NULL) //Tree is not Created
root = new_node;
else
insert(root, new_node);

printf(" Want To enter More Elements?(y/n)");
ans = getch();
} while (ans == 'y');
break;


case 2:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf(" The Inorder display : ");           //prints Inorder values
inorder(root);
printf(" The Preorder display : ");       //Print preorder values
preorder(root);
printf(" The Postorder display : ");       //Print postorder values
postorder(root);
}
break;
}
} while (choice != 4);
}

node *get_node() {                       //getting node
node *temp;
temp = (node *) malloc(sizeof(node));
temp->left_child = NULL;
temp->right_child = NULL;
return temp;
}


void insert(node *root, node *new_node) {       // creating a binary search tree

if (new_node->data < root->data) {
if (root->left_child == NULL)
root->left_child = new_node;
else
insert(root->left_child, new_node);
}

if (new_node->data > root->data) {
if (root->right_child == NULL)
root->right_child = new_node;
else
insert(root->right_child, new_node);
}
}

void inorder(node *temp) {           //This function displays the tree in inorder fashion
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->right_child);
}
}

void preorder(node *temp) {           // This function displays the tree in preorder fashion
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->left_child);
preorder(temp->right_child);
}
}

void postorder(node *temp) {           // This function displays the tree in postorder fashion

if (temp != NULL) {
postorder(temp->left_child);
postorder(temp->right_child);
printf("%d", temp->data);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote