0% found this document useful (0 votes)
14 views5 pages

Exp5 Binary Search Tree 24-25

Uploaded by

sujay.kamuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Exp5 Binary Search Tree 24-25

Uploaded by

sujay.kamuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

KJSCE/RAI/SY BTech/SEM III/DSA/2024-25

Experiment No. : 5

Title: Implementation of Dynamic Binary Search Tree using


Doubly Linked List (DLL)

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/RAI/SY BTech/SEM III/DSA/2024-25

Batch: Roll No.: Experiment No.:

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

___________________________________________________________________________

Resources needed: C/C++ editor and compiler


___________________________________________________________________________

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

Figure(a): Binary Tree 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.

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/RAI/SY BTech/SEM III/DSA/2024-25

1. Depth-first traversal
2. Breadth-first traversal

There are three different types of depth-first traversals:

 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.

Consider an example the following tree and its four traversals:

Figure 5.3: Example of Tree Traversals

PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3


InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8
LevelOrder - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2

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.

Methods for storing binary trees

Binary trees can be constructed from programming language primitives in several ways.

Nodes and references

(A Constituent College of Somaiya Vidyavihar University)


KJSCE/RAI/SY BTech/SEM III/DSA/2024-25

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.

Figure : Binary Search Trees example

___________________________________________________________________________

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.

____________________________________________________________________________

Program with output:

Outcomes:

Conclusion:

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of faculty in-charge with date


_____________________________________________________________________________

References:

Books/ Journals/ Websites:

 Y. Langsam, M. Augenstin and A. Tannenbaum, “Data Structures using


C”, Pearson Education Asia, 1st Edition, 2002.
 https://ds1-iiith.vlabs.ac.in/exp/binary-search-trees/index.html

(A Constituent College of Somaiya Vidyavihar University)

You might also like