Download presentation
1
Foundation of Computing Systems
School of Information Technology Foundation of Computing Systems Lecture 7 Trees: Part IV IT 60101: School of Information Technology, IIT Kharagpur
2
School of Information Technology
Weighted Binary Trees Path length Path length of a node in a tree is defined as the number of edges that has to be traversed from the node to the root node. External node The node with zero children is called an external node (or in other words, an external node is nothing but a leaf node). Internal node A node with one or more children is called an internal node. IT 60101: Lecture #7 IT 60101: School of Information Technology, IIT Kharagpur
3
School of Information Technology
Extended Binary Trees External node The node with zero children is called an external node (or in other words, an external node is nothing but a leaf node). Internal node A node with one or more children is called an internal node. IT 60101: Lecture #7 IT 60101: School of Information Technology, IIT Kharagpur
4
School of Information Technology
Extended Binary Trees Path length Path length of a node in a tree is defined as the number of edges that has to be traversed from the node to the root node Path length of N1 = 0 Path length of N9 = 4 Path length of L5 = 4 Path length of L8 = 5 IT 60101: Lecture #7 IT 60101: School of Information Technology, IIT Kharagpur
5
School of Information Technology
Extended Binary Trees External path length External path length (let it be denoted as E) of a binary tree is defined as the sum of all path lengths, summed over each path from the root node of the tree to an external node Pi denotes the path length of the i-th external node L1 L2 L3 L4 L5 L6 L7 L8 L9 L10 E = = 36 IT 60101: Lecture #7 IT 60101: School of Information Technology, IIT Kharagpur
6
School of Information Technology
Extended Binary Trees Internal path length Internal path length (let it be denoted as I) of a binary tree can be defined analogously as the sum of path lengths of all internal nodes in the tree Pi denotes the path length of the i-th internal node N1 N2 N3 N4 N5 N6 N7 N8 N9 I = = 18 IT 60101: Lecture #7 IT 60101: School of Information Technology, IIT Kharagpur
7
Extended Binary Trees Properties
In a binary tree with n internal nodes, if I denotes the internal path length, then external path length, E = I + 2n. IT 60101: Lecture #7
8
Extended Binary Trees IT 60101: Lecture #7
9
Weighted Binary Trees A binary tree is called weighted binary tree if a weight is assigned to each external node. IT 60101: Lecture #7
10
Weighted Binary Trees Weighted path length
The external path length with the weights are called external weighted path length (or, simply weighted path length). where Wi is a weight of an external node ni and its path length is Li , n is the total number of external nodes P = W1L1+ W2L2 + W3L3 + W4L4 + W5L5 + W6L6 + W7L7 = 2×2 + 5×2 + 3×4 + 4×4 + 1×3 + 7×3 + 6×3 = 84 IT 60101: Lecture #7
11
Minimum Weighted Binary Trees
The objective is to construct a weighted binary tree for a given set of weights of the external nodes, so that the weighted path length is minimum P(T1) = P(T2) = P(T3) = 34 IT 60101: Lecture #7
12
Huffman Tree A weighted binary tree according to Huffman’s algorithm
Minimum weighted binary tree Huffman’s algorithm Suppose, we have to construct a minimum weighted binary tree (Huffman tree) with n weights W1, W2, . . ., Wn. Sort the weights in ascending order. Obtain a sub-tree with two minimum weights as the weights of external nodes. Include the weighted path length of the sub-tree so obtained into the list of weights. Repeat the procedure until the list contains single weight. IT 60101: Lecture #7
13
Huffman’s Algorithm: Example
Input External node: A B C D E F Weight: IT 60101: Lecture #7
14
Huffman’s Algorithm: Example
IT 60101: Lecture #7
15
Huffman’s Algorithm: Example
A weighted binary tree but not Huffman tree Huffman tree according to Huffman’s algorithm P = = 53 P = = 54 IT 60101: Lecture #7
16
Huffman’s Algorithm: Example
Huffman tree is not necessarily unique for a given set weights IT 60101: Lecture #7
17
Application of Huffman Tree: Huffman Coding
To obtain an optimal set of codes for symbols S1, S2, ..., Sn which constitute messages. Each code is a binary string (combinations of 0’s and 1’s) which will be used for transmission of messages Fixed length coding Variable length coding IT 60101: Lecture #7
18
Application of Huffman Tree: Huffman Coding
Fixed length coding S1 0000 S2 0001 S3 0010 S4 0011 S5 0100 S S7 0110 S8 0111 S9 1000 S S IT 60101: Lecture #7
19
Application of Huffman Tree: Huffman Coding
Variable length coding S1 1/4 00 S2 3/ S3 1/ S4 1/ S5 1/ S6 1/ S7 1/ S8 1/ S9 1/ S10 1/ S11 1/ IT 60101: Lecture #7
20
Application of Huffman Tree: Huffman Coding
S1 16 S2 12 S3 8 S4 8 S5 4 S6 4 S7 4 S8 4 S9 2 S10 1 S11 1 IT 60101: Lecture #7
21
Application of Huffman Tree: Huffman Coding
S S S S S S S S S S S IT 60101: Lecture #7
22
Huffman Coding Advantages
The Huffman coding has the minimum average length It does not require any end-of-character delimiter although the binary codes are of variable length codes. This is because of its unique prefix property IT 60101: Lecture #7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.