Exp5 Binary Search Tree 24-25
Exp5 Binary Search Tree 24-25
Experiment No. : 5
Aim: Write a program for following operations on Binary Search Tree using Doubly Linked
List (DLL).
1) Create empty BST,
2) Insert a new element on the BST
3) Search for an element on the BST
4) Delete an element from the BST
5) Display all elements of the BST
___________________________________________________________________________
Theory
Binary Tree:-
A binary tree is made of nodes, where each node contains a "left" reference, a "right"
reference, and a data element. The topmost node in the tree is called the root.
Every node (excluding a root) in a tree is connected by a directed edge from exactly one
other node. This node is called a parent. On the other hand, each node can be connected to 0 /1 /
2 number of nodes, called children. Nodes with no children are called leaves, or external nodes.
Nodes which are not leaves are called internal nodes. Nodes with the same parent are called
siblings.
Example
Traversals:
A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data
structure, there is no unique traversal. We will consider several traversal algorithms with we
group in the following two kinds.
1. Depth-first traversal
2. Breadth-first traversal
PreOrder traversal - visit the parent first and then left and right children;
InOrder traversal - visit the left child, then the parent and the right child;
PostOrder traversal - visit left child, then the right child and then the parent.
There is only one kind of breadth-first traversal--the level order traversal. This traversal visits
nodes by levels from top to bottom and from left to right.
Binary Search Tree(BST): It is a binary tree where elements are stored in a particular order
of left child is less than the parent and parent is less than the right child. This small
modification makes BST efficient for searching. It is also called as BST property.
Example of BST following fig is the example BST whose root is 8 and follows the BST
property.
Binary trees can be constructed from programming language primitives in several ways.
In a language with records and references, binary trees are typically constructed by
having a tree node structure which contains some data and references to its left child and
its right child. Sometimes it also contains a reference to its unique parent. If a node has
fewer than two children, some of the child pointers may be set to a special null value, or
to a special sentinel node.
Binary Search Tree can be implemented as a linked data structure in which each node is
an object with two pointer fields. The two pointer fields left and right points to the nodes
corresponding to the left child and the right child NULL in any pointer field signifies that
there exists no corresponding child.
___________________________________________________________________________
Algorithm :
1. createBST() – This function should create a ROOT pointer with NULL value as empty BST.
2. insert( typedef newelement ) – This void function should take a newelement as an argument to be
inserted on an existing BST following the BST property.
3. typedef search(typedef element ) – This function should search for specified element on the non-
empty BST and return a pointer to it.
4. typedef delete(typedef element) – This function searches for an element and if found deletes it
from the BST and returns the same.
5. typedef getParent(typedef element) - This function searches for an element and if found, returns
its parent element.
6. DisplayInorder( ) – This is a void function which should go through non- empty BST, traverse it
in inorder fashion and print the elements on the way.
_________________________________________________________________________________
NOTE : All functions should be able to handle boundary(exceptional) conditions.
Activity: Write pseudocode for each method and implement the same in C.
(A Constituent College of Somaiya Vidyavihar University)
KJSCE/RAI/SY BTech/SEM III/DSA/2024-25
Results: A program depicting the correct behaviour of BST and capable of handling all possible
exceptional conditions and the same is reflecting clearly in the output.
____________________________________________________________________________
Outcomes:
Conclusion:
Grade: AA / AB / BB / BC / CC / CD /DD
References: