Call Girls Talegaon Dabhade Call Me 7737669865 Budget Friendly No Advance Boo...
Chapter 09-Trees
1. 9.1 INTRODUCTION
A B, C D
A
9.1.1 Basic Terminology
Root node R R = NULL
Sub-trees R NULL T1
, T2
, T3
R
Leaf node
Path path.
A I A, D I
Ancestor node
A, C G
K
LeaRNINg OBjeCTIve
So far, we have discussed linear data structures such as arrays, strings, stacks,
and queues. In this chapter, we will learn about a non-linear data structure called
tree. A tree is a structure which is mainly used to store data that is hierarchical in
manipulate arithmetic expressions, construct symbol tables, and for syntax analysis.
Trees
chapTer 9
2. 280 Data Structures Using C
Descendant node A
C, G, J K A
Level number level number
level number + 1
Degree
In-degree
Out-degree
9.2 TYPeS OF TReeS
9.2.1 general Trees
9.2.2 Forests
B
E F H I
D
A
T1
Root node
G
KJ
C
T3
T2
Figure 9.1
3. Trees 281
9.2.3 Binary Trees
'root' root = NULL
R T1
T2
R T1
R T2
R
Terminology
Parent N T left successor S1
right successor S2
N parent S1
S2
S1
S2
N
Level number level
number
parent's level number + 1
Degree of a node
Sibling
siblings
Root node
2
4 5 6 7
12111098
3
1
T1 T2
Figure 9.3 Binary tree
B
F
E
A
D
H
CB
F G
E
D
H
C
(a) (b)
G
Figure 9.2 Forest and its corresponding tree
2
4 5 6 7
12111098
3
1
Root node
(Level 0)
(Level 1)
(Level 2)
(Level 3)
Figure 9.4 Levels in binary tree
4. 282 Data Structures Using C
Leaf node
Similar binary trees T T¢
similar binary trees
Copies T T¢ copies
T¢ T
Edge N
n n – 1
Path .
Depth depth N
R N
Height of a tree
h h 2h
– 1
h 2h
– 1
n log2
(n+1) n.
In-degree/out-degree of a node
out-degree
Complete Binary Trees
A complete binary tree
Tn
n r T 2r
20
= 1 21
= 2 22
= 4
6 23
= 8
T13
K
2 × K
2 × K + 1
4 8 (2 × 4) 9 (2 × 4 + 1)
K | K/2 |
4 | 4/2 | = 2
Tn
Hn
= | log2
(n + 1) |
E
A
CB
D
Tree T Tree T¢
J
F
HG
I
Figure 9.5 Similar binary
trees
ED
A
CB
Tree T Tree T¢
ED
A
CB
Figure 9.6 ¢ is a copy
1
2 3
4 5 6 7
8 9 10 11 12 13
Figure 9.7 Complete binary tree
5. Trees 283
T
Extended Binary Trees
T
internal nodes external
nodes
Representation of Binary Trees in the Memory
Linked representation of binary trees
struct node {
struct node *left;
int data;
struct node *right;
};
ROOT
ROOT = NULL
X NULL
1
2 3
4 5 6 7
X 8 9 10 12X X X X X X XX X11
X X X
Figure 9.9 Linked representation of a binary tree
(a) (b)
Figure 9.8 (a) Binary tree and (b) extended
binary tree
7. Trees 285
Sequential representation of binary trees
∑ TREE
∑
is, TREE[1]
∑ K
(2 × K) (2 × K+1)
∑ TREE (2h
–1)
h height
∑ NULL TREE[1]
= NULL
9.2.4 Binary Search Trees
A
9.2.5 expression Trees
Exp = (a – b) + (c * d)
example 9.2 Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h))
Solution
%
+ ^ –
a b c d f g h i
–
Expression tree
/
example 9.3
Solution
%/
+
^
–
a b c d f g h i
/
20
15
12 17
16 18
35
21 39
36 45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
20
15
35
21
39
16
18
12
17
36
45
Figure 9.12 Binary tree and its sequential
representation
+
a b c d
Figure 9.13 Expression tree
8. 286 Data Structures Using C
[{(a/b) + (c*d)} ^ {(f % g)/(h – i)}]
example 9.4 Exp = a + b / c * d – e
Solution
Exp = ((a + ((b/c) * d)) – e)
9.2.6 Tournament Trees
n
selection tree
winner trees
loser tree
a, b, c, d, e, f, g h a b c d e
f g h
a, d, e
a e
a
9.3 CReaTINg a BINaRY TRee FROm a geNeRaL TRee
Rule 1:
Rule 2:
Rule 3:
example 9.5
+
/
a
b c
d
–
e
Expression tree
a b c d e f g h
a
a
a d e
e
g
Figure 9.14
B
E F I J
D
A
K
G H
C
9. Trees 287
Step 1: A
Step 2: A A
A A A
Step 3: B B is E C
Step 4: C C is F D
Step 5: D D is I
D
Step 6: I I I
I J
I
Step 7: J J is K
J
DF
B
CE
A
DF
B
CE
A
I
DF
B
CE
A
I
J
DF
B
CE
A
I
J
K K
B
CE
A
DF
IG
H J
Step 8: E, F, G, H, K
9.4 TRaveRSINg a BINaRY TRee
9.4.1 Pre-order Traversal
A
A
B
B
CE
A
B C
A
Figure 9.15 Binary tree
10. 288 Data Structures Using C
A, B, C
depth-first traversal
+ – a b * c d (from Fig. 9.13)
% – + a b * c d / ^ e f – g h (from Fig of Example 9.2)
^ + / a b * c d / % f g – h i (from Fig of Example 9.3)
example 9.6
Solution
TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J,
and K
TRAVERSAL ORDER: A, B, D, C, D, E, F, G, H, and I
9.4.2 In-order Traversal
B, A C
symmetric traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE DATA
Step 3: PREORDER(TREE LEFT)
Step 4: PREORDER(TREE RIGHT)
[END OF LOOP]
Step 5: END
->
->
->
Figure 9.16 Algorithm for pre-order traversal
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: INORDER(TREE LEFT)
Step 3: Write TREE DATA
Step 4: INORDER(TREE RIGHT)
[END OF LOOP]
Step 5: END
->
->
->
Figure 9.17 Algorithm for in-order traversal
A
B C
D E F
G H I J
KL
G
H I
B C
D E
F
A
(a) (b)
11. Trees 289
example 9.7
TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and J
TRAVERSAL ORDER: B, D, A, E, H, G, I, F, and C
9.4.3 Post-order Traversal
B, C,
A
LRN
example 9.8
TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A
TRAVERSAL ORDER: D, B, H, I, G, F, E, C, and A
9.4.4 Level-order Traversal
breadth-first traversal algorithm
TRAVERSAL ORDER:
A, B, C, D, E, F, G, H, I, J, L, and K
(b)(a)
TRAVERSAL ORDER:
A, B, and C
TRAVERSAL ORDER:
A, B, C, D, E, F, G, H, and I
(c)
A
CB
HG
B
ED
L
JI
C
F
K
A
B
D
G
C
F
A
E
IH
Figure 9.19 Binary trees
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: POSTORDER(TREE LEFT)
Step 3: POSTORDER(TREE RIGHT)
Step 4: Write TREE DATA
[END OF LOOP]
Step 5: END
->
->
->
Figure 9.18 Algorithm for post-order traversal
12. 290 Data Structures Using C
9.4.5 Constructing a Binary Tree from Traversal Results
In–order Traversal: D B E A F C G Pre–order Traversal: A B D E C F G
Step 1
Step 2
Step 3
A
DBHE FJCG
A
DBHE C
IF G
A
DBHE C
F G
I
A
B
D F G
J
HEI
A
B C
D E
H
I J
I I
C
Figure 9.21 Steps to show binary tree
9.5 HUFFmaN’S TRee
'0'
A
DBE FCG
D E
A
B FCG
A
B C
D E F G
Figure 9.20
13. Trees 291
'1'
n n – 1
external path length
internal path length
LI
= 0 + 1 + 2 + 1 + 2 + 3 + 3 = 12
LE
= 2 + 3 + 3 + 2 + 4 + 4 + 4 + 4 = 26
LI
+ 2 * n = 12 + 2 * 7 = 12 + 14 = 26 = LE
LI
+ 2n = LE
n n
P
P = W1
L1
+ W2
L2
+ …. + Wn
Ln
Wi
Li
Ni
example 9.9 T1
, T2
T3
5 2
2 3 11 5
2
3 4
5 7 5
2 3
11
T1 T2 T3
Binary tree
Solution
T1
P1
= 2◊3 + 3◊3 + 5◊2 + 11◊3 + 5◊3 + 2◊2 = 6 + 9 + 10 + 33 + 15 + 4 = 77
T2
P2
= 5◊2 + 7◊2 + 3◊3 + 4◊3 + 2◊2 = 10 + 14 + 9 + 12 + 4 = 49
T3
P3
= 2◊3 + 3◊3 + 5◊2 + 11◊1 = 6 + 9 + 10 + 11 = 36
Technique
n
Figure 9.22 Binary tree
14. 292 Data Structures Using C
Step 1: Create a leaf node for each character. Add the character and its weight or frequency
of occurrence to the priority queue.
Step 2: Repeat Steps 3 to 5 while the total number of nodes in the queue is greater than 1.
Step 3: Remove two nodes that have the lowest weight (or highest priority).
Step 4: Create a new internal node by merging these two nodes as children and with weight
equal to the sum of the two nodes' weights.
Step 5: Add the newly created node to the queue.
Figure 9.23 Huffman algorithm
example 9.10
A
7
B
9
C
11
D
14
E
18
F
21
G
27
H
29
I
35
J
40
C
11
D
14
E
18
F
21
G
27
H
29
I
35
J
40
16
A
7
B
9
25
A
7
B
9
16
A
7
B
9
C
11
D
14
E
18
F
21
G
27
H
29
I
35
J
40
25
F
21
G
27
H
29
I
35
J
40
E
18
C
11
D
14
16
34
C
11
D
14
25
46
F
21
G
27
H
29
I
35
J
40
E
18
16
34
A
7
B
9
15. Trees 293
86
25
46 J
40
C D
F
21
56
G
27
H
29
I
35
69
E16
34
125
211
11 1418
A
7
B
9
5646
I
35
J
40
34
J
40
46 56 69
A
7
B
9
8656 69
86
25
46 J
40
C
11
D
14
F
21
56
G
27
H
29
I
35
69
E
18
A
7
B
9
16
34
125
25
46 J
40
G
27
H
29
C
11
D
14
F
21
I
35
E
18
A
7
B
9
16
34
25 G
27
H
29
C
11
D
14
I
35
E
18
16
34F
21
C
11
D
14
25 F
21
G
27
H
29
E
18
16
A
7
B
9
16. 294 Data Structures Using C
Data Coding
r 2r
r=1
A B
B
r=2 r=3
ABBBBBBAAAACDEFGGGGH
000001001001001001001000000000000010011100101110110110110111
a, e,
i r w, x, y, z
A, E, R, W, X, Y,
Z
A E R
X Y W Z
0 1
0 1
0 0
0
0
1 1
1
1
Figure 9.24 Huffman tree
9.6 aPPLICaTIONS OF TReeS
∑
Table 9.3 Characters with their codes
Character Code
A 00
E 01
R 11
W 1010
X 1000
Y 1001
Z 1011
Table 9.1 Range of characters that
can be coded using r = 2
Code Character
00 A
01 B
10 C
11 D
Table 9.2 Range of characters that
can be coded using r = 3
Code Character
000 A
001 B
010 C
011 D
100 E
101 F
110 G
111 H