SlideShare a Scribd company logo
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
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
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
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
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
284 Data Structures Using C
2
4 5 6 7
12111098
3
1
LEFT DATA RIGHT
–1
–1
–1
5
9
20
1
–1
–1
–1
–1
–1
2
8
10
1
2
3
4
7
9
5
11
12
6
–1
–1
–1
18
12
14
8
–1
–1
16
ROOT
3
AVAIL
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
11
Figure 9.10 Binary tree T Figure 9.11 Linked representation of binary tree T
example 9.1
Solution
LEFT NAMES RIGHT
12
9
19
1
–1
–1
6
–1
–1
–1
11
–1
13
17
–1
–1
–1
20
–1
–1
–1
15
ROOT
3
AVAIL
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Pallav
Amar
Deepak
Janak
Kuvam
Rudraksh
Raj
Kunsh
Tanush
Ridhiman
Sanjay
Pallav
Amar
Deepak
Janak
KuvamRudraksh
Raj
KunshTanushRidhiman
Sanjay
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
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
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
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)
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
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
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
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
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
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

More Related Content

PDF
Merrk
PDF
Números primos repunits
PPTX
PPT
Chapter 3 2
PDF
LAC2013 UNIT preTESTs!
PDF
Mathematicalfoundationofcomputerscience
PPT
Merrk
Números primos repunits
Chapter 3 2
LAC2013 UNIT preTESTs!
Mathematicalfoundationofcomputerscience

What's hot (20)

PDF
Appendex f
PDF
Logarithms
PDF
Massively distributed environments and closed itemset mining
PPTX
20130108 ast signature based boolean matching in the presence of don’t cares_...
PPTX
Data made out of functions
PDF
Gate-Cs 1996
PPSX
INVERSE FUNCTION
PPTX
PPT
PPT
presentation about set theorem
PDF
AP PGECET Computer Science 2016 question paper
PPT
Review session2
PPTX
Chapter 22 Finite Field
PPSX
FUNCTION- Algebraic Function
PPTX
K - Map
PDF
Ee693 questionshomework
PPT
Directed Acyclic Graph
PPTX
Week 8 (trees)
PPT
UNIT .01
PPTX
Digital Logic Design-Lecture 5
Appendex f
Logarithms
Massively distributed environments and closed itemset mining
20130108 ast signature based boolean matching in the presence of don’t cares_...
Data made out of functions
Gate-Cs 1996
INVERSE FUNCTION
presentation about set theorem
AP PGECET Computer Science 2016 question paper
Review session2
Chapter 22 Finite Field
FUNCTION- Algebraic Function
K - Map
Ee693 questionshomework
Directed Acyclic Graph
Week 8 (trees)
UNIT .01
Digital Logic Design-Lecture 5
Ad

Similar to Chapter 09-Trees (20)

PPT
Data Structure And Algorithms for Computer Science
PPTX
PPTX
Data Structure in Tree form. .pptx
PPTX
lecture 17,18. .pptx
PPTX
lecture 17,18. .pptx
PPT
BINARY TREE REPRESENTATION.ppt
PPT
Chap 5 Tree.ppt
PPT
PDF
Chapter 5_Trees.pdf
PPT
Data Structures 4
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
358 33 powerpoint-slides_10-trees_chapter-10
PPT
9. TREE Data Structure Non Linear Data Structure
PPTX
Unit-VStackStackStackStackStackStack.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
PPT
ds 10-Binary Tree.ppt
PPTX
Tree structure and its definitions with an example
PPTX
Unit 6 tree
Data Structure And Algorithms for Computer Science
Data Structure in Tree form. .pptx
lecture 17,18. .pptx
lecture 17,18. .pptx
BINARY TREE REPRESENTATION.ppt
Chap 5 Tree.ppt
Chapter 5_Trees.pdf
Data Structures 4
UNIT III Non Linear Data Structures - Trees.pptx
358 33 powerpoint-slides_10-trees_chapter-10
9. TREE Data Structure Non Linear Data Structure
Unit-VStackStackStackStackStackStack.pptx
UNIT III Non Linear Data Structures - Trees.pptx
ds 10-Binary Tree.ppt
Tree structure and its definitions with an example
Unit 6 tree
Ad

More from MuhammadBakri13 (12)

PDF
Uxd01 uxd fundamental
PDF
Chapter 4 stack and queue
PPTX
Views in MySQL
PDF
Slide 03: Data Management
PDF
Chapter 02 - Database Development
PDF
Slide 01-Data and the Enterprise
PDF
Chapter 14 Searching and Sorting
PDF
Python Objects
PDF
PDF
Conditional Statements
PDF
Function
PDF
Introduction to Python
Uxd01 uxd fundamental
Chapter 4 stack and queue
Views in MySQL
Slide 03: Data Management
Chapter 02 - Database Development
Slide 01-Data and the Enterprise
Chapter 14 Searching and Sorting
Python Objects
Conditional Statements
Function
Introduction to Python

Recently uploaded (20)

DOCX
Double Membrane Roofs for Biomethane Storage Holds upgraded biomethane fuel.docx
PPTX
Plant_Cell_Presentation.pptx.com learning purpose
DOCX
Double Membrane Roofs for Bio-gas Tanks Reliable containment for biofuel gas....
PPTX
RadiationSafetyPt120252026nucchemis.pptx
PPTX
EME Aerospace.pptx basics of mechanical engineering
PPTX
102602734019608717246081273460745534.pptx
PPTX
AUTO IRRIGATION USING GRID SYSTEM123.pptx
PPTX
Waste management needs, techniques, ways
DOCX
Double Membrane Roofs for Agricultural Waste Biogas Digesters Turns various f...
PPTX
Biodiversity of nature in environmental studies.pptx
PDF
Tree Biomechanics, a concise presentation
PPTX
Minor Species of nutmeg, cinnamon and clove
DOCX
Double Membrane Roofs for Bio CNG Plants Stores biogas.docx
PPTX
"One Earth Celebrating World Environment Day"
PPT
PPTPresentation3 jhsvdasvdjhavsdhsvjcksjbc.jasb..ppt
PPTX
Arugula. Crop used for medical plant in kurdistant
PPTX
Conformity-and-Deviance module 7 ucsp grade 12
DOCX
Double Membrane Roofs for Cassava Wastewater Treatment Captures biogas from i...
PDF
1748933543SJA_41_2_826-834 SJA Ihsan ullha.pdf
PDF
Global Natural Disasters in H1 2025 by Beinsure
Double Membrane Roofs for Biomethane Storage Holds upgraded biomethane fuel.docx
Plant_Cell_Presentation.pptx.com learning purpose
Double Membrane Roofs for Bio-gas Tanks Reliable containment for biofuel gas....
RadiationSafetyPt120252026nucchemis.pptx
EME Aerospace.pptx basics of mechanical engineering
102602734019608717246081273460745534.pptx
AUTO IRRIGATION USING GRID SYSTEM123.pptx
Waste management needs, techniques, ways
Double Membrane Roofs for Agricultural Waste Biogas Digesters Turns various f...
Biodiversity of nature in environmental studies.pptx
Tree Biomechanics, a concise presentation
Minor Species of nutmeg, cinnamon and clove
Double Membrane Roofs for Bio CNG Plants Stores biogas.docx
"One Earth Celebrating World Environment Day"
PPTPresentation3 jhsvdasvdjhavsdhsvjcksjbc.jasb..ppt
Arugula. Crop used for medical plant in kurdistant
Conformity-and-Deviance module 7 ucsp grade 12
Double Membrane Roofs for Cassava Wastewater Treatment Captures biogas from i...
1748933543SJA_41_2_826-834 SJA Ihsan ullha.pdf
Global Natural Disasters in H1 2025 by Beinsure

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
  • 6. 284 Data Structures Using C 2 4 5 6 7 12111098 3 1 LEFT DATA RIGHT –1 –1 –1 5 9 20 1 –1 –1 –1 –1 –1 2 8 10 1 2 3 4 7 9 5 11 12 6 –1 –1 –1 18 12 14 8 –1 –1 16 ROOT 3 AVAIL 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 11 Figure 9.10 Binary tree T Figure 9.11 Linked representation of binary tree T example 9.1 Solution LEFT NAMES RIGHT 12 9 19 1 –1 –1 6 –1 –1 –1 11 –1 13 17 –1 –1 –1 20 –1 –1 –1 15 ROOT 3 AVAIL 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Pallav Amar Deepak Janak Kuvam Rudraksh Raj Kunsh Tanush Ridhiman Sanjay Pallav Amar Deepak Janak KuvamRudraksh Raj KunshTanushRidhiman Sanjay
  • 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