Tree
Case Study of Tree Diagrams
Discussed So far
• Here are some of the data structures we have
studied so far:
– Arrays
– Linked list
– Stacks, Queues, and Deques
2
Tree
A tree is defined as a finite set of one or more nodes such
that
a. There is a specially designated node called the root
and
b. The rest of the nodes could be partitioned into n
disjoint sets (n>0) each set representing a tree Ti, i=1,2,
. . . n known as subtree of the tree.
3
Tree
A node in the definition of the tree
represents an item of information, and
the links between the nodes termed as
branches, represent an association between
the items of information.
4
Tree
• Definition of Tree emphasizes on the aspect of
[a] Connectedness, and
[b] Absence of closed loops
5
Basic terminologies
12 nodes in the tree
Parent of H is B
Root is A
F,G,H are children of B
• Node: stores the actual data and links to other nodes
• Parent: immediate predecessor of a node
• Root: specially designated node which has no parent
• Children: immediate successors of a node.
6
Basic terminologies
Leaf nodes: F,G,H,C,I,J,L
K is at Level 2
A has 4 subtrees
• Leaf: node without any child
• Level: represents the hierarchy.
– Node at level i has the level i+1 for its child and i-1 for its
parent.
– This is true for all nodes except the root
• Subtree: of a node is a tree whose root is a child of
that node
7
Basic terminologies
Height of E = 3
Depth of E = 2
Height of Tree = 4
Degree of B = 3
• Height of a node: no of nodes in the longest path to a leaf from
that node
• Depth of a node: no of nodes in the path from root to that node
• Height (depth) of tree: no of nodes in the longest path from
root node to leaf node.
– Height of a tree given by h = lmax+1, lmax is the maximum level of the
tree.
• Degree of node: number of children for the node
8
Basic terminologies
Degree of Tree = 4
B,C,D,E are Siblings
A,E,K are ancestors of L
• Degree of a Tree: Maximum degree of the node in the
tree
• Siblings: nodes having the same parent
• Ancestor of a Node: Those nodes that occur on the
path from the root to the given node
• Forest : A set of Zero or more Disjoint trees.
9
Representation of a tree
1. List Representation
( A( B(F,G,H), C, D(I), E(J,K(L)) ) )
10
2. Linked List Representation
DATA LINK 1 LINK 2 …
LINK n
(a) General node structure
T
B C D E
F G H I J K
L
(b) Linked list representation of the tree 11
3. An alternative elegant linked list representation
TAG DATA / DOWNLINK LINK
1/0
(a) General node structure
12
3. An alternative elegant linked representation
T 1 A 0 1 C 0 0
1 B 1 F 1 G 1 H
1 D 1 I
1 E 1 J O
1 K 1 L
(b) Linked representation of the tree
13
4. Left Child - Right Sibling representation
data T
left child right sibling
A
B C D E
F I J K
G H
L
14
Binary Trees
A binary tree T is defined as a finite set of
elements called nodes such that
[a] T is empty (Called the Null tree or Empty
tree) or
[b] T contains a distinguished node R called the
root of T and the remaining nodes of T form an
pair of disjoint binary trees T1 and T2
15
Binary Trees
• A binary tree has the characteristic of all
nodes having at most two branches, that is, all
nodes have a degree of at most 2.
• A binary tree can therefore be empty or
consists of a root node and two disjoint binary
trees termed left subtree and right subtree.
16
A Level 0
B C Level 1
D E F G Level 2
17
Important observations regarding binary trees:
The maximum number of nodes on level i of a
binary tree is 2i, i>0
The maximum number of nodes in a binary tree of
height h is 2h-1, h>1
20+21+22+…+2h-1=2h-1
For an non-empty binary tree, if n is the number of
nodes and e is the number of edges, then e = n - 1.
For any non empty binary tree, if no is the number
of terminal nodes and n2 is the number of nodes of
degree 2, then no=n2+1
18
A binary tree of height h which has all its
permissible maximum number of nodes viz.,
2h-1 intact is known as a full binary tree of
height h.
1 A
2 B
3
C
D E 6 F G
4 5 7
19
A binary tree with n nodes is complete if all its levels
are having maximum no of nodes except possibly in the
last level where the nodes are filled from left to right.
Height of a complete
1 A binary tree with n given by
2 B C 3 h log 2 (n 1)
D E
4 5 6 F
20
A complete binary tree with n nodes obeys the
following properties with regard to its node numbering:
[a] If a parent node has a number i then its left child has
the number 2i (2i < n).
-- If 2i > n then i has no left child.
[b] If a parent node has a number i, then its right child
has the number 2i+1 (2i + 1 <n).
-- If 2i + 1 > n then i has no right child.
[c] If a child node (left or right) has a number i then the
parent node has the number i /2 if i 1. If i =1
then i is the root and hence has no parent.
21
A binary tree which is dominated solely by left child
nodes or right child nodes is called a skewed binary
tree or more specifically left skewed binary tree or
right skewed binary tree respectively.
a m
b n
c o
Left skewed Right skewed p
d
22
Extended Binary Tree: 2-Tree
A binary tree T is said to be 2-Tree or an
extended binary tree if each node N has either
0 or 2 children.
Nodes with 2 children are called internal nodes
and the nodes with 0 children are called
external nodes.
23
Representation of Binary Tree
Binary tree can be represented by means of
[a] Array
[b] linked list
24
Representation Of Binary Trees
Array Representation
1
a
Sequential representation of a
2
tree with height h will require an
b array with approx 2h-1 elements
4 5
c d
10
e f
11
1 2 3 4 5 6 7 8 9 10 11
a b c d e f
25
Array Representation
A [1] A
[2] B
[3] --
B
[4] C
[5] --
C [6] --
[7] --
D [8] D
[9] --
. .
E
[16] E
Memory wastage !!
26
Linked representation
T
LCHILD DATA RCHILD
b
c d
e f
27
• Observation regarding the linked representation
of Binary Tree
[a] If a binary tree has n nodes then the number of
pointers used in its linked representation is 2n
+1
[b] The number of null pointers used in the
linked representation of a binary tree with n
nodes is n + 1
28
Traversing Binary Tree
Three ways of traversing the binary tree T with
root R
Preorder
[a] Process the root R
[b] Traverse the left sub-tree of R in preorder
[c] Traverse the right sub-tree of R in preorder
a. k. a node-left-right traversal (NLR)
29
Traversing Binary Tree
In-order
[a] Traverse the left sub-tree of R in in-order
[b] Process the root R
[c] Traverse the right sub-tree of R in in-order
a. k. a left-node-right traversal (LNR)
30
Traversing Binary Tree
Post-order
[a] Traverse the left sub-tree of R in post-order
[b] Traverse the right sub-tree of R in post-order
[c] Process the root R
a. k. a left-right-node traversal (LRN)
31
Illustrations for Traversals
• Assume: visiting a node 1
is printing its label 3 7
• Preorder:
5 8 9
1 3 5 4 6 7 8 9 10 11 12
4 6 10
• Inorder:
4 5 6 3 1 8 7 9 11 10 12 11 12
• Postorder:
4 6 5 3 8 11 12 10 9 7 1
32
Illustrations for Traversals (Contd.)
• Assume: visiting a node 15
is printing its data 8 20
• Preorder: 15 8 2 6 3 7
11 10 12 14 20 27 22 30 2 11 27
• Inorder: 2 3 6 7 8 10 11
6 10 12 22 30
12 14 15 20 22 27 30
• Postorder: 3 7 6 2 10 14 3 7 14
12 11 8 22 30 27 20 15
33
Euler’s Tree traversals using “flags”
• The order in which the nodes are visited during a tree
traversal can be easily determined by imagining there is a
“flag” attached to each node, as follows:
preorder inorder postorder
• To traverse the tree, collect the flags:
A A A
B C B C B C
D E F G D E F G D E F G
ABDECFG DBEAFCG DEBFGCA
34
Formulation of Binary tree from
Its traversal
Easy if one given traversal sequence in inorder.
1. If preorder is given=>First node is the root
If postorder is given=>Last node is the root
2. Once the root node is identified ,all nodes in the left
subtrees and right subtrees of the root node can be
identified from inorder.
3. Same technique can be applied repeatedly to form subtrees
35
Example: For Given Inorder and Preorder
Inorder: D B H E A I F J F CG
Preorder: A B D E H C F I J G
Now root is A
Left subtree: D B H E
Right subtree: I F J C G
36
continues.
A
In: D B H E IFJCG
Pre: B D E H CFIJG
D HE IFJ G
EH FIJ
H I J
37
Example: For Given Inorder and
Postorder
Inorder: n1,n2, n3, n4, n5, n6, n7, n8, n9
Postorder: n1,n3, n5, n4, n2, n8, n7, n9, n6
So here n6 is the root
38
n6
In:n1,n2,n3,n4,n5 n7,n8,n9
Post:n1,n3,n5,n4,n2 n8,n7,n9
n1 n3,n4,n5 n7,n8
n3,n5,n4 n8,n7
n3 n5 n8
39
Given Preorder & Postorder
• Tree may not be unique
preorder: A B C
postorder: C B A
A A
B B
C C
Tree-1 Tree-2
40
Given Preorder & Postorder
1. First node in preorder is ROOT (same as last node
in postorder)
2. Find previous node of ROOT in postorder (say X)
and locate it in preorder
3. Nodes before X in preorder is the left subtree of
ROOT
4. X and nodes after X in preorder is the right subtree
of ROOT
5. Repeat stepts 1 to 5 until each subtree contains one
element
41
Given Preorder & Postorder
Pre: A B C D F G E
Post: C F G D B E A
Pre: B C D F G Pre: E
Post: C F G D B Post: E
Pre: C Pre: D F G
Post: C Post: F G D
Pre: F Pre: G
Post: F Post: G
42
Traversal Algorithm Using Stack
• Binary Tree is represented by
TREE(INFO, LEFT, RIGHT, ROOT)
• A pointer PTR will contain the location of the
node N currently being scanned.
• An array STACK will hold the addresses of
the node for future processing
43
Pre-order tree traversal with a stack
1. Push root onto the stack
2. While stack is not empty
– Pop a vertex off stack, and write it to the output list
– Push its children right-to-left onto stack
Step Output Stack
0 A
A
1 A C,B
B C 2 B C,D
3 D C,H,G
D E F 4 G C,H
5 H C,K
G H 6 K C
7 C F,E
K 8 E F
9 F --
44
In-order Traversal with a stack
[1] [Push NULL onto STACK and initialize PTR]
Set TOP =1, STACK[1] = NULL, PTR = ROOT
[2] Repeat while PTR NULL [Push the Left-most path onto STACK]
(a) Set TOP = TOP + 1, STACK[TOP] = PTR
(b) Set PTR = PTR LEFT
[3] Set PTR = STACK[TOP], TOP = TOP -1 [Pops node from STACK]
[4] Repeat Steps 5 to 7 while PTR NULL: [Backtracking]
[5] Apply PROCESS to PTRINFO
[6] [Right Child ?] If PTRRIGHT NULL then
(a) Set PTR = PTRRIGHT
(b) Go to Step 2
[7] Set PTR = STACK[TOP], TOP = TOP -1
[8] Exit
45
In-order Traversal with a stack
[1] Push the Left-most path from ROOT onto STACK
[2] While STACK is not empty
(a) Pop and process node X
(b) If Right Child of X (say Y) exists then
Push left most path from Y onto STACK
46
In-order Traversal with a stack
[1] Push the Left-most path from ROOT onto STACK
[2] While STACK is not empty
(a) Pop and process node X
(b) If Right Child of X (say Y) exists then
Push left most path from Y onto STACK
Step Output Stack
1 A,B,D,G,K
A 2 K A,B,D,G
3 G A,B,D
B C
4 D A,B,H,L
5 L A,B,H
D E
6 H A,B,M
7 M A,B
G H 8 B A
9 A C,E
K L M 10 E C
11 C --
47
Self Study
Write an algorithm to traverse a binary tree in postorder traversal
using stack. Discuss the algorithm with an example.