Experiment-5
Experiment-5
Experiment 5
Aim Write a program to construct a binary search tree, insert an element in BST, delete
an element from BST and traverse it.
Name Disha Hemant Deshmukh
UID 2023300044
Class A
Batch C
Date of 8/10/24
Submission
Theory A tree is a widely used data structure that is a hierarchical structure with
a set of linked nodes. Unlike linear data structures (like arrays, linked
lists), a tree is a non-linear data structure, which means that data
elements are arranged in a hierarchical manner. Trees have multiple
applications in fields like databases, file systems, and AI.
Types of Trees
1. General Tree: In a general tree, a node can have any number of
children.
2. Binary Tree: A binary tree is a special type of tree where each
node can have at most two children (left and right).
3. Binary Search Tree (BST): A binary tree with an additional
constraint that ensures the left child is smaller than the parent,
and the right child is greater.
4. Balanced Trees: A tree is balanced if the height of its left and
right subtrees differ by at most 1. Examples include AVL trees .
5. Heaps: A special kind of binary tree used for efficient priority-
based operations, where the parent node is either greater than
(Max Heap) or smaller than (Min Heap) its children.
Binary Search Trees are widely used because they allow fast lookup,
insertion, and deletion operations. Due to their ordered structure, we can
perform binary search efficiently.
Traversing a BST
Tree traversal refers to visiting all nodes in a specific order. There are
three common traversal methods for BST:
1. In-order Traversal (Left, Root, Right): This traversal visits the
nodes in ascending order of their values.
Traverse the left subtree.
Visit the root node.
Traverse the right subtree.
2. Pre-order Traversal (Root, Left, Right): This traversal visits the
root node first, followed by the left subtree and then the right
subtree.
3. Post-order Traversal (Left, Right, Root): This traversal visits the
left and right subtrees first, followed by the root node.
Advantages of BST
Efficient Searching: Searches are quicker than in a linear
structure like arrays.
Dynamic Size: BSTs adjust dynamically as you add or remove
elements.
Sorted Structure: In-order traversal of the BST yields sorted
elements.
Disadvantages of BST
Not self-balancing: Unbalanced BSTs can lead to poor
performance
Memory overhead: BSTs require additional memory to store
pointers to child nodes
Not suitable for large datasets: BSTs can become inefficient for
very large datasets
Limited functionality: BSTs only support searching, insertion,
and deletion operations
Applications of BST
Searching: Used in search engines, databases, and file systems.
Sorting: BST can be used to sort elements via in-order traversal.
Symbol Tables: Used in compilers for variable lookups.
Algorithm 1.Structure Definition
Each node contains:
key: The integer value stored in the node.
left: A pointer to the left child (smaller values).
right: A pointer to the right child (larger values).
2. newNode Function
The new node's left and right children are initialized to NULL, meaning
it's a leaf node
A new node is created using malloc().
The key is set, and pointers left and right are set to NULL
3.inorder Function
In-order traversal visits nodes in ascending order (Left -> Root ->
Right). It returns the nodes in sorted order for a binary search tree.
If the current node is not NULL, first visit its left subtree.
Print the value of the node itself.
Then, visit the right subtree.
4. insert Function
Inserts a node into the binary search tree while maintaining its
properties (left child is smaller, right child is larger).
If the node is NULL, it means it is the correct position, so a
new node is created and returned.
If the key to be inserted is smaller than the current node's key,
move to the left subtree.
If the key is larger, move to the right subtree.
The function is called recursively until the correct insertion
point is found.
4.minValueNode Function
Finds the node with the smallest value in a given subtree i.e leftmost
node.
Start from the given node and keep moving left until the
leftmost node is found.
This is used during deletion when a node with two children is
being removed i.e finds the in-order successor.
4.deleteNode Function
Deletes a node from the BST while maintaining its structure.
If Node has no children: Just remove the node.
If Node has one child: Replace the node with its child.
If Node has two children: Replace the node's value with its in-
order successor (smallest value in the right subtree), then delete
the in-order successor.
Problem
Solving
Program(Code) #include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
struct Node *left, *right;
};
int main() {
struct Node* root = NULL;
int choice, value;
while (1) {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Inorder Traversal\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 3:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
Output
Conclusion I learned how to implement a Binary Search Tree, performing basic
operations like insertion, deletion, and in-order traversal. The program
uses recursion to maintain the tree structure and efficiently manage data.