Binary search tree
Binary Search Tree (BST)
Aim
To implement a Binary Search Tree with create, search, and non-recursive traversals in C.
Operations in BST
Create → Insert a node into BST.
Search → Find whether an element exists in BST.
Non-recursive Traversals
Inorder (Left, Root, Right)
Preorder (Root, Left, Right)
Postorder (Left, Right, Root)
Algorithm
Create (Insert):
Create a new node.
If tree empty → new node becomes root.
Else, compare with root → go left if smaller, right if larger.
Repeat until correct position found.
Search:
Start at root.
If key = root → found.
If key < root → search left subtree.
Else search right subtree.
Repeat until found or NULL.
Non-Recursive Traversals:
Inorder: Use stack → push left nodes, visit root, then right.
Preorder: Use stack → push root, then right, then left.
Postorder: Use two stacks or modified stack logic.
Flowchart
C Code – BST Implementation
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left, *right;
};
// Create new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
// Insert node in BST
struct Node* insert(struct Node* root, int data) {
struct Node* newNode = createNode(data);
struct Node* parent = NULL, *temp = root;
while (temp != NULL) {
parent = temp;
if (data < temp->data)
temp = temp->left;
else
temp = temp->right;
}
if (parent == NULL) // Tree empty
return newNode;
else if (data < parent->data)
parent->left = newNode;
else
parent->right = newNode;
return root;
}
// Search element in BST
struct Node* search(struct Node* root, int key) {
while (root != NULL) {
if (key == root->data)
return root;
else if (key < root->data)
root = root->left;
else
root = root->right;
}
return NULL;
}
// Non-recursive Inorder Traversal
void inorder(struct Node* root) {
struct Node* stack[100];
int top = -1;
struct Node* curr = root;
while (curr != NULL || top != -1) {
while (curr != NULL) {
stack[++top] = curr;
curr = curr->left;
}
curr = stack[top--];
printf("%d ", curr->data);
curr = curr->right;
}
}
// Non-recursive Preorder Traversal
void preorder(struct Node* root) {
if (root == NULL) return;
struct Node* stack[100];
int top = -1;
stack[++top] = root;
while (top != -1) {
struct Node* node = stack[top--];
printf("%d ", node->data);
if (node->right) stack[++top] = node->right;
if (node->left) stack[++top] = node->left;
}
}
// Non-recursive Postorder Traversal
void postorder(struct Node* root) {
if (root == NULL) return;
struct Node* stack1[100], *stack2[100];
int top1 = -1, top2 = -1;
stack1[++top1] = root;
while (top1 != -1) {
struct Node* node = stack1[top1--];
stack2[++top2] = node;
if (node->left) stack1[++top1] = node->left;
if (node->right) stack1[++top1] = node->right;
}
while (top2 != -1) {
struct Node* node = stack2[top2--];
printf("%d ", node->data);
}
}
// Driver Program
int main() {
struct Node* root = NULL;
int choice, val, key;
struct Node* result;
while (1) {
printf("\n--- BST Menu ---\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Inorder Traversal (Non-Recursive)\n");
printf("4. Preorder Traversal (Non-Recursive)\n");
printf("5. Postorder Traversal (Non-Recursive)\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &val);
root = insert(root, val);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &key);
result = search(root, key);
if (result != NULL)
printf("Element %d found in BST.\n", key);
else
printf("Element %d not found.\n", key);
break;
case 3:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 5:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Comments
Post a Comment