Professional Documents
Culture Documents
Experiment No. : 13
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
data
left right
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 –
initNode(tmp,data)
root-> right = tmp
endif
endif
endif
}
Key concepts: