DSA Pract08
DSA Pract08
Practical No-8
AIM:- (Binary Search Tree) Construct all possible BSTs for keys 1 to N and display the post
order traversal of each tree.
THEORY :-
Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree
specifically.
Binary Tree is a special data structure used for data storage purposes. A binary tree has a special
condition that each node can have a maximum of two children. A binary tree has the benefits of
both an ordered array and a linked list as search is as quick as in a sorted array and insertion or
deletion operation are as fast as in linked list.
Important Terms
Following are the important terms with respect to tree.
Path − Path refers to the sequence of nodes along the edges of a tree.
Root − The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node called parent.
Child − The node below a given node connected by its edge downward is called its child
node.
Leaf − The node which does not have any child node is called the leaf node.
Subtree − Subtree represents the descendants of a node.
Visiting − Visiting refers to checking the value of a node when control is on the node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If the root node is at level
0, then its next child node is at level 1, its grandchild is at level 2, and so on.
keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.
Uses of Inorder
In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order. To
get nodes of BST in non-increasing order, a variation of Inorder traversal where Inorder traversal
s reversed, can be used.
Uses of Preorder
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix
expression on of an expression tree.
Uses of Postorder
Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the postfix
expression of an expression tree.
* Pre-order traversal
1. Print the data at the root
2. Recursively print out all data in the left subtree
3. Recursively print out all data in the right subtree
* Preorder traversal
1. node, left, right
2. prefix expression
3. ++a*bc*+*defg
* Postorder traversal
i. left, right, node
ii. postfix expression
iii. abc*+de*f+g*+
* Inorder traversal
i. left, node, right.
ii. infix expression
iii. a+b*c+d*e+f*g
A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned
properties −
The value of the key of the left sub-tree is less than the value of its parent (root) node's
key.
The value of the key of the right sub-tree is greater than or equal to the value of its parent
(root) node's key.
Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right sub-tree and
can be defined as − left_subtree (keys) < node (key) ≤ right_subtree (keys)
1 2
\ /
2 1
Source code:-
from drawtree import draw_bst
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
@staticmethod
def display(root, l):
if root is not None:
l.append(root.data)
Node.display(root.left, l)
Node.display(root.right, l)
@staticmethod
def postorder(root):
if root is not None:
Node.postorder(root.left)
Node.postorder(root.right)
print(root.data, end=" ")
@staticmethod
def construct(start, end):
list_of_trees = []
if start > end:
list_of_trees.append(None)
return list_of_trees
Output:-
Viva Questions:-
Define tree, Binary tree
Ans:-
l)Tree: Tree is a hierarchical data structure. A tree is a finite set of nodes that has a
distinguished node called the root node.
Remaining nodes of the tree forms disjoint sets of nodes. These sets are again trees. Each
of which is called subtree of root node.
Binary Tree: A binary tree is a tree in which every node can have at the most two
children.
It is a finite set of elements called nodes such that either Tree is empty or It contains a
root node R and remaining nodes forms two sets of disjoint binary trees called left
subtree and right subtree of R.
So a binary tree may be either empty or contains a distinguished node called root node.
Root node is the top most node of the tree. If there are more than one node then
remaining nodes belong to either left or the right subtree of root node.
How would you print out the data in a binary tree, level by level, starting at the top?
Ans:- A simple solution is to print using the recursive function discussed in the level
order traversal post and print a new line after every call to printGivenLevel().
Find height of tree and run depth first search and maintain current height, print nodes
for every height from root and for 1 to height.
Ans:-
int maxDepth(node* node)
{
if (node == NULL)
return 0;
else {
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
The pre-order traversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19,
17, 20.
Then the post-order traversal of this tree is:
(A) 2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20 (B) 2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12
(C) 7, 2, 6, 8, 9, 10, 20, 17, 19, 15, 16, 12 (D) 7, 6, 2, 10, 9, 8, 15, 16, 17, 20, 19, 12
A binary search tree contains the value 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in pre-
order and the values are printed out. Which of the following sequences is a valid output?
(a) 5 3 1 2 4 7 8 6 (b) 5 3 1 2 6 4 8 7 (c) 5 3 2 4 1 6 7 8 (d) 5 3 1 2 4 7 6 8
The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35,
42. Which one of the following is the postorder traversal sequence of the same tree?
(GATE 2013)
(A) 10, 20, 15, 23, 25, 35, 42, 39, 30
(B) 15, 10, 25, 23, 20, 42, 35, 39, 30
(C) 15, 20, 10, 23, 25, 42, 35, 39, 30
(D) 15, 10, 23, 25, 20, 35, 42, 39, 30