foundation of computing systems lecture 7 trees: part iv

22
Foundation of Computing Systems Lecture 7 Trees: Part IV

Upload: lynn-summers

Post on 19-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

  • Foundation of Computing SystemsLecture 7

    Trees: Part IV

    IT 60101: Lecture #7

  • Weighted Binary TreesPath lengthPath 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 nodeThe node with zero children is called an external node (or in other words, an external node is nothing but a leaf node).

    Internal nodeA node with one or more children is called an internal node.

    IT 60101: Lecture #7

  • Extended Binary Trees

    External nodeThe node with zero children is called an external node (or in other words, an external node is nothing but a leaf node).

    Internal nodeA node with one or more children is called an internal node.

    IT 60101: Lecture #7

  • Extended Binary Trees

    Path lengthPath 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 = 4Path length of L8 = 5

    IT 60101: Lecture #7

  • Extended Binary TreesExternal path lengthExternal 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 L10E = 2 + 3 + 3 + 4 + 4 + 3 + 3 + 5 + 5 + 4 = 36

    IT 60101: Lecture #7

  • Extended Binary TreesInternal path lengthInternal 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 = 0 + 1 + 2 + 1 + 2 + 3 + 2 + 3 + 4 = 18

    IT 60101: Lecture #7

  • Extended Binary TreesProperties

    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 TreesA binary tree is called weighted binary tree if a weight is assigned to each external node.

    IT 60101: Lecture #7

  • Weighted Binary TreesWeighted path lengthThe 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 nodesP = W1L1+ W2L2 + W3L3 + W4L4 + W5L5 + W6L6 + W7L7 = 22 + 52 + 34 + 44 + 13 + 73 + 63 = 84

    IT 60101: Lecture #7

  • Minimum Weighted Binary TreesThe 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 TreeA weighted binary tree according to Huffmans algorithmMinimum weighted binary tree

    Huffmans algorithmSuppose, 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

  • Huffmans Algorithm: ExampleInputExternal node: ABCDEFWeight: 253417

    IT 60101: Lecture #7

  • Huffmans Algorithm: ExampleP = 22 + 9 + 13 + 6 + 3 = 53

    IT 60101: Lecture #7

  • Huffmans Algorithm: ExampleP = 22 + 9 + 13 + 6 + 3 = 53 P = 22 + 8 + 14 + 3 + 7 = 54 A weighted binary tree but not Huffman tree Huffman tree according to Huffmans algorithm

    IT 60101: Lecture #7

  • Huffmans Algorithm: ExampleHuffman tree is not necessarily unique for a given set weights

    IT 60101: Lecture #7

  • Application of Huffman Tree: Huffman CodingTo obtain an optimal set of codes for symbols S1, S2, ..., Sn which constitute messages. Each code is a binary string (combinations of 0s and 1s) which will be used for transmission of messages

    Fixed length coding

    Variable length coding

    IT 60101: Lecture #7

  • Application of Huffman Tree: Huffman CodingFixed length codingS10000S20001S30010S40011S50100S6 0101 S70110 S80111 S91000 S101001S111010

    IT 60101: Lecture #7

  • Application of Huffman Tree: Huffman CodingVariable length codingS11/4 00S23/16 10S31/8 111S41/8 010S51/16 1101S61/16 01110S71/16 01111S81/16 0110S91/32 11001S101/64 110000 S111/64 110001

    IT 60101: Lecture #7

  • Application of Huffman Tree: Huffman CodingS116 S212 S38 S48 S54 S64 S74 S84 S92 S101 S111

    IT 60101: Lecture #7

  • Application of Huffman Tree: Huffman CodingS11600S21210S38111S48010S541101S6401110S7401111S840110S9211001S101110000 S111110001

    IT 60101: Lecture #7

  • Huffman CodingAdvantagesThe 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

    School of Information TechnologyIT 60101: School of Information Technology, IIT KharagpurSchool of Information TechnologyIT 60101: School of Information Technology, IIT KharagpurSchool of Information TechnologyIT 60101: School of Information Technology, IIT KharagpurSchool of Information TechnologyIT 60101: School of Information Technology, IIT KharagpurSchool of Information TechnologyIT 60101: School of Information Technology, IIT KharagpurSchool of Information TechnologyIT 60101: School of Information Technology, IIT Kharagpur