BST - IMPLEMENTATION
BST - IMPLEMENTATION
Problem Statement:
Write C Program to implement binary search tree.
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) 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
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.
#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;
};
if (root == NULL) {
root = new_node;
return;
}
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