0% found this document useful (0 votes)
17 views12 pages

BST - IMPLEMENTATION

The document outlines an experiment for implementing a Binary Search Tree (BST) using a linked list in C, focusing on insertion, deletion, and traversal operations. It includes the theoretical background of BSTs, algorithms for various operations, and a complete source code implementation. The experiment aims to enhance understanding of data structures and their functionalities.
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)
17 views12 pages

BST - IMPLEMENTATION

The document outlines an experiment for implementing a Binary Search Tree (BST) using a linked list in C, focusing on insertion, deletion, and traversal operations. It includes the theoretical background of BSTs, algorithms for various operations, and a complete source code implementation. The experiment aims to enhance understanding of data structures and their functionalities.
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/ 12

FR.

CONCEICAO RODRIGUES COLLEGE OF ENGINEERING


Department of Electronics and Computer Science
Class: S.E. ECS(Semester IV) Subject Name: Data Structures (VSE12EC03)
Name of the Student: Chris Coutinho
Experiment No 9 Roll Number 10113
Date of Performance Date of Submission
Experiment Title Implement Binary Search Tree ADT using Linked List
CO Mapping VSE12EC03

Problem Statement:
Write C Program to implement binary search tree.

Objective of the Experiment:


Understand insertion, deletion and traversal in binary search tree
using linked list

Theory:
​ Binary Search Tree, is a node-based binary tree data structure which has the

following properties:
♣The left subtree of a node contains only nodes with keys less than the
node’s key.
♣ The right subtree of a node contains only nodes with keys greater than the
node’s key.
♣ The left and right subtree each must also be a binary search tree. There
must be no duplicate nodes

The above properties of Binary Search Tree provide an ordering among keys
so that the operations like search, minimum and maximum can be done fast.
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
If there is no ordering, then we may have to compare every key to search a
given key.
Operations on Binary search Tree:
1) Insertion of new node
2) Deletion of existing node
3) Traversal

Algorithm:
1)​ Insertion of a key
A new key is always inserted at leaf. We start searching a key from root till
we hit a leaf node. Once a leaf node is found, the new node is added as a
child of the leaf node

2)​ Deletion of node from BST


When we delete a node, there possibilities arise.
1)​ Node to be deleted is leaf: Simply remove from the tree.

2)​ Node to be deleted has only one child: Copy the child to the node and
delete the child
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science

3)​ Node to be deleted has two children: Find inorder successor of the
node. Copy contents of the inorder successor to the node and delete
the inorder successor. Note that inorder predecessor can also be used.

The important thing to note is, inorder successor is needed only when
right child is not empty. In this particular case, inorder successor can
be obtained by finding the minimum value in right child of the node.

4)​ Traversal
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc)
which have only one logical way to traverse them, trees can be
traversed in different ways. Following are the generally used ways for
traversing trees
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
Depth First Traversals:
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
Breadth First or Level Order Traversal : 1 2 3 4 5

a)​ Inorder Traversal:


Algorithm Inorder(root)
1. Traverse the left subtree, i.e., call Inorder(left-subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right-subtree)

b) Postorder Traversal
Algorithm Postorder(root)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root.

b)​ Preorder Traversal


Algorithm Preorder(root)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Source Code:

#include <stdio.h>
#include <stdlib.h>
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
struct node {
int data;
struct node *left, *right;
};

struct node *root = NULL;

struct node *create_node(int data) {


struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (!new_node) {
printf("Node not created\n");
return NULL;
}
new_node->data = data;
new_node->left = new_node->right = NULL;
return new_node;
}

void insert(int data) {


struct node *new_node = create_node(data);
if (!new_node) return;

if (root == NULL) {
root = new_node;
return;
}

struct node *node2 = root;


struct node *node1 = NULL;
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
while (node2 != NULL) {
node1 = node2;
if (data < node2->data)
node2 = node2->left;
else
node2 = node2->right;
}

if (data < node1->data)


node1->left = new_node;
else
node1->right = new_node;
}

void inorder(struct node *root) {


if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

int search(struct node *root, int key) {


if (root == NULL)
return 0;
if (key == root->data)
return 1;
else if (key < root->data)
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
return search(root->left, key);
else
return search(root->right, key);
}

struct node* findMin(struct node *root) {


while (root->left != NULL)
root = root->left;
return root;
}

struct node* delete(struct node *root, int key) {


if (root == NULL)
return NULL;

if (key < root->data)


root->left = delete(root->left, key);
else if (key > root->data)
root->right = delete(root->right, key);
else {
// Node found
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
else if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
}
else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
} else {
struct node *successor = findMin(root->right);
root->data = successor->data;
root->right = delete(root->right, successor->data);
}
}
return root;
}

int main() {
int choice, value;

while (1) {
printf("\nBinary Search Tree\n");
printf("1. Insert\n");
printf("2. Inorder Traversal\n");
printf("3. Search\n");
printf("4. Delete\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &value);
if (search(root, value))
printf("Value found in the tree.\n");
else
printf("Value not found.\n");
break;
case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
root = delete(root, value);
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science
return 0;
}
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science

The program was tested for different sets of inputs.


Program is working is SATISFACTORY NOT SATISFACTORY (Tick appropriate outcome)
Post Lab Questions:
1)​ How can we find minimum element from BST?
Evaluation: -
On Time Completion and Knowledge of the Implementation and Total (10)
Submission (2) topic (4) Output (4)
FR. CONCEICAO RODRIGUES COLLEGE OF ENGINEERING
Department of Electronics and Computer Science

Date and Signature of teacher:

You might also like