You are on page 1of 7

S.Y.B.

Tech (CSE) Subject : Data Structures

Experiment No. : 13

Title: Write a program to implement binary search tree.

Objectives:
1. To learn binary search tree
2. To implement binary search tree insertion operation in static and
dynamic tree.
3. To implement traversal techniques such as Inorder, Preorder,
Postorder on binary search tree
Theory:

Binary Search tree is a binary tree in which each node x stores an element
such that the element stored in the left subtree of x are less than x and elements
stored in the right subtree of x are greater than x. This is called binary-search-tree
property. The height of the Binary Search Tree equals the number of links from the
root node to the deepest node.

100

75 150

65 85 185

Binary Search Tree Storage:


Linked Data structure for Binary Search Tree(Dynamic Tree):
Binary Search Tree can be implemented as a linked data structure(dynamic
tree) in which each node contains two pointer fields. The two pointer fields are
left, and right pointer.

Textile and Engineering Institute, Ichalkaranji. Page 13.1


S.Y.B.Tech (CSE) Subject : Data Structures

Tree Node declaration:


struct treeNode
{
int data
struct treeNode *left;
struct treeNode *right;
}

data
left right

Array Representation of Binary Search Tree (static tree):


Binary Tree or Binary search tree can be stored in an array. This is called as static
representation or static tree or static tree storage.
When binary tree stored in an array, if the root node/ parent node is stored at “R”
index the its left child node is stored at 2*R +1 index and right child node is stored
at 2*R +2 index. This concept is recursively applied to all the nodes in the tree. The
example of binary tree is shown in the figure below:

100 index value


0 100
1 75
75 150
2 150
3 65
65 85 185 4 85
5 -1
6 185
Binary Search Tree Traversal
A binary tree traversal processes each node of the tree once and only once in a
predetermined sequence. Since tree is a non-linear data structure, there are
different ways for traversal. Most common methods for tree traversal are:
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal

Textile and Engineering Institute, Ichalkaranji. Page 13.2


S.Y.B.Tech (CSE) Subject : Data Structures

Preorder Traversal:
In this traversal, nodes are visited in the sequence root, left child and right child
node recursively for entire tree.
Example: the preorder traversal of above tree is
100,75,65,85, 150,185
Inorder Traversal:
In this traversal, nodes are visited in the sequence left child, root and right child
node recursively for entire tree.
Example: the Inorder traversal of above tree is
65,75,85,100,150,185
Postorder Traversal:
In this traversal, nodes are visited in the sequence left child, right child, and root
node recursively for entire tree.
Example: the Postorder traversal of above tree is
65,85,75,185,150,100

Algorithms:
Iterative Algorithm to insert data in binary search tree using linked
representation:
Algorithm: linkedBSTInsert
1. Start.
2. Is tree empty?
3. Yes –
a. create node.
b. store the data in the node.
c. attach the node to the root.
4. No –
a. Get the data from the root node
b. Is insertData < rootData?
i. Yes –
1. Is left child node exists?
2. Yes – call the left child node as root and repeat from step a)
3. No –

Textile and Engineering Institute, Ichalkaranji. Page 13.3


S.Y.B.Tech (CSE) Subject : Data Structures

I. Create new tree node.


II. Store the data in the new node.
III. Attach the new node to the left of root.
ii. No –
1. Is right child node exists?
2. Yes - call the right child node as root and repeat from step a)
3. No -
I. No - Create new tree node.
II. Store the data in the new node.
III. Attach the new node to the right of the root.
5. End.
Recursive Algorithm to insert data in binary search tree using linked
representation:
Algorithm:
BSTInsert( root, data)
{
If(root == NULL)
tmp = creteNode()
initNode(tmp,data)
root = tmp
Else
If( data < root-> data)
If( root -> left != NULL)
BSTInsert( root-> left, data)
Else
tmp = creteNode()
initNode(tmp,data)
root-> left = tmp
endIf
Else
If( root -> right != NULL)
BSTInsert( root-> right, data)
Else
tmp = creteNode()

Textile and Engineering Institute, Ichalkaranji. Page 13.4


S.Y.B.Tech (CSE) Subject : Data Structures

initNode(tmp,data)
root-> right = tmp
endif
endif
endif
}

Iterative Algorithm to insert data in binary search tree using array


representation:
Algorithm: linkedBSTInsert
1. Start.
2. Is tree empty?
3. Yes –
a. Store the data at the root index
4. No –
a. Get the data from the root index
b. Is insertData < rootData?
i. Yes –
1. Is left child node exists?
2. Yes – set the left child node index as root index and repeat
from step a)
3. No –
I. Get the left child index.
II. Store the data at the left child index
ii. No –
1. Is right child node exists?
2. Yes – set the right child node index as root indexand repeat
from step a)
3. No -
I. Get the right child node index.
II. Store the data at the right child index.
5. End.

Textile and Engineering Institute, Ichalkaranji. Page 13.5


S.Y.B.Tech (CSE) Subject : Data Structures

Recursive Algorithm to insert data in binary search tree using linked


representation:
Algorithm:
BSTInsert( treeArray, rootIndex, data)
{
If(treeArray[rootIndex] == -1)
treeArray[rootIndex] = data
Else
If( data < treeArray[rootIndex])
leftIndex = 2* rootIndex +1
If( treeArray[leftIndex] != -1)
BSTInsert( treeArray, leftIndex, data)
Else
treeArray[leftIndex] = data
endIf
Else
rightIndex = 2* rootIndex +2
If( treeArray[rightIndex] != -1)
BSTInsert( treeArray,rightIndex, data)
Else
treeArray[rightIndex] = data
endif
endif
endif
}

Textile and Engineering Institute, Ichalkaranji. Page 13.6


S.Y.B.Tech (CSE) Subject : Data Structures

Tree traversal algorithms:

Preorder Tree Traversal


preorder( root)
{
If( root != NULL)
{
Process(root)
preorder(root -> left)
preorder(root -> right)
}
}

Inorder Tree Traversal


inorder( root)
{
If( root != NULL)
{
inorder(root -> left)
Process(root)
inorder(root -> right)
}
}

Postorder Tree Traversal


postorder( root)
{
If( root != NULL)
{
postorder(root -> left)
postorder(root -> right)
Process(root)
}
}

Key concepts:

tree, root node, left subtree ,right subtree, inorder, preorder,


postorder, left child, Right child.

Textile and Engineering Institute, Ichalkaranji. Page 13.7

You might also like