You are on page 1of 131

DATA STRUCTURES AND ALGORITHMS

Week 10-11: Binary Trees &


Applications
Romana Talat
DEPARTMENT OF COMPUTER SCIENCE
BAHRIA UNIVERSITY, ISLAMABAD

1
Real world DATA STRUCTURE

2
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.

3
A Tree

4
Examples: directory structure

5
Organizational organograms

6
Definition

A tree is a finite set of nodes, such that:


◦ There is a special node called the root.
◦ The remaining nodes are partitioned into n ≥ 0 disjoint sets T1, T2, ......., Tn.
◦ Each of the sets T1, T2, ......., Tn is itself a tree (subtrees of root).

7
Trees - Definition and Terminology
Tree: A finite set of elements called nodes (or vertices)
and a finite set of directed arcs that connect pairs of
nodes. If the tree is not empty, one of the nodes, called the
root, has no incoming arcs.

Leaf: Node with no outgoing arcs.

Nodes directly accessible (using one arc) from a node are called
the children of that node, which is called the parent of these
children; these nodes are siblings of each other.

8
1 root

2 3

4 5 6 7 children of this parent


siblings of each other

8 9

leaves

9
A

Tree Terminologies B C

Root
◦ It is the key node of a tree structure that 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.

10
A
Tree Terminologies
B C

Child
◦ All successor nodes are called child nodes. In the figure B and C
are the child nodes of A
Sibling
◦ The child node of same parent are called sibling.
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 / Terminal Node
◦ This node is located at the end of the tree. It does not have any
child hence it is called leaf node.

11
Tree Terminologies
Level
◦ Level is the rank of tree hierarchy. The whole tree structure 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.

Depth
◦ The depth of a node n is the length of the path from the root to the node.
◦ The depth (or height) of a tree is the length of the path from the root to the
deepest node in the tree.
Root
node
Interior Heig
nodes ht
Leaf
nodes

12
Levels and depth
Root

Level 0 Node

Level 1
Arc

Level 2

Level 3

Leaf

13
Binary Trees
A binary tree is a tree in which each node has at most 2 children.

root
O

M T
left subtree right subtree
of node O of node O
C E • P U

left subtree right subtree


of node M of node M

14
Binary Tree Examples

15
Binary Trees
If A is the root of a binary tree and B is the root of its left or right
subtree, then A is said to be father of B, and B is said to be the
left or right son of A.

left son of A B C right son of A

D E • F G


left son of B right son of B

16
Binary Trees
A node having no sons is called a leaf of the binary tree.

B C

D E F G

leave
s

17
Binary Tree

18
Binary Trees
Node n1 is an ancestor of node n2 (and n2 is descendent of n1) if n1
is either the father of n2 or the father of some ancestor of n2.

A
✔A is ancestor of D
B C ✔H is descendent of B

D E F G ✔G is ancestor of J

❖F is ___ of H ???
H I J

19
Binary Trees
⮚A node n2 is left descendent of node n1 if n2 is either the left son of n1
or a descendent of the left son of n1.

⮚Similar definition for right descendent.

⮚Two nodes are brothers if they are left and right sons of the same
father.

20
Binary Trees
A
⮚G is rightdescendent of A.

B C
⮚E is left descendent of A.

⮚H is rightdescendent of B.
D E F G
⮚I is rightdescendent of C.
H I J
⮚H is left descendent of E.

21
Examples – familiarization
with notation
Root A

B C

D E F

Left Subtree of A
G H I

Right Subtree of A
22
EXAMPLES – FAMILIARIZATION WITH
NOTATION

A
An Edge

B C

D E F

G H I

23
EXAMPLES – FAMILIARIZATION WITH
NOTATION

A
Child nodes of C?
B C
Child nodes of A?

D E F

G H I

24
EXAMPLES – FAMILIARIZATION WITH
NOTATION

Descendants of Node C? A
Descendants of A?
B C

D E F

G H I

25
EXAMPLES – FAMILIARIZATION WITH
NOTATION

Parent of Node E?
A
Parent of Node H?

B C

D E F

G H I

26
EXAMPLES – FAMILIARIZATION WITH
NOTATION

Ancestors of Node D? A
Ancestors of Node H?
B C

D E F

G H I

27
EXAMPLES – FAMILIARIZATION WITH
NOTATION

A path from J to A
A

Path: B C
If n1, n2,…nk is a sequence of
nodes such that ni is the parent
D E F
( child) of ni+1, then that sequence
is a path.

G H I

J
28
EXAMPLES – FAMILIARIZATION WITH
NOTATION

Leaf node: A
Any node that has empty children

B C

D E F

G H I

29
EXAMPLES – FAMILIARIZATION WITH
NOTATION

B C

D E F

Internal node:
Any node that has at least one
G H I
non-emptyChild

30
Examples – familiarization with notation

31
Example Binary Trees

One node Two nodes

Three
nodes

32
Types of Binary Trees
Strictly Binary Trees
Complete Binary Trees

33
Strictly Binary Tree
If every non-leaf node in a binary tree has nonempty left and right
subtrees, the tree is known as Strictly Binary Tree.

A A

B C B C

D E F G D E F G

H I J I J

Strictly Binary Tree?? ✔Strictly Binary Tree

34
Binary Trees (recall)
Level:

The root of the tree has level 0, and the level of any other node in
the tree is one more than the level of its father.

A
Level
0

B C Level
1
Level D E F G
2
I J

35
Binary Trees (recall)
Depth:

The depth of a binary tree is the maximum level of any


leaf in the tree.

B C

D E F G

Depth: 3 I J

36
Complete Binary Tree
A complete binary tree of depth d is a strictly binary tree all
of whose leaves are at level d.

B C

D E F G

A complete binary tree of depth 2

37
Almost Complete Binary Tree
A binary tree (of depth d) is an almost complete
binary tree if:
1. Each leaf in the tree at level d or at level d-1.
2. For any node ‘n’ in the tree with a right descendent at level d, all the left
descendents of ‘n’ are also at level d. (The tree is completely filled from
the left)

38
Binary Trees
⮚If a binary tree contains m nodes at level l, it can contain
at the most 2m nodes at level l+1.

⮚Since a binary tree can have at the most one node at level 0, It can
have at the most 2l nodes at level l.

⮚A complete binary tree of depth d is a binary tree that contains


exactly 2l nodes at each level between 0 and d.

⮚A complete binary tree contains exactly 2d nodes at level d, that is;


2d leaves.

39
Binary Trees
A

B C

D E F G

A Complete Binary Tree…


Dept d = 2
No of nodes at level i = 2i
No of leaves = 2d = 22 = 4

40
Binary Trees
A

B C

D E F G
. . . .
. . . .
. . . .
Total Number of nodes in a complete binary tree:

N = 20+21+22+ ……+2d = j=0 Σd 2 j

= 2 d+1- 1

41
Binary Trees
Number of nodes in a complete binary tree:

N = 2 d+1- 1

Given the number of nodes of a complete binary tree,


its depth d would be:

d = log2(N + 1) -1

42
BINARY TREE ADT
Create
◦ Create an empty binary tree

Empty
◦ Return true when binary tree is empty else return false.

Tree Traversal
◦ Inorder Traversal
◦ Preorder Traversal
◦ Postorder Traversal

Insert
◦ To insert a node

Deletion
◦ To delete a node

Search
◦ To search a given node

Copy
◦ Copy one tree into another

43
Tree Traversal
Moving through a tree, visiting each node exactly once

• Preorder Traversal
• Inorder Traversal
• Postorder Traversal

44
Traversal of a Binary Tree
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

45
Traversal of a Binary Tree

46
Tree Traversal
Preodrder:

✔Visit the root


✔Traverse the left subtree in preorder
✔Traverse the right subtree in preorder

Also known as depth-first traversal.

47
✔Visit the root
✔Traverse the left subtree in preorder
Preorder Traversal ✔Traverse the right subtree in preorder

32

left subtree 79 42 right subtree

13 95 16

Output: 32
Problem now reduced to traversal of two smaller binary trees.

48
Preorder Traversal
79 ⮚Visit root
⮚Traverse left subtree
⮚Traverse right subtree

13

Output: 32 79

•Left subtree is empty


•Now, traverse the right subtree

49
Preorder Traversal
⮚Visit root
13
⮚Traverse left subtree
⮚Traverse right subtree

Output: 32 79 13

50
Preorder Traversal
32

79 42

13 95 16

Output: 32 79 13

51
Preorder Traversal
42 Output: 32 79 13 42

Output: 32 79 13 42 95

16
Output: 32 79 13 42 95 16
95

95 16

52
Preorder Traversal
32

79 42

13 95 16

Output: 32 79 13 42 95 16

53
Preorder Traversal
32

79 42

13 95 16

32 79 13 42 95 16

54
Tree Traversal
Inorder:

✔Traverse the left subtree in inorder


✔Visit the root
✔Traverse the right subtree in inorder

Also known as Symmetric order.

55
Inorder Traversal
32 ✔Traverse the left subtree
✔Visit the root
✔Traverse the right subtree

79 42

13 95 16

79 13 32 95 42 16

56
Tree Traversal
Postorder:

✔Traverse the left subtree in postorder


✔Traverse the right subtree in postorder
✔Visit the root

57
Postorder Traversal
32
✔Traverse the left subtree
✔Traverse the right subtree
✔Visit the root

79 42

13 95 16

13 79 95 16 42 32

58
Tree Traversal
Preorder:

root – left – right

Postorder:

left – right – root

Inorder:

left – root – right

59
Tree Traversal
A
Preorder: A B D G C E F H

Inorder: D G B A E C H F
B C
Postorder: G D B E H F C A

D
E F

G
H

60
Tree Traversal
1
5 1
5 6
2
3 1
0
2 2
1 1 1
3
0 3 8
6

Pre-order: 15, 5, 3, 12, 10, 6, 7, 13, 16, 20, 18, 23


Post-order: 3, 7, 6, 10, 13, 12, 5, 18, 23, 20, 16, 15
In-order: 3, 5, 6, 7, 10, 12, 13, 15, 16, 18, 20, 23
Binary Tree Applications
Expression trees

62
Binary Expression Tree
A special kind of binary tree in which:

◦ Each leaf node contains a single operand

◦ Each nonleaf node contains a single binary operator

◦ The root contains the operator that is to be applied to


the results of evaluating the expressions in the left and
right subtrees

63
Binary Expression Trees
Infix Tree
Postfix Tree

64
A binary expression tree
treePt
r

‘-’
‘8
’ ‘5’
INORDER TRAVERSAL: 8 - 5

PREORDER TRAVERSAL: - 8 5

POSTORDER TRAVERSAL: 8 5 -

65
A Binary Expression Tree
‘*’

‘3
‘+’ ’
‘4 ‘2
’ ’
Evaluate the expression?

( 4 + 2 ) * 3 = 18
66
A Binary Expression Tree
‘*’

‘3
‘+’ ’
‘4 ‘2
’ ’
What infix, prefix, postfix expressions does it
represent?

67
A Binary Expression Tree
‘*’

‘3
‘+’ ’
‘4 ‘2
’ ’
Infix: ((4+2)*3)
Prefix: * + 4 2 3
Postfix: 4 2 + 3 *
68
Representing Expressions
A * B + C A * (B + C) ((A + B) * C) / (D - E)

+ * /

* A * -
C +

A B + C D E
B C

A B
Δ
x Δ y
x y 69
A Binary Expression Tree
Draw an expression tree for the following:
(a+b*c)+((d*e+f)*g)

70
Traverse the tree in Left-Right-Root order /
(postorder) to get RPN:
* -
A B + C * D E - /
+ C D E
Traverse tree in Root-Left-Right
/
order (preorder) to get prefix: A B

* -
/ * + A B- C D E

+ C D E
Traverse tree in Left-Root-Right /
order (inorder) to get infix A B
— must insert ()'s * -

( ((A + B)* C)/ (D - )E) + D E


C

A B 71
Binary expression tree
‘*’

‘-’ ‘/’

‘8 ‘5 ‘4
’ ’ ‘+’ ’
‘4 ‘2
’ ’
What infix, prefix, postfix expressions does it
represent? 72
Binary Expression Tree
‘*’

‘/’
‘-’
‘8 ‘5 ‘4
’ ’ ‘+’ ’
‘4 ‘2
’ ’
Infix: ((8-5)*((4+2)/4))
Prefix: *-85 /+424
Postfix: 85- 42+4/*
73
Conversion of an Infix expression into tree
The first step in building a parse tree is to break up the expression string into a list of
tokens. Using this information we can define four rules as follows:
If the current token is a '(', add a new node as the left child of the current node, and
descend to the left child.
If the current token is in the list ['+','-','/','*'], set the root value of the current node to
the operator represented by the current token. Add a new node as the right child of the
current node and descend to the right child.
If the current token is a number, set the root value of the current node to the number
and return to the parent.
If the current token is a ')', go to the parent of the current node.
((7+3)*(5-2))
Conversion of a postfix expression to tree
I. Scan the postfix expression from left to right.
II. Create a node Curr
III. Get the next symbol from the expression.
IV. If the symbol is an operand
A. Set this operand as data member of the node Curr
B. Push the node on the stack
V. If the symbol is an operator
A. T2 = Pop()
B. T1 = Pop()
C. Attach T1 to the left and T2 to the right of Curr
D. Set the operator as data member of the node Curr
E. Push Curr (with child nodes attached) onto the stack
VI.Repeat Steps I-V till end of expression
VII.Pop the (only remaining) node from the stack which is a pointer to
the root of the expression tree.

76
Conversion of Postfix Expression to tree
Expression: a b + c d e + * *

Next, c, d, and e are read, and for each a one-


node tree is created and a pointer to the
corresponding tree is pushed onto the + c d e
stack.
a b

Now a ‘+’ is read, so two trees are merged.

+ c +

a b d e

77
Expression: a b + c d e + * *

Continuing, a ‘*’ is read, so we pop two tree pointers and form a new tree
with a ‘*’ as root.

+ c + + *

a b d e a b c +

d e

78
Expression: a b + c d e + * *

Finally, the last symbol is read, two trees are merged, and a pointer to the
final tree is left on the stack.

+ * *

a b c + + *

d e a b c +

d e

79
Expression: a b + c d e + * *

‘a’ is an operand, a one-node tree is created and


pushed on the stack

‘b’ is again an operand, and is pushed on the a b


stack

Next, a ‘+’ is read, so two pointers to trees are


popped, a new tree is formed, and a pointer to +
it is pushed onto the stack.
a b

80
Binary Tree Applications
Huffman Coding & Decoding:
Motivation
◦ Fixed length codes
◦ Same number of bits – Some symbols may not occur or occur very rarely

Variable Length Codes


◦ Shorter codes for frequently occurring symbols
◦ Longer codes for less frequent symbols

Objective
◦ Reduce the overall number of encoded bits
◦ Compression

Named after Huffman, 1952


81
Huffman Coding
� Given a text with 5 unique symbols
� Occurrence probabilities – Computed from text

Character A B C D E
Probability 0.2 0.1 0.1 0.15 0.45

Objective: Generate the VLC Codes for the character set

82
Huffman Coding
1-Construct a list of one-node binary trees, one for each
character, the weight of each node being its probability of
occurence
2-Do the following n-1 times:
A- Find two trees T’ and T’’ with roots of minimal
weights w’ and w’’.

B - Replace these two trees with a binary tree whose root


is w’+w’’, and whose subtrees are T’ and T’’.

C - Label the pointers to these subtrees 0 and 1, respectively.

3- The code for character Ci is the bit string labeling a path


in the final binary tree from the root to the leaf for Ci

83
Huffman Coding
Character A B C D E
Probability 0.2 0.1 0.1 0.15 0.45

0. 0.
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

84
Huffman Coding

0.
2
0 1
0. 0.
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

85
Huffman Coding
0.
0.
3
2
5
0 1 0 1
0. 0.
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

86
Huffman Coding
0.
5
5
0 1

0.
0.
3
2
5
0 1 0 1
0. 0.
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

87
Huffman Coding
1.
0
0 1
0.
5
5
0 1

0.
0.
3
2
5
0 1 0 1
0. 0.
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

88
Huffman Coding
1.
0
0 1 Character Huffman
0. Code
5
5 A 011
0 1 B 000
0.
0. C 001
3
2 D 010
5
0 1 0 1
0. 0. E 1
0. 0. 0.
1 4
1 1 2
5 5
B C D A E

89
Huffman Decoding
1- Initialize a pointer p to the root of the Huffman tree.

2-While ! End of Message:

A- Let x be the next bit in the message


B- If x is Zero
Set p equal to its left child pointer
else
Set p equal to its right child pointer

C- If p points to a leaf
i. Display the character associated with the leaf
ii. Reset p to the root of the Huffman Tree

90
Huffman Decoding
1.
0
0 1 0 1 010 1 1 0 1 0
0.
5 0 1 010 1 1 0 1 0
5
0 1 D
0. 0 1 010 1 1 0 1 0
0.
3
2 D E
5
0 1 0 1
0. 0. 0 1 010 1 1 0 1 0
0. 0. 0.
1 4
1 1 2 D E A
5 5
B C D A E
0 1 010 1 1 0 1 0

D E A D
91
Char Freq
Huffman Coding E 125
T 93
Compressing a given text
A 80
Character counts in the text O 76
I 73
N 71
S 65
R 61
H 55
L 41
D 40
C 31
U 27

92
Ch
Freq
ar
E 125
T 93
A 80
O 76
I 73
N 71
S 65
R 61
H 55
L 41
D 40
C 31
U 27

C U
31 27
93
Ch
Freq
ar
E 125
T 93
A 80
O 76
I 73
N 71
S 65
R 61
58
H 55
L 41
D 40

C 31
U 27

58

C U
31 27
94
Ch
Freq
ar
E 125
T 93
81
A 80
O 76
I 73
N 71
S 65
R 61
58
H 55

L 41
D 40

81

D L

40 41 58

C U
31 27
95
Ch
Freq
ar
E 125
113
T 93
81
A 80
O 76
I 73
N 71
S 65
R 61

58
H 55

81 113

D L H

40 41 58 55

C U
31 27
96
Ch
Freq
ar
126
E 125
113
T 93
81
A 80
O 76
I 73
N 71

S 65
R 61

81 126 113

D L R S H

40 41 61 65 58 55

C U
31 27
97
Ch
Freq
ar
144
126
E 125
113
T 93
81
A 80
O 76

I 73
N 71

81 126 144 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
98
Ch
Freq
ar
156
144
126
E 125
113
T 93
81

A 80
O 76

156

A O

80 76 81 126 144 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
99
Ch
Freq
ar
174
156
144
126
E 125
113

T 93
81

156 174

A O T

80 76 81 93 126 144 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
100
Ch
Freq
ar
238
174
156
144
126

E 125
113

156 174
238
A O T E

80 76 81 93 126 144 125 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
101
Ch
Freq
ar
270
238
174
156

144
126

156 174 270


238
A O T E

80 76 81 93 126 144 125 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
102
Ch
Freq
ar
330
270
238

174
156

330

156 174 270


238
A O T E

80 76 81 93 126 144 125 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
103
Ch
Freq
ar
508
330

270
238

330 508

156 174 270


238
A O T E

80 76 81 93 126 144 125 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
104
Ch
Freq
ar
838

508
330

838

330 508

156 174 270


238
A O T E

80 76 81 93 126 144 125 113

D L R S N I H

40 41 61 65 71 73 58 55

C U
31 27
105
0 Char Freq Fixed Huff
1
E 125 0000 110
T 93 0001 011
0 1 A 0 80 0010 1 000
O 76 0011 001
I 73 0100 1011
0 1 0 1 0 N 1 71 0101 0 1010 1

S 65 0110 1001
A O T E
R 61 0111 1000
0 1 0 1 0 1 0 1
H 55 1000 1111
L 41 1001 0101
D L R S N I H
D 40 1010 0100
0 1
C 31 1011 11100
U 27 1100 11101
C U
Total 838 4.00 3.62
106
Char Freq Fixed Huff
E 125 0000 110
T 93 0001 011
A 80 0010 000
O 76 0011 001
Navg : Average number of bits to I 73 0100 1011
N 71 0101 1010
encode the given data
S 65 0110 1001
R 61 0111 1000
Nk : Number of bits in symbol k H 55 1000 1111
L 41 1001 0101
D 40 1010 0100
P(k): Probability of occurrence of C 31 1011 11100

symbol k U 27 1100 11101


Total 838 4.00 3.62
107
Hints on implementation
Nodes of Binary Tree
◦ Character
◦ Frequency of character

A priority queue
Nodes in a priority queue
◦ The lower the occurrence frequency, the higher the priority in the queue

class HuffNode
{
public:
char value;
int freq;
HuffNode * left, * right;

};
PriorityQueue myQueue;

108
Building the Tree

E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8

While priority queue contains two or more nodes


Create new node
Dequeue node and make it left subtree
Dequeue next node and make it right subtree
Frequency of new node equals sum of frequency of left and right children
Enqueue new node back into queue

109
Building the Tree

E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8

y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8

110
E i
1 1
Building the Tree

y l k . r s n a sp e
1 1 1 1 2 2 2 2 2 4 8

E i
1 1

k . r s n a sp e
1 1 2 2 2 2 2 4 8

E i
1 1 2
y l111
1 1
Building the Tree

2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i 1 1
1 1

r s n a 2 2 sp e
2 2 2 2 4 8
y l 2
E i
1 1
1 1
k .
1 1
112
Building the Tree

r s n a 2 2 sp e
2
2 2 2 2 4 8

E i y l k .

1 1 1 1 1 1

n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1
4
113

r s
2 2
Building the Tree

n a 2 sp e
2 4
2
2 2 4 8

E i y l k . r s
1 1 1 1 1 1 2 2

Repeating these steps………..

114
Building the Tree

10 16

4
6
e 8
2 2 2 sp 8
4 4
E i y l k . 4
1 1 1 1 1 1 r s n a
2 2 2 2

115
Building the Tree

26

16
10

4 e 8
6
8
2 2
2 sp 4 4

E i y l k . 4
r s n a
1 1 1 1 1 1
2 2 2 2

116
Building huffman tree
Dequeue the single node left in the queue
This tree contains the new code words for each character
Frequency of root node should equal number of characters in text
Perform a traversal of the tree to obtain new code words – Going left is
a 0 going right is a 1
Code word is complete when a leaf node is reached

117
Encoding using Huffman tree
Char Code
E 0000
i 0001 26
y 0010
l 0011 10
16
k 0100
. 0101 4
6
e 8
Space 011 2 2 8
e 10 2 sp 4 4
r 1100 E i y l k . 4
s 1101 r s n a
1 1 1 1 1 1
n 1110 2 2 2 2
a 1111

Can implement as a Term Project

118
Binary Tree Implementation
Using Array
Using Linked Nodes

119
A binary tree is a tree in which each node has at most 2
children

Array-Based Implementation:
An array can be used to store some binary trees. Number
the nodes level by level, from left to right,
0
O

1 2
M T

3 4 5 6
C E • P U


Store node #0 in array location 0, node #1 in array location 1, etc.

i 0 1 2 3 4 5 6 . ..
t [i ] O M T C E P U . ..

120
But, unless each level of the tree
is full so there are no "dangling
limbs," there can be much
wasted space in the array.

For example, this binary tree


contains the same characters
as before but requires 58
___
array positions for storage:
Max # nodes on level i:
2i i 0 1 2 3 4 5 6 7 8 9 1 11 1 1 1 1 1 1 1 1
t[i] E C M U 0 2 3T 4 5 6 7 8 9
In array representation,
children of i are at: 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
2i + 1, 2i + 2 0 1 2 3 4 5 6 7 8P 9 0 1 2 3 4 5 6 7 8 9

Parent of i is at: 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 …
(i - 1) / 2 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7O …

121
Linked Nodes Implementation:
Use nodes of the form
data

left right

Left Right child


child
And maintain a pointer to the root.

122
C++ Implementation:
class Node
{
public:
int data;
Node *left, *right;
};
class BinaryTree
{
public:
Node *root; // pointer to root node
BinaryTree();
Node* insert(int);
//Node* search(int); //tree type dependent
//void delete_node(int); //tree type dependent
void traverse(); //pre, in, post order
};

123
BinaryTree::BinaryTree()
{
root=NULL;
}
Node* BinaryTree::insert(int val)
{
Node *p= new Node;
p->data=val;
p->left=NULL;
p->right=NULL;
return p;
}
//Driver.cpp
Void main() 4
{
BinaryTree b; 6 7
b->root = b.insert(4);
b->root->left = b.insert(6); 9
b->root->right = b.insert(7);
b->root->right->left = b.insert(9);
}
124
Implementing Tree
Traversals

125
Tree Traversal
Preorder
:
root – left – right

Postorder
:
left – right – root

Inorder:
left – root – right

126
Implementing Tree Traversals
void Preorder(Node* ptr)
{
if(ptr!=NULL)
{
cout << ptr->data << “ “;
Preorder(ptr->left);
Preorder(ptr->right);

127
Implementing Tree Traversals
void Postorder(Node* ptr)
{
if(ptr!=NULL)
{
Postorder(ptr->left);
Postorder(ptr->right);
cout << ptr->data << “ “;
}

128
Implementing Tree Traversals
void Inorder(Node* ptr)
{
if(ptr!=NULL)
{
Inorder(ptr->left);
cout << ptr->data << “ “;
Inorder(ptr->right);

129
Traversal exercises: finding output 1
4 1
4 5
1
3 1
9 8
4 2
1
7 9 0
6
5 1
7
4 5
14 4 3 3 9 7 5 4 4 5 5 5 7 9 9 9 4 15 14 14 18 16 17 17 16 20 20 18 15 14

void Traversal(Node* temp)


{
if(temp!=NULL) Practice Tracing The Tree Using
{ The Given Traversal Function
cout<<temp->data;
Traversal(temp->left);
Traversal(temp->right);
cout<<temp->data;
}
} 130
Utility functions
Counting the number of leaf nodes:
Int countleafnodes(Node *ptr)
{
if(ptr==NULL)
return 0;
if(ptr->left==NULL&&ptr->right==NULL)
return 1;
else
return countleafnodes(ptr->left)+coutnleafnodes(ptr->right);
}

131

You might also like