3. 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
7. 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
8. root
8 9
5 6 7
3
4
2
1
children of this parent
siblings of each other
leaves
8
9. Trees - Definition and Terminology
9
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.
10. Tree Terminologies
10
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.
B
A
C
11. Tree Terminologies
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
B
A
C
12. Tree Terminologies
12
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
Leaf nodes
Interior nodes Height
14. A binary tree is a tree in which each node has at most 2 children.
•
C E P U
T
M
•
•
O
root
left subtree
of node O
right subtree
of node O
left subtree
of node M
right subtree
of node M
Binary Trees
14
16. Binary Trees
16
•
D E F G
C
B
•
•
A
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 right son of A
left son of B right son of B
17. Binary Trees
17
A node having no sons is called a leaf of the binary tree.
D E F G
C
B
A
leaves
19. Binary Trees
19
Node n1 is an ancestor (Forefathers) 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.
D E F G
C
B
A
I J
H
A is ancestor of D
H is descendent of B
G is ancestor of J
F is ___ of H ???
20. 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
21. Binary Trees
21
D E F G
C
B
A
I J
H
G is descendent of A.
right
E is descendent of A.
H is descendent of B.
I is descendent of C.
left
right
right
H is descendent of E.
left
23. A
B C
D E F
G H I
An Edge
EXAMPLES – FAMILIARIZATION WITH NOTATION
23
24. A
B C
D E F
G H I
Child nodes of C?
Child nodes of A?
EXAMPLES – FAMILIARIZATION WITH NOTATION
24
25. A
B C
D E F
G H I
Descendants of Node C?
Descendants of A?
EXAMPLES – FAMILIARIZATION WITH NOTATION
25
26. A
B C
D E F
G H I
Parent of Node E?
Parent of Node H?
EXAMPLES – FAMILIARIZATION WITH NOTATION
26
27. A
B C
D E F
G H I
Ancestors of Node D?
Ancestors of Node H?
EXAMPLES – FAMILIARIZATION WITH NOTATION
27
28. A
B C
D E F
G H I
Path:
If n1, n2,…nk is a sequence of
nodes such that ni is the parent (
child) of ni+1, then that sequence is
a path.
J
A path from J to A
EXAMPLES – FAMILIARIZATION WITH NOTATION
28
29. A
B C
D E F
G H I
Leaf node:
Any node that has empty children
EXAMPLES – FAMILIARIZATION WITH NOTATION
29
30. A
B C
D E F
G H I
Internal node:
Any node that has at least one
non-emptyChild
EXAMPLES – FAMILIARIZATION WITH NOTATION
30
33. Types of Binary Trees
Strictly Binary Trees
Complete Binary Trees
33
34. Strictly Binary Tree
34
If every non-leaf node in a binary tree has nonempty left and
right subtrees, the tree is known as Strictly Binary Tree.
D E F G
C
B
A
I J
H
D E F G
C
B
A
I J
Strictly Binary Tree
Strictly Binary Tree??
35. Binary Trees (recall)
35
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.
D E F G
C
B
A
I J
Level 0
Level 2
Level 1
37. Complete Binary Tree
37
A complete binary tree of depth d is a strictly binary tree all
of whose leaves are at level d.
D E F G
C
B
A
A complete binary tree of depth 2
38. 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
39. Binary Trees
39
If a binary tree contains m nodes at level l, it can contain
at the most nodes at level l+1.
2m
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.
40. Binary Trees
40
D E F G
C
B
A
A Complete Binary Tree…
Dept d = 2
No of nodes at level i = 2i
No of leaves = 2d = 22 = 4
41. Binary Trees
41
D E F G
C
B
A
.
.
.
.
.
.
.
.
.
.
.
.
Total Number of nodes in a complete binary tree:
N = 20+21+22+ ……+2d = j=0 Σd
2j
= 2
d+1
- 1
42. Binary Trees
42
N = 2
d+1
- 1
Given the number of nodes of a complete binary tree,
its depth d would be:
Number of nodes in a complete binary tree:
d = log2(N + 1) -1
43. 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
44. Tree Traversal
44
Moving through a tree, visiting each node exactly once
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
45. 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
47. Tree Traversal
47
Preodrder:
Visit the root
Traverse the left subtree in preorder
Traverse the right subtree in preorder
Also known as depth-first traversal.
48. Preorder Traversal
48
32
16
13
42
79
95
Output: 32
left subtree right subtree
Problem now reduced to traversal of two smaller binary trees.
Visit the root
Traverse the left subtree in preorder
Traverse the right subtree in preorder
49. Preorder Traversal
49
13
79 Visit root
Traverse left subtree
Traverse right subtree
Output: 32 79
•Left subtree is empty
•Now, traverse the right subtree
63. 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
70. A Binary Expression Tree
Draw an expression tree for the following:
(a+b*c)+((d*e+f)*g)
70
71. C
A B
D E
*
+
/
-
Traverse the tree in Left-Right-Root order
(postorder) to get RPN:
C
A B
D E
*
+
/
-
Traverse tree in Root-Left-Right
order (preorder) to get prefix:
Traverse tree in Left-Root-Right
order (inorder) to get infix
— must insert ()'s
A B + C * D E - /
- D E
(A + B)
( * C)
/
( )
(D - E)
C
A B
D E
*
+
/
-
/ * + A B C
71
74. 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.
76. 76
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.
77. 77
Expression: a b + c d e + * *
‘a’ is an operand, a one-node tree is created and
pushed on the stack
a b
a b
+
‘b’ is again an operand, and is pushed on the
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.
78. Conversion of Postfix Expression to tree
78
Expression: a b + c d e + * *
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
stack.
Now a ‘+’ is read, so two trees are merged.
a b
+ c
d e
+
79. 79
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.
a b
+
c
d e
+
*
a b
+ c
d e
+
80. 80
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
+
*
81. 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
82. Huffman Coding
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
Given a text with 5 unique symbols
Occurrence probabilities – Computed from text
82
83. Huffman Coding
1-Construct a list of one-node binary trees, one for each
character, the weight of each node being its probability of
occurrence
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
84. Huffman Coding
0.1 0.1 0.15 0.2 0.45
C
B E
A
D
Character A B C D E
Probability 0.2 0.1 0.1 0.15 0.45
84
89. Huffman Coding
0.1 0.1 0.15 0.2 0.45
C
B E
A
D
0.2
0 1
0.35
0 1
0.55
0 1
1.0
1
0 Character Huffman
Code
A 011
B 000
C 001
D 010
E 1
89
90. 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
91. Huffman Decoding
0 1 0 1 0 1 1 0 1 0
0.1 0.1 0.15 0.2 0.45
C
B E
A
D
0.2
0 1
0.35
0 1
0.55
0 1
1.0
1
0
0 1 0 1 0 1 1 0 1 0
D
0 1 0 1 0 1 1 0 1 0
D E
0 1 0 1 0 1 1 0 1 0
D E A
0 1 0 1 0 1 1 0 1 0
D
A
E
D
91
92. 92
Huffman Coding
Compressing a given text
Character counts in the text
125
Freq
93
80
76
73
71
61
55
41
40
E
Char
T
A
O
I
N
R
H
L
D
31
27
C
U
65
S
95. 95
C U
58
D L
81
31 27
40 41
125
Freq
93
80
76
73
71
61
58
55
E
Char
T
A
O
I
N
R
H
81
65
S
41
40
L
D
96. 96
H
C U
58
113
D L
81
31 27
55
40 41
125
Freq
93
80
76
73
71
61
113
E
Char
T
A
O
I
N
R
81
65
S
58
55
H
97. 97
R S H
C U
58
113
126
D L
81
31 27
55
61 65
40 41
125
Freq
93
80
76
73
71
113
E
Char
T
A
O
I
N
81
126
61
R
65
S
98. 126
98
R S N I H
C U
58
113
144
126
D L
81
31 27
55
71 73
61 65
40 41
125
Freq
93
80
76
144
113
E
Char
T
A
O
81
73
71
I
N
99. 144
99
R S N I H
C U
58
113
144
126
D L
81
156
A O
31 27
55
71 73
61 65
40 41
80 76
126
125
Freq
93
156
113
E
Char
T
81
80
76
A
O
100. 100
R S N I H
C U
58
113
144
126
D L
81
156 174
A O T
31 27
55
71 73
61 65
40 41
93
80 76
144
126
125
Freq
156
113
E
Char
174
93
T
81
101. 174
101
R S N I
E
H
C U
58
113
144
126
238
T
D L
81
156 174
A O
80 76
71 73
61 65
40 41
31 27
55
125
93
144
126
238
Freq
156
Char
125
113
E
102. 102
R S N I
E
H
C U
58
113
144
126
238
270
T
D L
81
156 174
A O
31 27
55
71 73
61 65
125
40 41
93
80 76
238
156
270
Freq
174
Char
144
126
103. 103
R S N I
E
H
C U
58
113
144
126
238
270
330
T
D L
81
156 174
A O
31 27
55
71 73
61 65
125
40 41
93
80 76
270
330
Freq
238
Char
156
174
104. 104
R S N I
E
H
C U
58
113
144
126
238
270
330 508
T
D L
81
156 174
A O
31 27
55
71 73
61 65
125
40 41
93
80 76
330
508
Freq
Char
270
238
105. 105
R S N I
E
H
C U
58
113
144
126
238
270
330 508
838
T
D L
81
156 174
A O
31 27
55
71 73
61 65
125
40 41
93
80 76
838
Freq
Char
330
508
106. 106
R S N I
E
H
C U
0
0
T
D L
1
0 0
A O
0
1
1
1
1
0
0
1
1
1
1
1
1
0
0
0
0
0
1
125
Freq
93
80
76
73
71
61
55
41
40
E
Char
T
A
O
I
N
R
H
L
D
31
27
C
U
65
S
0000
Fixed
0001
0010
0011
0100
0101
0111
1000
1001
1010
1011
1100
0110
110
Huff
011
000
001
1011
1010
1000
1111
0101
0100
11100
11101
1001
838
Total 4.00 3.62
108. 108
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;
109. E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
Building the Tree
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
117. 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
118. 118
Encoding using Huffman tree
Char Code
E 0000
i 0001
y 0010
l 0011
k 0100
. 0101
Space 011
e 10
r 1100
s 1101
n 1110
a 1111
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
10
16
26
Can implement as a Term Project
120. A binary tree is a tree in which each node has at most 2
children
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 . ..
•
C E P U
T
M
•
•
O
2
1
3 4 5 6
0
Array-Based Implementation:
An array can be used to store some binary trees. Number
the nodes level by level, from left to right,
120
121. 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 ___
array positions for storage:
M
C
E
U
T
P
O
i
t[i]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
21
41
0
…
…
58
Max # nodes on level i:
2i
In array representation,
children of i are at:
2i + 1, 2i + 2
Parent of i is at:
(i - 1) / 2
E C M U T
O
P
121
122. Linked Nodes Implementation:
Use nodes of the form
And maintain a pointer to the root.
data
left
Left child Right child
right
80
60
75
65
58 92
root
75
60
58 65
80
92
122
123. 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