What is a Binary Search Tree (BST)?
A Binary Search Tree (BST) is a binary tree where:
-
Each node has at most two children (left and right).
-
Left subtree of a node contains only nodes with values less than the node’s value.
-
Right subtree of a node contains only nodes with values greater than the node’s value.
-
No duplicate nodes are allowed (usually).
This property helps in fast searching, insertion, and deletion (average time complexity: O(log n)).
Example
Figure shows a binary search tree. Notice that this tree is obtained by inserting the values 13, 3, 4, 12, 14, 10, 5, 1, 8, 2, 7, 9, 11, 6, 18 in that order, starting from an empty tree.
Algorithms
1. Creation (Insertion) into BST
Algorithm to insert a node into a BST:
-
Create a new node.
-
If tree is empty, the new node becomes the root.
-
Otherwise:
-
Compare new value with current node's value.
-
If new value < current node → go to left child.
-
If new value > current node → go to right child.
-
Repeat until you find a NULL position and insert the new node there.
Tree Traversals
Traversal means visiting each node exactly once in a specific order.
There are mainly three types of tree traversal:
Traversal Type | Order | Meaning |
---|
Inorder | Left → Root → Right | Gives sorted order for BST. |
Preorder | Root → Left → Right | Useful for copying tree. |
Postorder | Left → Right → Root | Useful for deleting tree nodes safely. |
Algorithms for Traversals
(a) Inorder Traversal
Inorder(node):
if node is not NULL
Inorder(node->left)
Visit(node)
Inorder(node->right)
(b) Preorder Traversal
Preorder(node):
if node is not NULL
Visit(node)
Preorder(node->left)
Preorder(node->right)
(c) Postorder Traversal
Postorder(node):
if node is not NULL
Postorder(node->left)
Postorder(node->right)
Visit(node)
C Program-Binary Search Tree
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
// Insert into BST
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data)
root->left = insert(root->left, value);
else if (value > root->data)
root->right = insert(root->right, value);
return root;
}
// Inorder Traversal
void inorder(struct Node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
// Preorder Traversal
void preorder(struct Node* root) {
if (root != NULL) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
// Postorder Traversal
void postorder(struct Node* root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
int main() {
struct Node* root = NULL;
int choice, value;
do {
printf("\nMenu:\n1. Insert\n2. Inorder\n3. Preorder\n4. Postorder\n5. Exit\nEnter 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("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 5:
printf("Exiting...");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
Comments
Post a Comment