CSL 210: Data Structures with
Applications: Module3
IIIT Nagpur
Aniket Pingley
Binary Heap
• A binary heap is a complete binary tree that satis es the heap property - min
and max
• A min-heap is a binary tree such that - the data contained in each node is less
than (or equal to) the data in that node’s children
• A max-heap is a binary tree such that - the data contained in each node is
greater than (or equal to) the data in that node’s children.
fi
Binary Heap
Complete Binary Tree that satis es the heap property
fi
Array Representation for Binary Heap
• Use an array to hold the data
• Store the root at index 1
• For any node at index i
• its left child (if any) is in position 2i
• its right child (if any) is in position
2i + 1
• its parent (if any) is in position i/2
(integer division)
Huffman Coding
• Encoding: process to convert given sequence of input (digits/characters/
numbers/symbols etc.) into a speci ed format for communication/
transmission/storage etc.
• Hu man Coding, developed by David Hu man in 1951, is a technique to
encode data into binary sequence that achieves lossless compression.
• It represents data using variable-length code where length of the code is
inversely (not strictly) proportional to the frequency of data.
• Applications: variety of zipping techniques such as gzip, winzip etc.
ff
fi
ff
Example of variable-length en/coding
A data le of 100,000 characters contains only the characters a–f, with the frequencies
indicated. If we assign each character a 3-bit codeword, we can encode the le in
300,000 bits. Using the variable-length code shown, we can encode the le in only
224,000 bits (1000 x (45x1 + 13x3 + 12x3 + 16x3 + 9x4 + 5x4)).
fi
fi
fi
Binary Tree Representation
Disregard the values at non-leaf nodes for this slide
Prefix codes
• As per variable-length coding is previous slide, if we encode ‘abc’ as 0101100
(0.101.100), then how will we decode without misinterpretation/ambiguity?
• Pre x codes = codes in which no codeword is also a pre x of some other
codeword
• Encoding and Decoding a pre x code is done using Binary Tree, where leaf
nodes are the input symbol characters/symbols.
fi
fi
fi
Build
Huffman
Code
Constructing Huffman Code
Step-by-step
extraction and
insertion process of
Huffman Coding