0% found this document useful (0 votes)
48 views16 pages

18bce0680 Dsa Lab Da-2

The document describes a C program that implements a binary search tree and includes functions for insertion, deletion, and traversing the tree using inorder, preorder, and postorder methods. It also includes functions for searching for a node and deleting the entire tree from memory. The program is tested by building a sample binary search tree and performing various traversals and searches on it.

Uploaded by

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

18bce0680 Dsa Lab Da-2

The document describes a C program that implements a binary search tree and includes functions for insertion, deletion, and traversing the tree using inorder, preorder, and postorder methods. It also includes functions for searching for a node and deleting the entire tree from memory. The program is tested by building a sample binary search tree and performing various traversals and searches on it.

Uploaded by

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

LAB ASSIGNMENT-2/LAB CAT-1 UPLOADFILE

NAME: B.SAI ROHIT


REG.NO:18BCE0680
BST AND BT
Q) Implementation of a Binary Search Tree and performing
Insertion, deletion, inorder, preorder and post order traversals
on it.
SOURCE CODE (in C language):
#include <stdio.h>

#include <stdlib.h>

struct btnode
{

int value;

struct btnode *l;


struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();
void insert();
void delete();

void inorder(struct btnode *t);


void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);

int flag = 1;

void main()

{
int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");


printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");

printf("5 - Postorder Traversal\n");

printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");

scanf("%d", &ch);

switch (ch)
{
case 1:

insert();
break;
case 2:
delete();
break;
case 3:

inorder(root);
break;
case 4:
preorder(root);

break;

case 5:
postorder(root);
break;

case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;

else
search(root);
}
void create()
{
int data;

printf("Enter data of node to be inserted : ");


scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;


}

void search(struct btnode *t)

{
if ((temp->value > t->value) && (t->r != NULL))
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL))


search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}
void inorder(struct btnode *t)
{
if (root == NULL)

{
printf("No elements in a tree to display");

return;
}
if (t->l != NULL)
inorder(t->l);

printf("%d -> ", t->value);


if (t->r != NULL)

inorder(t->r);
}

void delete()
{
int data;

if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}

printf("Enter the data to be deleted : ");


scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}

/* To find the preorder traversal */

void preorder(struct btnode *t)

{
if (root == NULL)
{
printf("No elements in a tree to display");
return;

}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);

if (t->r != NULL)

preorder(t->r);
}
void postorder(struct btnode *t)

{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;

}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}
void search1(struct btnode *t, int data)

{
if ((data>t->value))
{
t1 = t;
search1(t->r, data);
}

else if ((data < t->value))


{
t1 = t;
search1(t->l, data);

}
else if ((data==t->value))
{
delete1(t);

}
}
void delete1(struct btnode *t)
{
int k;

if ((t->l == NULL) && (t->r == NULL))


{
if (t1->l == t)
{
t1->l = NULL;
}

else
{

t1->r = NULL;
}
t = NULL;
free(t);
return;
}

else if ((t->r == NULL))


{
if (t1 == t)
{

root = t->l;
t1 = root;
}
else if (t1->l == t)

t1->l = t->l;

}
else

{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)

{
if (t1 == t)
{
root = t->r;
t1 = root;
}

else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;

t == NULL;

free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{

k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}

search1(root, k);
t->value = k;
}
}
int smallest(struct btnode *t)

{
t2 = t;
if (t->l != NULL)
{

t2 = t;
return(smallest(t->l));
}
else

return (t->value);
}
int largest(struct btnode *t)
{
if (t->r != NULL)

{
t2 = t;
return(largest(t->r));
}
else
return(t->value);
}

OUTPUT:
Insertion(choice-1) and Deletion(choice-2) of an element in Binary
Search Tree:

Inorder(choice-3),Preorder(choice-4) and Postorder(choice-5) Traversal


on Binary Search tree:
Q) Implementation of Binary tree and performing traversals on
it.
Source Code ( in C language):
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;

void insert(node ** tree, int val)


{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}

if(val < (*tree)->data)


{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}

void print_preorder(node * tree)


{
if (tree)
{
printf("%d\n",tree->data);
print_preorder(tree->left);
print_preorder(tree->right);
}

void print_inorder(node * tree)


{
if (tree)
{
print_inorder(tree->left);
printf("%d\n",tree->data);
print_inorder(tree->right);
}
}

void print_postorder(node * tree)


{
if (tree)
{
print_postorder(tree->left);
print_postorder(tree->right);
printf("%d\n",tree->data);
}
}

void deltree(node * tree)


{
if (tree)
{
deltree(tree->left);
deltree(tree->right);
free(tree);
}
}

node* search(node ** tree, int val)


{
if(!(*tree))
{
return NULL;
}

if(val < (*tree)->data)


{
search(&((*tree)->left), val);
}
else if(val > (*tree)->data)
{
search(&((*tree)->right), val);
}
else if(val == (*tree)->data)
{
return *tree;
}
}

void main()
{
node *root;
node *tmp;
//int i;

root = NULL;

insert(&root, 9);
insert(&root, 4);
insert(&root, 15);
insert(&root, 6);
insert(&root, 12);
insert(&root, 17);
insert(&root, 2);

printf("Pre Order Display\n");


print_preorder(root);

printf("In Order Display\n");


print_inorder(root);

printf("Post Order Display\n");


print_postorder(root);

tmp = search(&root, 4);


if (tmp)
{
printf("Searched node=%d\n", tmp->data);
}
else
{
printf("Data Not found in tree.\n");
}

deltree(root);
}

Output:

You might also like