0% found this document useful (0 votes)
51 views

Assignment 1

The document discusses implementing binary search trees (BSTs) in C including: 1) Creating a BST library with functions for create, search, insert, inorder, preorder and postorder traversals. 2) Writing a menu driven program to perform the BST operations. 3) Implementing functions to count the total nodes and leaf nodes in a BST. 4) Implementing functions to copy one BST to create an exact duplicate, and to compare two BSTs and return if they are equal.

Uploaded by

Deepak Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Assignment 1

The document discusses implementing binary search trees (BSTs) in C including: 1) Creating a BST library with functions for create, search, insert, inorder, preorder and postorder traversals. 2) Writing a menu driven program to perform the BST operations. 3) Implementing functions to count the total nodes and leaf nodes in a BST. 4) Implementing functions to copy one BST to create an exact duplicate, and to compare two BSTs and return if they are equal.

Uploaded by

Deepak Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Name: Deepak rai

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;
}

void insert(BST * root, int x)


{
BST *t = root;
BST *t1 = root; while (t != NULL)
{
if (x < t->data)
{
t1 = t;
t = t->Lchild;
}
else if (x > t->data)
{
t1 = t;
t = t->Rchild;
}
else if (x == t->data)
{
printf("\nSame Same\n");
break;
}
}
if (t == NULL)
{
BST *newnode = (BST *)malloc(sizeof(BST));
newnode->data = x;
newnode->Lchild = NULL;
newnode->Rchild = NULL;
if (t1->data < x)
{
t1->Rchild = newnode;
}
else if (t1->data > x)
{
t1->Lchild = newnode;
}
}
}

void search(BST * root, int x)


{
BST *t = root;
int flag = 0;
while (t != NULL)
{
if (x < t->data)
{
t = t->Lchild;
}
else if (x > t->data)
{
t = t->Rchild;
}
else if (x == t->data)
{
printf("\nFound!!\n");
flag = 1;
break;
}
}
if (flag == 0)
{
printf("\nNot Found\n");
}
}

void inorder(BST * root)


{
if (root != NULL)
{
inorder(root->Lchild);
printf("%d ", root->data);
inorder(root->Rchild);
}
}

void preorder(BST * root)


{
if (root != NULL)
{
printf("%d ", root->data);
preorder(root->Lchild);
preorder(root->Rchild);
}
}

void postorder(BST * root)


{
if (root != NULL)
{
postorder(root->Lchild);
postorder(root->Rchild);
printf("%d ", root->data);
}
}

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

Enter your choice: 2


Enter data to search: 40
Found!!

*WELCOME*

1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder
Enter your choice: 3

Enter data to insert: 80


*WELCOME*

1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder

Enter your choice: 4


Inorder:
10 18 28 30 35 40 45 80
*WELCOME*

1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder

Enter your choice: 5


Preorder
30 18 10 28 45 40 35 80
*WELCOME*
1.create
2.search
3.insert
4.inorder
5.preorder
6.postorder

Enter your choice: 6


Postorder
10 8 18 35 40 80 45 30

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>

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 count(BST *root){


if(root == NULL){
return 0;
}
return 1 + count(root->Lchild) + count(root->Rchild);
}

int count_leaf(BST *root){


int n = count(root);
n = (n / 2) ;
return n;
}

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

Enter your choice: 1


CREATING BINARY SEARCH TREE
Enter how many nodes?: 8
Enter data: 30
Enter data: 18
Enter data: 28
Enter data: 45
Enter data: 40
Enter data: 35
Enter data: 10
Enter data: 80
*WELCOME*

1.create
2.count total number of nodes
3.count toatl number of leaf nodes

Enter your choice: 2


Total number of nodes: 8

*WELCOME*

1.create
2.count total number of nodes
3.count toatl number of leaf nodes

Enter your choice: 3


Total number of leaf nodes: 4

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>

typedef struct BST{


struct BST *Lchild;
struct BST *Rchild;
int data;
}BST;

BST *init(int x){


BST *temp = (BST*)malloc(sizeof(BST));
temp->Lchild = NULL;
temp->Rchild = NULL;
temp->data = x;
return temp;
}

void insert(BST *root , int x){


BST *t = root;
BST *t1 = root;
while(t != NULL){
if(x < t->data){
t1 = t;
t = t->Lchild;
}else if(x > t->data){
t1 = t;
t = t->Rchild;
}else if(x == t->data){
printf("\nSame Same\n"); break;
}
}
if(t == NULL){
BST *newnode = (BST*)malloc(sizeof(BST));
newnode->data = x;
newnode->Lchild = NULL;
newnode->Rchild = NULL;
if(t1->data < x){
t1->Rchild = newnode;
} else if(t1->data > x){
t1->Lchild = newnode;
}
}
}

void preorder(BST *root){


if(root != NULL){
printf("%d " , root->data);
preorder(root->Lchild);
preorder(root->Rchild);
}
}

BST *copy(BST *root){


if(root == NULL){
return NULL;
}
BST *newnode = (BST*)malloc(sizeof(BST));
newnode->data = root->data;
newnode->Lchild = copy(root->Lchild);
newnode->Rchild = copy(root->Rchild);
return newnode;
}

int compare(BST *one , BST *two){


if(one == NULL && two == NULL){
return 1;
}
if(one->data != two->data) return 0;
return ((compare(one->Lchild , two->Lchild)) && (compare(one->Rchild , two->Rchild)));
}

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

1 //indicates that both tree are identical

_________________________________________________________________
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>

typedef struct BST{


struct BST *Lchild;
struct BST *Rchild;
int data;
}BST;

BST *init(int x){


BST *temp = (BST*)malloc(sizeof(BST));
temp->Lchild = NULL;
temp->Rchild = NULL;
temp->data = x;
return temp;
}
void insert(BST *root , int x){
BST *t = root;
BST *t1 = root;
while(t != NULL){
if(x < t->data){
t1 = t;
t = t->Lchild;
}else if(x > t->data){
t1 = t;
t = t->Rchild;
}else if(x == t->data){
printf("\nSame Same\n"); break;
}
}
if(t == NULL){
BST *newnode = (BST*)malloc(sizeof(BST));
newnode->data = x;
newnode->Lchild = NULL;
newnode->Rchild = NULL;
if(t1->data < x){
t1->Rchild = newnode;
} else if(t1->data > x){
t1->Lchild = newnode;
}
}
}

void preorder(BST *root){


if(root != NULL){
printf("%d " , root->data);
preorder(root->Lchild);
preorder(root->Rchild);
}
}

int sumOdd(BST *root){


int sum = 0;
if(root != NULL){
if((root->data % 2) != 0){
sum = sum + root->data;
}
sum = sum + sumOdd(root->Lchild);
sum = sum + sumOdd(root->Rchild);
}
return sum;
}

int sumEven(BST *root){


int sum = 0;
if(root != NULL){
if((root->data % 2) == 0){
sum = sum + root->data;
}
sum = sum + sumEven(root->Lchild);
sum = sum + sumEven(root->Rchild);
}
return sum;
}

BST *mirror(BST *root){


BST *newnode = (BST*)malloc(sizeof(BST));
if(root == NULL){
return NULL;
}
newnode->data = root->data;
newnode->Lchild = mirror(root->Rchild);
newnode->Rchild = mirror(root->Lchild);
return newnode;
}

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

b) Write a function to delete an element from BST.

BST *succ(BST *root){


BST *current = root;
if(current->Lchild==NULL)
{
return current;
}
while(current->Lchild->Lchild != NULL)
{
     current = current->Lchild;
}
BST *temp = current->Lchild;
current->Lchild = NULL;
return temp;
}

void delete(BST *root , int x){


BST *t = root;
int flag = 0;
BST *parent = root;
while((t != NULL) && (flag == 0)){
     if(x < t->data){
         parent = t;
         t = t->Lchild;
     }else if(x > t->data){
         parent = t;
         t = t->Rchild;
     }else if(x == t->data){
         flag = 1;
         break;
     }
}
if(flag == 0){
     printf("\nNot Found!!\n");
}else{
     //no child
     if((t->Lchild == NULL) && (t->Rchild == NULL)){
         if(parent->Lchild == t){
             parent->Lchild = NULL;
         }else if(parent->Rchild == t){
             parent->Rchild = NULL;
         }
         //two child
     }else if((t->Lchild != NULL) && (t->Rchild != NULL))
{
if(t->Rchild->Lchild==NULL && t->Rchild->Rchild==NULL)
{
t->data=t->Rchild->data;
free t->Rchild;
t->Rchild=NULL;
}
else
{
         BST *temp = succ(t->Rchild);
         t->data = temp->data;
     }
}//one child
else if(parent->Lchild == t){
         if(t->Lchild == NULL){
             parent->Lchild = t->Rchild;
         }else if(t->Rchild == NULL){
             parent->Lchild = t->Lchild;
         }
     }else if(parent->Rchild == t){
         printf("hello\n");
         if(t->Lchild == NULL){
             parent->Rchild = t->Rchild;
         }else if(t->Rchild == NULL){
             parent->Rchild = t->Lchild;
         }
     }
}
}

c) What modifications are required in search function to count the number of comparisons required?

int search(BST *root , int x){


BST *t = root;
int flag = 0;
int com;
while(t!=NULL){
     if(x < t->data){
         com++;
         t = t->Lchild;
     }else if(x > t->data){
         com++;
         t = t->Rchild;
     }else if(x == t->data){
         com++;
         printf("\nFound!!\n");
         flag = 1;
         break;
     }
}
if(flag == 0){
     printf("\nNot Found\n");
}
return com;
}

You might also like