Assignment 1
Assignment 1
Rollno : 18
Batch : 1
Assignment 1. Binary Search Tree and Traversals
SET A
a) Implement a Binary search tree (BST) library (btree.h) with operations – create, search, insert,
inorder, preorder and postorder. Write a menu driven program that performs the above operations.
#include <stdio.h>
#include <stdlib.h>
typedef struct BST
{
struct BST *Lchild;
struct BST *Rchild;
int data;
} BST;
BST *create()
{
BST *temp, *root = NULL;
char ch;
int t;
printf("Enter how many nodes?: ");
int nn;
scanf(" %d", &nn);
printf("Enter data: ");
scanf("%d", &t);
root = (BST *)malloc(sizeof(BST));
root->Lchild = root->Rchild = NULL;
root->data = t;
nn = nn - 1;
for (int i = 0; i < nn; i++)
{
printf("Enter data: ");
scanf(" %d", &t);
temp = root;
while (1)
{
if (t < temp->data)
{
if (temp->Lchild == NULL)
{
BST *newnode = (BST *)malloc(sizeof(BST));
newnode->data = t;
newnode->Lchild = newnode->Rchild = NULL;
temp->Lchild = newnode;
break;
}
else
{
temp = temp->Lchild;
}
}
else if (t > temp->data)
{
if (temp->Rchild == NULL)
{
BST *newnode = (BST *)malloc(sizeof(BST));
newnode->data = t;
newnode->Lchild = newnode->Rchild = NULL;
temp->Rchild = newnode;
break;
}
else
{
temp = temp->Rchild;
}
}
else if (t == temp->data)
{
printf("Same Same\n");
break;
}
}
}
return root;
}
int main() {
BST *root;
int ch , t;
while(1){
printf("\n*WELCOME*\n");
printf("\n1.create\n2.search\n3.insert\n4.inorder\n5.preorder\n6.postorder\n");
printf("\nEnter your choice: ");
scanf("%d" , &ch);
switch(ch){
case 1:
root = create();
printf("\nBST CREATED\n");
break;
case 2:
printf("\nEnter data to search: ");
scanf("%d", &t);
search(root , t);
break;
case 3:
printf("\nEnter data to insert: ");
scanf("%d" , &t);
insert(root , t);
break;
case 4:
printf("\nInorder:\n");
inorder(root);
break;
case 5:
printf("\nPreorder\n");
preorder(root);
break;
case 6:
printf("\nPostorder\n");
postorder(root);
break;
default:
printf("\nEnter valid option!!\n");
exit(0);
}
}
return 0;
}
OUTPUT:
*WELCOME*
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
Enter your choice: 1
Enter how many nodes?: 7
Enter data: 30
Enter data: 18
Enter data: 28
Enter data: 45
Enter data: 40
Enter data: 35
Enter data: 10
BST CREATED
*WELCOME*
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
*WELCOME*
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
Enter your choice: 3
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
b) Write a program which uses binary search tree library and counts the total nodes and total
leaf nodes in the tree.
int count(T) – returns the total number of nodes from BST
int countLeaf(T) – returns the total number of leaf nodes from BST
#include<stdio.h>
#include<stdlib.h>
BST *create()
{
BST *temp, *root = NULL;
char ch;
int t;
printf("Enter how many nodes?: ");
int nn;
scanf(" %d", &nn);
printf("Enter data: ");
scanf("%d", &t);
root = (BST *)malloc(sizeof(BST));
root->Lchild = root->Rchild = NULL;
root->data = t;
nn = nn - 1;
for (int i = 0; i < nn; i++)
{
printf("Enter data: ");
scanf(" %d", &t);
temp = root;
while (1)
{
if (t < temp->data)
{
if (temp->Lchild == NULL)
{
BST *newnode = (BST *)malloc(sizeof(BST));
newnode->data = t;
newnode->Lchild = newnode->Rchild = NULL;
temp->Lchild = newnode;
break;
}
else
{
temp = temp->Lchild;
}
}
else if (t > temp->data)
{
if (temp->Rchild == NULL)
{
BST *newnode = (BST *)malloc(sizeof(BST));
newnode->data = t;
newnode->Lchild = newnode->Rchild = NULL;
temp->Rchild = newnode;
break;
}
else
{
temp = temp->Rchild;
}
}
else if (t == temp->data)
{
printf("Same Same\n");
break;
}
}
}
return root;
}
int main() {
BST *root;
int ch , t;
char c;
while(1){
printf("\n*WELCOME*\n");
printf("\n1.create\n2.count total number of nodes\n3.count toatl number of leaf nodes\n");
printf("\nEnter your choice: ");
scanf("%d" , &ch);
switch(ch){
case 1:
printf("\nCREATING BINARY SEARCH TREE\n");
root = create();
break;
case 2:
printf("\nTotal number of nodes: %d\n" , count(root));
break;
case 3:
printf("\nTotal number of leaf nodes: %d\n" , count_leaf(root));
break;
default:
printf("\nEnter valid option!!\n");
exit(0);
}
}
return 0;
}
OUTPUT:
*WELCOME*
1.create
2.count total number of nodes
3.count toatl number of leaf nodes
1.create
2.count total number of nodes
3.count toatl number of leaf nodes
*WELCOME*
1.create
2.count total number of nodes
3.count toatl number of leaf nodes
SET B
a) Write a C program which uses Binary search tree library and implements following function
with recursion:
T copy(T) – create another BST which is exact copy of BST which is passed as parameter.
int compare(T1, T2) – compares two binary search trees and returns 1 if they are equal and 0
otherwise.
#include<stdio.h>
#include<stdlib.h>
int main() {
BST *root = init(5);
insert(root , 3);
insert(root , 6);
preorder(root);
printf("\n");
BST *newroot = copy(root);
preorder(newroot);
printf("\n");
printf("\n%d\n" , compare(root , newroot));
return 0;
}
OUTPUT:
536
536
_________________________________________________________________
SET C
a) Write a C program which uses Binary search tree library and implements following two functions:
int sumodd(T) – returns sum of all odd numbers from BST
int sumeven(T) – returns sum of all even numbers from BST
mirror(T) – converts given tree into its mirror image.
#include<stdio.h>
#include<stdlib.h>
int main() {
BST *root = init(5);
insert(root , 3);
insert(root , 6);
printf("SUM OF ODD NUMBERS:%d\n" , sumOdd(root));
printf("SUM OF EVEN NUMBERS:%d\n" , sumEven(root));
BST *newroot = mirror(root);
printf("MIRROR IMAGE NODES:");
preorder(newroot);
return 0;
}
OUTPUT:
SUM OF ODD NUMBERS:8
SUM OF EVEN NUMBERS:6
MIRROR IMAGE NODES:5 6 3
c) What modifications are required in search function to count the number of comparisons required?