Chapter 7
Chapter 7
Chapter 7
Chapter 07
8/28/2024 1
Data Structures- Chapter 7
❑ Binary Tree
– A tree is a nonlinear data structure used to represent data containing a
hierarchical relationship between elements.
– A tree in which each element may has 0-child , 1-child or maximum of
2-children.
– A Binary Tree T is defined as finite set of elements, called nodes, such
that:
a) T is empty (called the null tree or empty tree.)
b) T contains a distinguished node R, called the root of T, and the
remaining nodes of T form an ordered pair of disjoint binary trees T1
and T2.
8/28/2024 2
Data Structures- Chapter 7
❑ Binary Tree
– If T does contain a root R, then the two trees T1 and T2 are called,
respectively, the left sub tree and right sub tree of R.
– If T1 is non empty, then its node is called the left successor of R;
similarly, if T2 is non empty, then its node is called the right successor
of R. The nodes with no successors are called the terminal nodes.
– The line drawn from a node N of T to a successor is called an edge, and
a sequence of consecutive edges is called a path. A terminal node is
called a leaves, and a path ending in a leaves is called a branch.
– The depth (or height) of a tree T is the maximum number of nodes in a
branch of T. This turns out to be 1 more than the largest level number
of T.
8/28/2024 3
Data Structures- Chapter 7
❑ Binary Tree
8/28/2024 4
Data Structures- Chapter 7
❑ Level of node & its generation
Each node in binary tree T is assigned a level number, as follows.
– The root R of the tree T is assigned the level number 0, and
– Every other node is assigned a level number which is 1 more than the
level number of its parent. Furthermore, those nodes with the same
level number are said to belong to the same generation.
8/28/2024 5
Data Structures- Chapter 7
❑ Complete Binary Tree
– Consider a binary tree T. Each node of T can have at most two children.
– A tree is said to be complete if all its levels, except possibly the last,
have the maximum number of possible nodes, and if all the nodes at the
last level appear as far left as possible.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26
8/28/2024 7
Data Structures- Chapter 7
❑ General Tree
– A tree where a node can have any number of children / descendants is
called General Tree.
– A general tree (sometimes called a tree) is defined to be a nonempty
finite set T of elements, called nodes, such that:
8/28/2024 8
Data Structures- Chapter 7
❑ General Tree
A
B C D
E F G H J K
L M N
8/28/2024 9
Data Structures- Chapter 7
❑ Binary tree vs. General Tree
– A binary tree T/ is not a special case of a general tree T. Binary trees
and general trees are different objects. Two basic differences follows:
1) A binary tree T/ may be empty, but a general tree T is nonempty.
2) Suppose a node has only one child. Then the child is distinguished as a left
child or right child in a binary tree T/, but no such distinction exists in a
general tree T.
A A
– As binary trees, T1 and T2 are different,
since inT1, B is the left child of A, B B
but in T2, B is the right child of A
– As general trees, there is no difference C D C D
between T1 and T2. T1 T2
8/28/2024 10
Data Structures- Chapter 7
❑ Forest
8/28/2024 11
Data Structures- Chapter 7
❑ Representing Binary Trees in Memory
Let T be a binary tree. The first and usual way of representing binary
tree T in memory is called the link representation. The second way,
which uses a single array, called the sequential representation of T.
8/28/2024 12
Data Structures- Chapter 7
❑ Linked Representation of Binary Trees
8/28/2024 13
Data Structures- Chapter 7
❑ Linked Representation of Binary Trees
8/28/2024 14
Data Structures- Chapter 7
❑ Sequential Representation of Binary Trees
Suppose T is binary tree that is complete or nearly complete. Then there
is an efficient way of maintaining T in memory called the sequential
representation of T. This representation uses only a single linear array
TREE as follows:
8/28/2024 15
Data Structures- Chapter 7
❑ Sequential Representation of
Binary Trees
– The sequential representation
of the binary tee T in Fig. (a)
appears in Fig. (b).
– T requires 14 locations in the
array TREE even though T has
only 9 nodes.
– Accordingly, this sequential
representations is usually
inefficient unless, the binary
tree T is complete or nearly
complete.
8/28/2024 16
Data Structures- Chapter 7
❑ Sequential Representation of Binary Trees
8/28/2024 17
Data Structures- Chapter 7
❑ Traversing Binary Trees
There are three standard ways of traversing a binary tree T with root R.
These three algorithms, called preorder, inorder and postorder, are as
follows:
8/28/2024 18
Data Structures- Chapter 7
❑ Traversing Binary Trees
D E F
8/28/2024 19
Data Structures- Chapter 7
❑ Traversing Binary Trees
D E F
8/28/2024 20
Data Structures- Chapter 7
❑ Example 7.7: Represent the following algebraic expression by means of a
binary tree:
[ a+ ( b - c ) ] * [ (d - e) / ( f + g – h ) ]
Find the preorder and postorder traversal of the tree.
Preorder traversal:
*+a-bc/-de-+fgh
Postorder traversal:
abc-+de-fg+h-/*
8/28/2024 21
Data Structures- Chapter 7
❑ Binary Search Trees (BST)
Binary Search tree is a binary tree T in which each node N has the
following property:
The value at N is greater than every value in the left sub tree of N and
is less than or equal to every value in the right sub tree of N.
BST: This structure enables one to search for and find an element with an
average running time f(n) = O(log2n). It also enables one to easily insert
and delete elements.
Sorted Linear Array: Here one can search for and find an element with a
running time f(n)=O(log2n), but it is expensive to insert and delete
elements.
Linked List: Here one can easily insert and delete elements, but it is
expensive to search for and find an element, since one must use a linear
search with running time f(n) = O(n).
8/28/2024 22
Data Structures- Chapter 7
❑ Example 7.15: Construct a binary search tree for the following numbers:
40, 60, 50, 33, 55, 11
8/28/2024 23
Data Structures- Chapter 7
❑ Heap
Suppose H is a complete binary tree with n elements. Then H is called a
heap, or a maxheap, if each node N of H has the following property:
“The value at N is greater than or equal to the value at each of the
children of N.”
Accordingly, the value at N is greater than or equal to the value at any of
the descendants of N.
88
72 55
70 44 30 50
50 66 22 33 25
8/28/2024 24
Data Structures- Chapter 7
❑ MinHeap
Suppose H is a complete binary tree with n elements. Then H is called a
minheap, if each node N of H has the following property:
“The value at N is less than or equal to the value at any of the children
of N.”
2
4 3
6 7 9 3
8 6
8/28/2024 25
Data Structures- Chapter 7
❑ Heap
Heaps are used
1. to implement priority queues,
2. to implement the Heap sort algorithm.
8/28/2024 26
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
Insert the numbers one after the other into an empty heap H.
44 44
ITEM = 44 30
ITEM = 30
8/28/2024 27
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
44 50
30 50 30 44
ITEM = 50
8/28/2024 28
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
50 50 50 60
30 44 30 44 60 44 50 44
22 22 60 22 30 22 30
ITEM = 22 ITEM = 60
8/28/2024 29
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
60 60 60
50 44 50 55 50 55
22 30 55 22 30 44 22 30 44
ITEM = 55
8/28/2024 30
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
60 60 77
50 55 50 77 50 60
22 30 44 77 22 30 44 55 22 30 44 55
ITEM = 77
8/28/2024 31
Data Structures- Chapter 7
❑ Example 7.34: Construct a heap tree for the following numbers:
44, 30, 50, 22, 60, 55, 77, 55.
Solution:
77 77 77
50 60 50 60 55 60
22 30 44 55 55 30 44 55 50 30 44 55
55 22 22
ITEM = 55 Maxheap
8/28/2024 32
Data Structures- Chapter 7
❑ Deleting the Root of a Heap
Suppose H is a heap with N elements, and suppose we want to delete the
root R of H. This is accomplished as follows:
8/28/2024 33
Data Structures- Chapter 7
❑ Deleting the Root of a Heap
Delete the root of the following heap:
77
55 60
50 30 44 55
22
22
Solution:
ITEM= 77 (Root of the Heap) 55 60
Replace the root by the last node 22
50 30 44 55
8/28/2024 34
Data Structures- Chapter 7
❑ Deleting the Root of a Heap
Solution:
Now find the appropriate place of L in H.
22 60
55 60 55 22
50 30 44 55 50 30 44 55
60
55 55
50 30 44 22
8/28/2024 35
Data Structures- Chapter 7
❑ Applications of Heaps
8/28/2024 36
Data Structures- Chapter 7
❑ HeapSort
Since the root of H always contains the largest node in H, Phase B deletes
the elements of A in decreasing order.
8/28/2024 37
Data Structures- Chapter 7
❑ Algorithm 7.11: HEAPSORT(A, N)
8/28/2024 38
Data Structures- Chapter 7
❑ Complexity of HeapSort
HeapSort:
Worst case complexity of the heapsort algorithm:
f(n) = O(nlog2n)
Bubble Sort:
Complexity of the Bubble sort algorithm:
f(n) = O(n2)
Quick Sort:
Complexity of the Quick sort algorithm:
Average case: f(n) = O(nlog2n)
Worst case : f(n) = O(n2)
8/28/2024 39
Data Structures- Chapter 7
❑ Path Lengths; Huffman’s Algorithm
– An extended binary tree or 2-tree is a binary tree T in which each node
has either 0 or 2 children.
– The nodes with 0 children are called external nodes.
– The nodes with 2 children are called internal nodes.
8/28/2024 40
Data Structures- Chapter 7
❑ Path Lengths; Huffman’s Algorithm
– For any 2-tree,
NE = No. of external nodes and
NI = No. of internal nodes
Then,
NE = NI + 1
8/28/2024 41
Data Structures- Chapter 7
❑ Path Lengths; Huffman’s Algorithm
– An algorithm can be represented by a 2-tree T where the internal nodes
represent tests and the external nodes represent actions.
– Accordingly, the running time of the algorithm may depend on the
lengths of the paths in the tree.
8/28/2024 42
Data Structures- Chapter 7
❑ Path Lengths; Huffman’s Algorithm
Clearly,
LE = LI + 2n
8/28/2024 43
Data Structures- Chapter 7
❑ Path Lengths; Huffman’s Algorithm
8/28/2024 44
Data Structures- Chapter 7
❑ Example 7.36:
– Three 2-trees, each having external nodes with weights 2, 3, 5, and 11.
T1 T2 T3
2 11
2 3 5 11 11 5
3 5 2 3
– Now question is which tree will give a minimal weighted path length, P?
8/28/2024 46
Data Structures- Chapter 7
❑ Huffman Tree
8/28/2024 47
Data Structures- Chapter 7
❑ Huffman’s Algorithm
Suppose w1 and w2 are two minimum weights among the n given weights
w1, w2,…., wn. Find a tree T/ which gives a solution for the n-1 weights
w1 + w2, w3, w4,….., wn
Then, in the tree T/, replace the external node w1 + w2
by the subtree
w1 w2
8/28/2024 48
Data Structures- Chapter 7
❑ Example 7.37: A, B, C, D, E, F, G and H are 8 data items and they are
assigned weights as follows:
Data item : A B C D E F G H
Weight : 22 5 11 19 2 11 25 5
Construct a Huffman tree using the above data and Huffman’s algorithm.
Solution: 22 5 11 19 2 11 25 5
(a) A B C D E F G H
22 11 7 19 11 25 5
(b) A C D F G H
2 5
E B
8/28/2024 49
Data Structures- Chapter 7
❑ Example 7.37:
Solution: 22 11 19 11 25
(c) A C D 12 F G
5 7
H
2 5
E B
22 19 25
(d) A D 12 22 G
5 7 11 11
H F C
2 5
E B
8/28/2024 50
Data Structures- Chapter 7
❑ Example 7.37:
Solution:
(e) 22 31 22 25
A G
12 19 11 11
D F C
5 7
H
2 5
E B
8/28/2024 51
Data Structures- Chapter 7
❑ Example 7.37:
Solution:
(f) 31 44 25
G
12 19 22 22
D A
5 7 11 11
H F C
2 5
E B
8/28/2024 52
Data Structures- Chapter 7
❑ Example 7.37:
Solution:
(g) 56 44
25 22 22
31
G A
19 11 11
12
D F C
5 7
H
2 5
E B
8/28/2024 53
Data Structures- Chapter 7
❑ Example 7.37:
Solution: 100
(h)
44 56
22 22 25 31
A G
11 11 19
12
F C D
5 7
Building a Huffman tree H
Weighted Path Length:
2 5
P = 22.2+11.3+11.3+25.2+5.4+2.5+5.5+19.3 E B
= 272
8/28/2024 54
Data Structures- Chapter 7
❑ Computer Implementation of Huffman’s Algorithm
8/28/2024 55
Data Structures- Chapter 7
❑ Application to Coding
8/28/2024 56
Data Structures- Chapter 7
❑ Huffman Codes
8/28/2024 57
Data Structures- Chapter 7
❑ Huffman Coding
8/28/2024 58
Data Structures- Chapter 7
❑ Huffman Coding
Initial sequence
Sorted by frequency
Move it to correct
place
8/28/2024 59
Data Structures- Chapter 7
❑ Huffman Coding
Move sub-tree to
correct place
8/28/2024 60
Data Structures- Chapter 7
❑ Huffman Coding
8/28/2024 61
Data Structures- Chapter 7
❑ Huffman Coding
8/28/2024 62
Data Structures- Chapter 7
❑ Huffman Coding
Combine
last two trees
8/28/2024 63
Data Structures- Chapter 7
❑ Huffman Coding
Encoding: Concatenate the codewords representing each characters of the
file.
String Encoding:
TEA 10 00 010
SEA 011 00 010
TEN 10 00 110
8/28/2024 64