SlideShare ist ein Scribd-Unternehmen logo
1 von 131
Binary Trees & Applications
1
DATA STRUCTURES AND ALGORITHMS
Real world
2
DATA STRUCTURE
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 chart
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
root
8 9
5 6 7
3
4
2
1
children of this parent
siblings of each other
leaves
8
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.
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
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
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
Levels and depth
13
Root
Node
Leaf
Arc
Level 0
Level 1
Level 2
Level 3
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
Binary Tree Examples
15
Binary Trees
76
26
50
21
85
99
80
99
26
76
21
85
80
50
26
76
85
99
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
Binary Trees
17
A node having no sons is called a leaf of the binary tree.
D E F G
C
B
A
leaves
Binary Tree
18
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 ???
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
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
Examples – familiarization with
notation
22
A
B C
D E F
G H I
Left Subtree of A
Right Subtree of A
Root
A
B C
D E F
G H I
An Edge
EXAMPLES – FAMILIARIZATION WITH NOTATION
23
A
B C
D E F
G H I
Child nodes of C?
Child nodes of A?
EXAMPLES – FAMILIARIZATION WITH NOTATION
24
A
B C
D E F
G H I
Descendants of Node C?
Descendants of A?
EXAMPLES – FAMILIARIZATION WITH NOTATION
25
A
B C
D E F
G H I
Parent of Node E?
Parent of Node H?
EXAMPLES – FAMILIARIZATION WITH NOTATION
26
A
B C
D E F
G H I
Ancestors of Node D?
Ancestors of Node H?
EXAMPLES – FAMILIARIZATION WITH NOTATION
27
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
A
B C
D E F
G H I
Leaf node:
Any node that has empty children
EXAMPLES – FAMILIARIZATION WITH NOTATION
29
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
31
Examples – familiarization with notation
Example Binary Trees
32
One node Two nodes
Three nodes
Types of Binary Trees
Strictly Binary Trees
Complete Binary Trees
33
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??
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
Binary Trees (recall)
36
Depth:
The depth of a binary tree is the maximum level of any
leaf in the tree.
D E F G
C
B
A
I J
Depth: 3
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
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
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.
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
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
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
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
44
Moving through a tree, visiting each node exactly once
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
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
47
Preodrder:
Visit the root
Traverse the left subtree in preorder
Traverse the right subtree in preorder
Also known as depth-first traversal.
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
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
Preorder Traversal
50
13
Visit root
Traverse left subtree
Traverse right subtree
Output: 32 79 13
Preorder Traversal
51
32
16
13
42
79
95
Output: 32 79 13
Preorder Traversal
52
16
42
95
Output: 32 79 13 42
95
Output: 32 79 13 42 95
16
Output: 32 79 13 42 95 16
Preorder Traversal
53
32
16
13
42
79
95
Output: 32 79 13 42 95 16
Preorder Traversal
54
32
16
13
42
79
95
32 79 13 42 16
95
Tree Traversal
55
Inorder:
Traverse the left subtree in inorder
Visit the root
Traverse the right subtree in inorder
Also known as Symmetric order.
Inorder Traversal
56
32
16
13
42
79
95
Traverse the left subtree
Visit the root
Traverse the right subtree
79 13 32 95 42 16
Tree Traversal
57
Postorder:
Traverse the left subtree in postorder
Traverse the right subtree in postorder
Visit the root
Postorder Traversal
58
32
16
13
42
79
95
Traverse the left subtree
Traverse the right subtree
Visit the root
13 79 95 16 42 32
Tree Traversal
59
Preorder:
root – left – right
Postorder:
left – right – root
Inorder:
left – root – right
Tree Traversal
60
A
G
F
E
C
D
B
H
Preorder: A B D G C E F H
Inorder: D G B A E C H F
Postorder: G D B E H F C A
Tree Traversal
15
16
5
12
3 20
23
18
13
10
6
7
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
‘-’
‘8’ ‘5’
treePtr
INORDER TRAVERSAL: 8 - 5
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
A binary expression tree
65
A Binary Expression Tree
66
‘*’
‘+’
‘4’
‘3’
‘2’
Evaluate the expression?
( 4 + 2 ) * 3 = 18
A Binary Expression Tree
67
‘*’
‘+’
‘4’
‘3’
‘2’
What infix, prefix, postfix expressions does it represent?
A Binary Expression Tree
68
‘*’
‘+’
‘4’
‘3’
‘2’
Infix: ( ( 4 + 2 ) * 3 )
Prefix: * + 4 2 3
Postfix: 4 2 + 3 *
Representing Expressions
69
A * B + C
+
C
*
A B
A * (B + C) ((A + B) * C) / (D - E)
*
A
+
B C
C
A B
D E
*
+
/
-
D
x y
x D y
A Binary Expression Tree
Draw an expression tree for the following:
(a+b*c)+((d*e+f)*g)
70
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
Binary expression tree
72
‘*’
‘-’
‘8’ ‘5’
What infix, prefix, postfix expressions does it represent?
‘/’
‘+’
‘4’
‘4’
‘2’
Binary Expression Tree
73
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 4 ) )
Prefix: * - 8 5 / + 4 2 4
Postfix: 8 5 - 4 2 + 4 / *
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘4’
‘2’
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))
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
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.
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
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
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
+
*
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
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
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
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
Huffman Coding
0.1 0.1 0.15 0.2 0.45
C
B E
A
D
0.2
0 1
85
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
86
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
87
88
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
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
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
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
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
93
C U
31 27
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
94
C U
58
31 27
125
Freq
93
80
76
73
71
61
55
41
40
E
Char
T
A
O
I
N
R
H
L
D
58
65
S
31
27
C
U
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
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
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
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
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
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
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
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
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
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
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
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
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



L
k
k
avg k
P
N
N
1
)
(
Navg : Average number of bits to
encode the given data
Nk : Number of bits in symbol k
P(k): Probability of occurrence of
symbol k
107
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;
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
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
E
1
i
1
2
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
110
Building the Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
111
Building the Tree
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
112
Building the Tree
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
113
Building the Tree
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
Repeating these steps………..
114
Building the Tree
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
115
Building the Tree
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
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
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
Binary Tree Implementation
Using Array
Using Linked Nodes
119
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
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
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
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
124
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()
{
BinaryTree b;
b->root = b.insert(4);
b->root->left = b.insert(6);
b->root->right = b.insert(7);
b->root->right->left = b.insert(9);
}
9
7
6
4
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
130
Traversal exercises: finding output
void Traversal(Node* temp)
{
if(temp!=NULL)
{
cout<<temp->data;
Traversal(temp->left);
Traversal(temp->right);
cout<<temp->data;
}
}
14
15
4
9
3 18
20
16
17
14
9
7
5
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
Practice Tracing The Tree Using
The Given Traversal Function
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

Weitere ähnliche Inhalte

Ähnlich wie Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx

Ähnlich wie Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx (20)

Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
7.tree
7.tree7.tree
7.tree
 
Tree
TreeTree
Tree
 
Lec6
Lec6Lec6
Lec6
 
Lecture 5 tree.pptx
Lecture 5 tree.pptxLecture 5 tree.pptx
Lecture 5 tree.pptx
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
 
heap sort
 heap sort heap sort
heap sort
 
Data structures
Data structuresData structures
Data structures
 
Tree
TreeTree
Tree
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Tree terminology and introduction to binary tree
Tree terminology and introduction to binary treeTree terminology and introduction to binary tree
Tree terminology and introduction to binary tree
 
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDYDATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
 
Binary tree
Binary treeBinary tree
Binary tree
 
AD3251-Data Structures Design-Notes-Tree.pdf
AD3251-Data Structures  Design-Notes-Tree.pdfAD3251-Data Structures  Design-Notes-Tree.pdf
AD3251-Data Structures Design-Notes-Tree.pdf
 
07 trees
07 trees07 trees
07 trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Unit 3 Tree chapter 5
Unit 3  Tree chapter 5Unit 3  Tree chapter 5
Unit 3 Tree chapter 5
 

Kürzlich hochgeladen

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx

  • 1. Binary Trees & Applications 1 DATA STRUCTURES AND ALGORITHMS
  • 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
  • 13. Levels and depth 13 Root Node Leaf Arc Level 0 Level 1 Level 2 Level 3
  • 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
  • 15. Binary Tree Examples 15 Binary Trees 76 26 50 21 85 99 80 99 26 76 21 85 80 50 26 76 85 99
  • 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
  • 22. Examples – familiarization with notation 22 A B C D E F G H I Left Subtree of A Right Subtree of A Root
  • 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
  • 32. Example Binary Trees 32 One node Two nodes Three nodes
  • 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
  • 36. Binary Trees (recall) 36 Depth: The depth of a binary tree is the maximum level of any leaf in the tree. D E F G C B A I J Depth: 3
  • 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
  • 46. Traversal of a Binary Tree 46
  • 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
  • 50. Preorder Traversal 50 13 Visit root Traverse left subtree Traverse right subtree Output: 32 79 13
  • 52. Preorder Traversal 52 16 42 95 Output: 32 79 13 42 95 Output: 32 79 13 42 95 16 Output: 32 79 13 42 95 16
  • 55. Tree Traversal 55 Inorder: Traverse the left subtree in inorder Visit the root Traverse the right subtree in inorder Also known as Symmetric order.
  • 56. Inorder Traversal 56 32 16 13 42 79 95 Traverse the left subtree Visit the root Traverse the right subtree 79 13 32 95 42 16
  • 57. Tree Traversal 57 Postorder: Traverse the left subtree in postorder Traverse the right subtree in postorder Visit the root
  • 58. Postorder Traversal 58 32 16 13 42 79 95 Traverse the left subtree Traverse the right subtree Visit the root 13 79 95 16 42 32
  • 59. Tree Traversal 59 Preorder: root – left – right Postorder: left – right – root Inorder: left – root – right
  • 60. Tree Traversal 60 A G F E C D B H Preorder: A B D G C E F H Inorder: D G B A E C H F Postorder: G D B E H F C A
  • 61. Tree Traversal 15 16 5 12 3 20 23 18 13 10 6 7 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
  • 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
  • 64. Binary Expression Trees Infix Tree Postfix Tree 64
  • 65. ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL: 8 - 5 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5 - A binary expression tree 65
  • 66. A Binary Expression Tree 66 ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Evaluate the expression? ( 4 + 2 ) * 3 = 18
  • 67. A Binary Expression Tree 67 ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ What infix, prefix, postfix expressions does it represent?
  • 68. A Binary Expression Tree 68 ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( 4 + 2 ) * 3 ) Prefix: * + 4 2 3 Postfix: 4 2 + 3 *
  • 69. Representing Expressions 69 A * B + C + C * A B A * (B + C) ((A + B) * C) / (D - E) * A + B C C A B D E * + / - D x y x D y
  • 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
  • 72. Binary expression tree 72 ‘*’ ‘-’ ‘8’ ‘5’ What infix, prefix, postfix expressions does it represent? ‘/’ ‘+’ ‘4’ ‘4’ ‘2’
  • 73. Binary Expression Tree 73 Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 4 ) ) Prefix: * - 8 5 / + 4 2 4 Postfix: 8 5 - 4 2 + 4 / * ‘*’ ‘-’ ‘8’ ‘5’ ‘/’ ‘+’ ‘4’ ‘4’ ‘2’
  • 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
  • 85. Huffman Coding 0.1 0.1 0.15 0.2 0.45 C B E A D 0.2 0 1 85
  • 86. 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 86
  • 87. 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 87
  • 88. 88 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
  • 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
  • 119. Binary Tree Implementation Using Array Using Linked Nodes 119
  • 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
  • 124. 124 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() { BinaryTree b; b->root = b.insert(4); b->root->left = b.insert(6); b->root->right = b.insert(7); b->root->right->left = b.insert(9); } 9 7 6 4
  • 126. Tree Traversal Preorder: root – left – right Postorder: left – right – root Inorder: left – root – right 126
  • 127. Implementing Tree Traversals void Preorder(Node* ptr) { if(ptr!=NULL) { cout << ptr->data << “ “; Preorder(ptr->left); Preorder(ptr->right); } } 127
  • 128. Implementing Tree Traversals void Postorder(Node* ptr) { if(ptr!=NULL) { Postorder(ptr->left); Postorder(ptr->right); cout << ptr->data << “ “; } } 128
  • 129. Implementing Tree Traversals void Inorder(Node* ptr) { if(ptr!=NULL) { Inorder(ptr->left); cout << ptr->data << “ “; Inorder(ptr->right); } } 129
  • 130. 130 Traversal exercises: finding output void Traversal(Node* temp) { if(temp!=NULL) { cout<<temp->data; Traversal(temp->left); Traversal(temp->right); cout<<temp->data; } } 14 15 4 9 3 18 20 16 17 14 9 7 5 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 Practice Tracing The Tree Using The Given Traversal Function
  • 131. 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