0% found this document useful (0 votes)
15 views

Experiment-5

The document outlines a program for constructing a binary search tree (BST) that includes operations for insertion, deletion, and traversal. It provides a theoretical background on tree structures, types of trees, common operations, and specific details about BSTs, including their advantages and disadvantages. The document also includes a complete C program demonstrating these operations and concludes with a reflection on the learning experience.

Uploaded by

Disha Deshmukh
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)
15 views

Experiment-5

The document outlines a program for constructing a binary search tree (BST) that includes operations for insertion, deletion, and traversal. It provides a theoretical background on tree structures, types of trees, common operations, and specific details about BSTs, including their advantages and disadvantages. The document also includes a complete C program demonstrating these operations and concludes with a reflection on the learning experience.

Uploaded by

Disha Deshmukh
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/ 10

Bharatiya Vidya Bhavan’s

SARDAR PATEL INSTITUTE OF TECHNOLOGY


(Autonomous Institute Affiliated to University of Mumbai)
Munshi Nagar, Andheri (W), Mumbai – 400 058.
COMPS Department

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.

 Node: A fundamental element of the tree that contains a value or


data.
 Edge: A link between two nodes, representing the relationship
between them.
 Root: The topmost node of the tree. It has no parent.
 Parent: A node that has edges leading to one or more child
nodes.
 Child: A node that is directly connected to another node (the
parent) and comes directly below it in the hierarchy.
 Leaf: A node that has no children.
 Subtree: A tree formed by any node and its descendants.
 Depth: The number of edges from the root to the node.
 Height: The number of edges from a node to the deepest leaf.
The height of the tree is the height of the root node.
 Level: The level of a node is determined by the number of edges
between it and the root (the root is at level 0).

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.

Common Operations on Trees:


1. Insertion: Adding a new node to the tree.
2. Deletion: Removing a node from the tree.
3. Traversal: Visiting all nodes in the tree in a specific order, such
as in-order, pre-order, or post-order traversal.

Binary Search Tree (BST)


A BST is a specialized binary tree structure that organizes data in a
hierarchical manner. The primary feature of a BST is that for any given
node:
 The left subtree contains nodes with values less than the node’s
key.
 The right subtree contains nodes with values greater than the
node’s key.

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.

Basic Operations in BST


 Insertion: Adds an element to the BST while maintaining its
order.
 Deletion: Removes an element from the tree and ensures the
BST properties are preserved.
 Traversal: There are different ways to visit all the nodes (e.g., in-
order, pre-order, post-order)

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;
};

struct Node* newNode(int item) {


struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}

struct Node* insert(struct Node* node, int key) {


if (node == NULL) {
return newNode(key);
}
if (key < node->key) {
if (node->left == NULL) {
printf("Inserted %d as left child of %d\n", key, node->key);
}
node->left = insert(node->left, key);
} else if (key > node->key) {
if (node->right == NULL) {
printf("Inserted %d as right child of %d\n", key, node->key);
}
node->right = insert(node->right, key);
}
return node;
}

struct Node* minValueNode(struct Node* node) {


struct Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}

struct Node* deleteNode(struct Node* root, int key) {


if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL) {
struct Node* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root->left;
free(root);
return temp;
}
struct Node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy