C programming. Answer question only in C code In the eighth part, you have to implement a
binary search tree. The tree must satisfy the binary search tree property: the key in each node
must be greater than all keys stored in the left sub-tree, and smaller than all keys in right sub-
tree. You have to dynamically allocate space for each node and free the space for the nodes at the
end of the program. This program takes a file name as an argument from the command line. The
file is either blank or contains successive lines of input. Each line starts with a character, either
\'i\' or \'s\' followed by a tab and then an integer. For each line that starts with V, your program
should insert that number in the binary search tree if it is not already there. If it is already
present, you will print \"duplicate\" and not change the tree. If the line starts with a \'s\' your
program should search for the value. Output format: For each fine in the input file, your
program should print the status/result of the operation. For an insert operation, the program
should print either \"inserted\" with a single space followed by a number, the height of the
inserted node in the tree, or \"duplicate\" if the value is already present in the tree. The height of
the root node is 1. For a search, the program should either print \"present\", followed by the
height of the node, or \"absent\" based on the outcome of the search. Your program should print
\"error\" (and nothing else) if the file does not exist or for input lines with improper structure.
Example Execution: Let\'s assume we have a file.txt with the following contents: 5 3 4 1 6 s
Executing the program in the following fashion should produce the output shown below:
$./eighth file1.txt inserted 1 inserted 2 inserted 3 inserted 3 inserted 2 present 3.
Solution
Above program only inserts data / nodes to the binary tree. To print the tree, we need to know
how to traverse a Binary Search Tree
C Program to implement Binary Search Tree Traversal
Preorder traversal sequence : F, B, A, D, C, E, G, I, H
(root, left, right)
Inorder traversal sequence : A, B, C, D, E, F, G, H, I
(left, root, right)
Postorder traversal sequence: A, C, E, D, B, H, I, G, F
(left, right, root)
Program :
# include
# include
# include
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
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\");
printf(\"\ 2.Search\");
printf(\"\ 3.Recursive Traversals\");
printf(\"\ 4.Exit\");
printf(\"\ Enter your choice :\");
scanf(\"%d\", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf(\"\ Enter The Element \");
scanf(\"%d\", &new_node->data);
if (root == NULL) /* Tree is not Created */
root =.
C programming. Answer question only in C code In the eighth part, yo.pdf
1. C programming. Answer question only in C code In the eighth part, you have to implement a
binary search tree. The tree must satisfy the binary search tree property: the key in each node
must be greater than all keys stored in the left sub-tree, and smaller than all keys in right sub-
tree. You have to dynamically allocate space for each node and free the space for the nodes at the
end of the program. This program takes a file name as an argument from the command line. The
file is either blank or contains successive lines of input. Each line starts with a character, either
'i' or 's' followed by a tab and then an integer. For each line that starts with V, your program
should insert that number in the binary search tree if it is not already there. If it is already
present, you will print "duplicate" and not change the tree. If the line starts with a 's' your
program should search for the value. Output format: For each fine in the input file, your
program should print the status/result of the operation. For an insert operation, the program
should print either "inserted" with a single space followed by a number, the height of the
inserted node in the tree, or "duplicate" if the value is already present in the tree. The height of
the root node is 1. For a search, the program should either print "present", followed by the
height of the node, or "absent" based on the outcome of the search. Your program should print
"error" (and nothing else) if the file does not exist or for input lines with improper structure.
Example Execution: Let's assume we have a file.txt with the following contents: 5 3 4 1 6 s
Executing the program in the following fashion should produce the output shown below:
$./eighth file1.txt inserted 1 inserted 2 inserted 3 inserted 3 inserted 2 present 3.
Solution
Above program only inserts data / nodes to the binary tree. To print the tree, we need to know
how to traverse a Binary Search Tree
C Program to implement Binary Search Tree Traversal
Preorder traversal sequence : F, B, A, D, C, E, G, I, H
(root, left, right)
Inorder traversal sequence : A, B, C, D, E, F, G, H, I
(left, root, right)
Postorder traversal sequence: A, C, E, D, B, H, I, G, F
(left, right, root)
Program :
# include
# include
# include
2. typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
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");
printf(" 2.Search");
printf(" 3.Recursive Traversals");
printf(" 4.Exit");
printf(" Enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf(" Enter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf(" Want To enter More Elements?(y/n)");
3. ans = getch();
} while (ans == 'y');
break;
case 2:
printf(" Enter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf(" Parent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf(" The Inorder display : ");
inorder(root);
printf(" The Preorder display : ");
preorder(root);
printf(" The Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
4. */
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf(" The %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
5. This function displays the tree in inorder fashion
*/
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}
Explanation :
if(root == NULL)
# include
# include
# include
typedef struct BST {
6. int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
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");
printf(" 2.Search");
printf(" 3.Recursive Traversals");
printf(" 4.Exit");
printf(" Enter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf(" Enter The Element ");
scanf("%d", &new_node->data);
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();
7. } while (ans == 'y');
break;
case 2:
printf(" Enter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf(" Parent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)
printf("Tree Is Not Created");
else {
printf(" The Inorder display : ");
inorder(root);
printf(" The Preorder display : ");
preorder(root);
printf(" The Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
/*
Get new Node
*/
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*
This function is for creating a binary search tree
*/
8. void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
/*
This function is for searching the node from
binary Search Tree
*/
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf(" The %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
/*
This function displays the tree in inorder fashion
9. */
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);
}
}
/*
This function displays the tree in preorder fashion
*/
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
/*
This function displays the tree in postorder fashion
*/
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}