220 likes | 333 Vues
Foundation of Computing Systems. Lecture 7 Trees: Part IV. 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
E N D
Foundation of Computing Systems Lecture 7 Trees: Part IV
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
Extended Binary Trees Internal node • A node with one or more children is called an internal 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). IT 60101: Lecture #7
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
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 Pidenotes the path length of the i-th external node L1L2L3L4L5L6L7L8L9L10 E = 2 + 3 + 3 + 4 + 4 + 3 + 3 + 5 + 5 + 4 = 36 IT 60101: Lecture #7
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 Pidenotes the path length of the i-th internal node N1N2N3N4N5N6N7N8N9 I = 0 + 1 + 2 + 1 + 2 + 3 + 2 + 3 + 4 = 18 IT 60101: Lecture #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
Extended Binary Trees IT 60101: Lecture #7
Weighted Binary Trees • A binary tree is called weighted binary tree if a weight is assigned to each external node. IT 60101: Lecture #7
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
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) = 38 P(T2) = 44 P(T3) = 34 IT 60101: Lecture #7
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
Huffman’s Algorithm: Example • Input External node: A B C D E F Weight: 2 5 3 4 1 7 IT 60101: Lecture #7
Huffman’s Algorithm: Example P = 22 + 9 + 13 + 6 + 3 = 53 IT 60101: Lecture #7
Huffman’s Algorithm: Example A weighted binary tree but not Huffman tree Huffman tree according to Huffman’s algorithm P = 22 + 9 + 13 + 6 + 3 = 53 P = 22 + 8 + 14 + 3 + 7 = 54 IT 60101: Lecture #7
Huffman’s Algorithm: Example • Huffman tree is not necessarily unique for a given set weights IT 60101: Lecture #7
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
Application of Huffman Tree: Huffman Coding • Fixed length coding S1 0000 S2 0001 S3 0010 S4 0011 S5 0100 S6 0101 S7 0110 S8 0111 S9 1000 S10 1001 S11 1010 IT 60101: Lecture #7
Application of Huffman Tree: Huffman Coding • Variable length coding S1 1/4 00 S2 3/16 10 S3 1/8 111 S4 1/8 010 S5 1/16 1101 S6 1/16 01110 S7 1/16 01111 S8 1/16 0110 S9 1/32 11001 S10 1/64 110000 S11 1/64 110001 IT 60101: Lecture #7
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
Application of Huffman Tree: Huffman Coding S1 16 00 S2 12 10 S3 8 111 S4 8 010 S5 4 1101 S6 4 01110 S7 4 01111 S8 4 0110 S9 2 11001 S10 1 110000 S11 1 110001 IT 60101: Lecture #7
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