Lecture Tree traversal
Lecture Tree traversal
1
INTRODUCTION
Data structure such as Arrays, Stacks, Linked List and
Queues are linear data structure. Elements are arranged in
linear manner i.e. one after another.
Tree is a non-linear data structure.
Tree imposes a Hierarchical structure, on a collection of
items.
Several Practical applications
–Organization charts.
–Family hierarchy
–Representation of algebraic expression
2
Tree
A tree is a collection of nodes
The collection can be empty
If not empty, a tree consists of a distinguished
node
R(root)
SubtreesT1, T2, ...., Tn, each of whose
roots are connected by a directed edge
from Root.
3
Tree Terminologies
Root - It is the mother node of a tree structure.
This tree does not have parent. It is the first node
in hierarchical arrangement.
Node - The node of a tree stores the data and its
role is the same as in the linked list. Nodes are
connected by the means of links with other
nodes.
Parent - It is the immediate predecessor of a
node. In the figure A is the parent of B and C.
4
Child - When a predecessor of a node is parent
then all successor nodes are called child nodes. In
the figure B and C are the child nodes of A
Link / Edge - An edge connects the two nodes.
The line drawn from one node to other node is
called edge / link. Link is nothing but a pointer to
node in a tree structure.
Leaf - This node is located at the end of the tree. It
does not have any child hence it is called leaf node.
5
Level - Level is the rank of tree hierarchy. The whole
tree structured is leveled. The level of the root node
is always at 0. The immediate children of root are at
level 1 and their children are at level 2 and so on.
Height - The highest number of nodes that is
possible in a way starting from the first node (root)
to a leaf node is called the height of tree. The formula
for finding the height of a tree , h = imax + 1, where
h is the height and I is the max level of the tree.
6
Sibling - The child node of same parent are called
sibling. They are also called brother nodes.
Degree of a Node - The maximum number of children
that exists for a node is called as degree of a node.
Terminal Node - The node with degree zero is called
terminal node or leaf.
Path length - Is the number of successive edges from
source node to destination node.
Ancestor and Descendant - Proper ancestor and
proper descendant.
7
Depth - Depth of a binary tree is the
maximum level of any leaf of a tree.
Forest - It is a group of disjoint trees. If we
remove a root node from a tree then it
becomes the forest.
8
Example: Expression Trees
9
Used to display/access the data in a tree in a certain order.
In traversing always right sub-tree is traversed after left sub-
tree.
Three methods of traversing
–Preorder Traversing
Root –Left –Right
Inorder Traversing
Left –Root –Right
Postorder Traversing
Left –Right -Root
10
Preorder traversal
Node –Left –Right
Prefix expression
++a*bc*+*defg
11
Inorder traversal
Left, Node, Right
Infix Expression
a+b*c+d*e+f*g
12
Postorder Traversal
Left, Right, Node
Postfix Expression
abc*+de*f+g*+
13