DSAL Assignments 4 5 6
DSAL Assignments 4 5 6
Aim: To construct an expression tree and perform recursive and non-recursive traversals.
Problem statement: To construct an Expression Tree from postfix and prefix expression.
Perform recursive and non- recursive in-order, pre-order and post-order traversals.
Theory:
The terminal nodes (leaves) of an expression tree are the variables or constants in the
expression (3,4, 6, or a, b, c). The non-terminal nodes of an expression tree are the operators (+,
-, *, /).
Notice that the parentheses which appear in equation do not appear in the tree.
1. Expression conversion i.e. to convert infix expression to postfix or prefix and vice a versa.
2. Evaluation of an infix, prefix and postfix expression.
+ -
a b c d
1. Pre-order (Prefix) : *+ab–cd
Structure: Binary_Tree ( ) is
Objects: a finite set of nodes either empty or consisting of a root node, left_Binary_Tree, and
right_Binary_Tree.
Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE
Binary_Tree MakeBT(bt1, item, bt2)::= return a binary tree whose left sub tree is bt1, whose
right sub tree is bt2, and whose root node contains the data item
Binary_Tree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left sub tree of bt
Element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt
Binary_Tree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right sub tree of bt
END
Algorithms:
For constructing expression tree we use a stack. We loop through input expression and do
following for every character.
1. Read an infix expression in an array and convert it to postfix and prefix. And perform following:
3. If character is operator pop two values from stack make them its child and push current node
again.
1. Traverse the left sub tree, i.e. call In-order (left – sub-tree)
2. Visit the root.
3. Traverse the right sub tree, i.e. call In-order (right – sub-tree)
1. Traverse the left sub tree, i.e. call Post-order (left – sub-tree)
2. Traverse the right sub tree, i.e. call Post-order (right – sub-tree)
3. Visit the root.
Validations:
Test Cases
Infix Prefix
A * (B + C) / D /*A+BCD
Infix Postfix
3 + (8 * 7) – 2 387*+2-
FAQS:
1. What is tree? What are properties of trees?
2. What is Binary tree, Binary search tree, Expression tree & General tree?
3. What are the members of structure of tree & what is the size of structure?
4. What are rules to construct binary tree?
5. What is preorder, post-order, in-order traversal?
6. Difference between recursive & Non-recursive traversal?
7. What are rules to construct binary search tree?
8. What are rules to construct expression tree?
9. How binary tree is constructed from its traversals?
5. Binary Tree
OBJECTIVE:
1. To understand the concept of binary tree as a data structure.
2. Applications and Operations of BT.
THEORY:
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.
Definition: A binary tree is a finite set of elements that is either empty or is partitioned into
three disjoint subsets. The first subset contains a single element called the root of the tree.
The other two subsets are themselves binary trees, called the left and right subtrees of the
original tree. Conventional method of picturing binary tree
Every node (excluding a root) in a tree is connected by edge from exactly one other node.
This node is called a parent. On the other hand, each node can be connected to arbitrary
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.
Advantages of BT:
Trees are so useful and frequently used, because they have some very serious
advantages:
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. Two broad categories of traversals are
depth-first traversal
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.
ALGORITHM:
Insert Node:
Insertion operation means inserting a new node into any position of the binary tree.
The insertion operation can take place in any one of the following two positions
Steps:
1. Accept the KEY (or data item) which has to be searched in a BT after which
the new data item has to be placed
2. Accept the item to be inserted, ITEM
3. Start from the ROOT node, search KEY in the binary tree. The address of
the node with KEY as the data will be in the ptr.
4. Check whether ptr contains NULL or not. If ptr is NULL then Search is
Unsuccessful.
5. Otherwise
a. Node with data item KEY is there in the binary tree (i.e in ptr)
b. Now check whether the left child of ptr is empty or the right child of ptr
is empty. If the left child of the ptr is empty then do steps (i) to (v)
(i) Allocate memory space for the new node (say new)
(ii) Place the item to be inserted (ITEM) in the new node.
(iii) Make the left child and right child fields of the new node to be
NULL
(iv) Make the new node as left child of ptr.
c. Otherwise check whether the right child of ptr is empty. If the right
child of the ptr is empty then do steps (i) to (v)
(i) Allocate memory space for the new node (say new)
(ii) Place the item to be inserted (ITEM) in the new node.
(iii) Make the left child and right child fields of the new node to be
NULL
(iv) Make the new node as right child of ptr.
d. Otherwise Print ―Insertion is not possible. The KEY node already has
a child
6. Stop
Display:
Depth of a Tree:
1. Start from the ROOT node and store the address of the root node in ptr.
2. Check whether ptr contains NULL or not. If ptr is NULL then tree is empty.
3. Otherwise
Display Leaf-Nodes:
This function displays all the leaf nodes in the linked binary tree.
1. Start from the ROOT node and store the address of the root node in ptr.
2. Check whether ptr contains NULL or not. If ptr is NULL then tree is empty.
3. Otherwise
a. Check whether the left child and right child of ptr are empty or not. If the
left child and right child of ptr are NULL then display the content of the
node.
b. Traverse the left subtrees and the right subtrees and repeat step a for all
the nodes.
4. Stop
This function will make a copy of the linked binary tree. The function should allocate
memory for necessary nodes and copy respective contents in it.
1. Start from the ROOT node. The address of the root node will be in the ptr. Let
newroot be the root of the new tree after the copy.
2. Check whether ptr contains NULL or not. If ptr is NULL then newroot = NULL
3. Otherwise
4. Stop
INPUT:
Test Case O/P
Tree Empty Display message ―Tree Empty‖
Insertion is not possible. The KEY node already has a child
Binary Tree
OUTPUT:
Height of a Binary Tree
FAQ:
For example, (Ref. figure below), let us consider a binary tree for the
prefix arithmetic expression +*/A-BCDE
(Note: It is not mandatory that TBT should be constructed only by
using either prefix / postfix expression.)
The tree has n=9 nodes, n-1 (i.e. 8) normal (constructional) pointers
(represented by solid directional lines), and n+1 (i.e. 10) null links. In
TBT, the n+1 null links are replaced by Threads (represented by dotted
lines) (Ref. figure below).
If the above tree is traversed in the In-order manner, then the output
would be
A / B – C * D + E ------------------------- (1)
For example, node D’s left link is replaced with a pointer (Thread) to a
node ‘*’ which is an immediate predecessor of node D (Ref. In-order
Traversal in 1). Node D’s right link is replaced with a pointer (Thread) to a
node ‘+’ which is an immediate successor of node D (Ref. In-order
Traversal in 1).
In the above figure node ‘A’ s left pointer and node ‘E’ s right pointer have
been left dangling as node ‘A’ doesn’t have any In-order predecessor and
node ‘E’ doesn’t have any In-order successor (Ref. 1). In order to have no
loose Threads, all dangling pointers are connected to a node called as
Head node in all the TBT’s.
The empty in order threaded tree binary tree will have only one HEAD
node as shown in the following diagram
1. Time Complexity:
Time complexity for the construction of binary tree from input prefix
expression is O (n), while loop is executed for time which is equal to
number of nodes in constructed binary tree.
Time complexity for normal binary tree traversal whether recursive or non-
recursive is also O(n), where ‘n’ is equal to number of nodes in binary
tree, since every node is visited only once in both. For the TBT, it is still
O(n), but it is constant times less than recursive and non-recursive using
stack.
2. Space Complexity:
In recursive and non-recursive traversals of normal binary tree. We need
a stack of depth ‘k’ which is equal to height or depth of a binary tree. But
in TBT stack is not required.