Data Structure and Algorithm (CS-102) : Ashok K Turuk
Data Structure and Algorithm (CS-102) : Ashok K Turuk
Algorithm (CS-102)
Ashok K Turuk
Discussed So far
• Here are some of the data structures
we have studied so far:
– Arrays
– Stacks, Queues and deques
– Linked list
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
4
A Level 1
B C D E Level 2
F G H I J K Level 3
L Level 4
5
Tree
6
Basic terminologies
11
Tree Terminology(continued)
13
Tree Terminology
• Ancestor of a Node: Those nodes that
occur on the path from the root to the
given node
B C D E Level 2
F G H I J K Level 3
15
Representation of a tree
• List Representation
16
LINK 1 LINK 2 LINK n
…
DATA
B C D E
F G H I J K
L
(b) Linked list representation of the tree 17
An alternative elegant linked representation
18
An alternative elegant linked representation
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 C Level 2
D E F G Level 3
22
Important observations regarding binary
trees:
1 A
2 B
3
C
D E 6 F
4 5 7 G
24
A binary tree with n’ nodes and height h is
complete if its nodes correspond to the nodes
which are numbered 1 to n (n’ n) in a full
binary tree of height h.
Height of a complete
1 A binary tree with n
given by
2 B C 3 h log 2 (n 1)
4 D
5 E 6 F
25
A complete binary tree obeys the following
properties with regard to its node
numbering:
26
A complete binary tree obeys the following
properties with regard to its node
numbering:
27
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
29
Representation of Binary Tree
30
Representation Of Binary Trees
Array Representation
1
a
Sequential representation
2
of a tree with depth d will
b require an array with
4 5 approx 2d + 1 elements
c d
10
e f
11
1 2 3 4 5 6 7 8 9 10 11
a b c d e f
31
Linked representation
T
LCHILD DATA RCHILD
b
c d
e f
32
• Observation regarding the linked
representation of Binary Tree
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
37
Illustrations for Traversals (Contd.)
• Inorder: 2 3 6 7 8 10 11 30
6 10 12 22
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
38
Formulation of Binary tree from
Its traversal
1. If preorder is given=>First node is the
root
If postorder is given=>Last node is the
root
39
4.Two traversals are essential out of
which one should inorder, another may
be preorder or postorder
40
Example: For Given Inorder and
Preorder
Inorder: D B H E A I F J 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
41
continues.
A
In:D B H E IFJCG
Pre:B D E H CFIJG
D HE IFJ G
EH FIJ
I J
H
42
Example: For Given Inorder and
Postorder
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
44
Traversal Algorithm Using Stack
Assumption
46
Pre-Order Traversal
[4] [Right Child ?]
If PTR -> RIGHT NULL, then [Push
on STACK]
SET TOP = TOP + 1
STACK[TOP] = PTR->RIGHT
[5] [Left Child ?]
If PTR->LEFT NULL, Then
Set PTR = PTR->LEFT
Else
Set PTR = STACK[TOP],
TOP = TOP-1
[6] Exit
47
Pre-Order Traversal
A
B C
D E F
G H
K
48
[1] Initially push NULL onto STACK
STACK = φ
Set PTR = A , the root of T
[2] Proceed down the left-most path rooted at
PTR = A as follows
(i) Process A and Push its right child C onto
STACK:
STACK: φ, C
(ii) Process B. (There is no Right Child)
(iii) Process D and push its Right Child H
onto STACK. STACK: φ, C, H
(iv) Process G (There is no right child)
49
[3][Backtracking] Pop the top element H from
STACK, and set PTR = H
STACK: φ, C
51
[9] [Backtracking] Pop the top element F
from STACK, and set PTR = F
STACK: φ
[10] Proceed down the left-most path
rooted at PTR = F as follows
(ix) Process F. (There is no right child)
[No other node is processed, since F has
no left child]
[11] [Backtracking] Pop the top element
NULL from STACK, and set PTR = NULL
52
In-order Traversal
[1] [Push NULL onto STACK and initialize
PTR]
Set TOP =1, STACK[1] = NULL, PTR =
ROOT
[2] Repeat while PTR NULL [Pushes 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]
53
In-order Traversal
[4] Repeat Steps 5 to 7 while PTR
NULL: [Backtracking]
[5] Apply PROCESS to PTR->INFO
[6] [Right Child ?] If PTR->RIGHT NULL
then
(a) Set PTR = PTR->RIGHT
(b) Go to Step 2
[7] Set PTR = STACK[TOP], TOP = TOP -1
[8] Exit
54
In-Order Traversal
A
B C
D E
G H
K L M
55
[1] Initially Push NULL onto STACK
STACK = φ
Set PTR = A , the root of T
[2] Proceed down the left-most path rooted at
PTR = A, pushing the nodes A, B, D, G and K
onto STACK:
STACK = φ, A, B, D, G, K
[3] [Backtracking] The nodes K, G and D are
popped and processed
STACK = φ, A, B
Set PTR = H [Right Child of D]
[4] Proceed down the left-most path rooted at
PTR = H, pushing the nodes H and L onto
STACK:
STACK = φ, A, B, H, L
56
[5] [Backtracking] Nodes L and H are
popped and processed
STACK = φ, A, B
Set PTR = M, the Right child of H
[6] Proceed down the left-most path rooted
at PTR = M, pushing node M onto STACK
STACK = φ, A, B, M
[7] [Backtracking] Nodes M, B and A are
popped and processed
STACK = φ
Set PTR = C, the Right child of A
57
[8] Proceed down the left-most path
rooted at PTR = C, pushing node C and E
onto STACK
STACK = φ, C, E
[9] [Backtracking] Nodes E and C are
popped and processed.
58