Adaptive Huffman Coding And Decoding
Last Updated :
12 Jul, 2025
Prerequisite: Huffman Coding, Huffman Decoding
Adaptive Huffman Coding is also known as Dynamic Huffman Coding. The implementation is done using Vitter Algorithm.
Encoding
Adaptive Huffman coding for a string containing alphabets:
Let m be the total number of alphabets. So m = 26.
For Vitter Algorithm, find a parameters e & r such that
m = 2e + r and 0 ≤ r ≤ 2e
Therefore, for m = 26 we get e = 4 & r = 10
There are two type of code NYT Code & Fixed Code.
NYT code = Traversing tree from the root node to that particular NYT node.
For Fixed Code, it can be calculated from the following two conditions:
- If 0 ≤ k ≤ 2r Then the letter Sk is encoded as the binary representation of (k-1) in (e+1) bits. (where k is position of alphabet in sorted order)
- Else the letter Sk is encoded as the binary representation of (k-r-1) in e bits.
Tree Updation
Tree Updation in Vitter Algorithm follows Implicit Numbering. In Implicit numbering,
- Nodes are numbered in increasing order i.e., by level and from left to right
- The Nodes that have the same weight and the type together form a block
- Blocks are related to each other as by increasing order of their weights
- Internal Node is represented by Oval shape. Weight of internal nodes = Sum of child node weights
- External Node is represented by a square shape. Weight of external nodes = Initially 1 and if repeated then increased the weight by 1
Steps for Tree Updation:
- Initialize the tree with the NYT Node
- For a symbol is recognized for the first time, the initial NYT node is further divided into an NYT Node and new Node initialize to that symbol and weight = 1.
- Assign the sum of the weight of child nodes to the parent node
- If a repeated symbol is encountered than weights are updated to that symbol.
Note: During Updation in Tree if the weight of the left subtree is greater than the right subtree, then nodes must be swapped.
Example
code = "aardvark"
The final Code we get is:
00000 1 010001 0000011 0001011 0 10 110001010
a a r d v a r k
Explanation:
For string code = "aardvark", e = 5, r = 10
As shown in the above image Tree is initialize with NYT Node with weight 0.
- For symbol 'a', k = 1.
NYT Code = "" (initially tree is empty)
For Fixed Code: As k < 2r i.e, 1 < 2*10, satisfy condition (1)
So Fixed Code is Binary Representation of (k-1) = 0 as 5-bit representation
Fixed Code = "00000"
Huffman Code for symbol for 'a' is "00000"
- For symbol 'a' which already exists in the tree. Traversing Tree up to symbol 'a', we get code = "1"
Huffman Code for symbol for 'a' is "1"
- For symbol 'r', k = 18.
NYT Code = "0" (traversing up to NYT Node)
For Fixed Code: As k > 2r i.e, 18 > 2*10, satisfy condition (2)
So Fixed Code is Binary Representation of (k-1 = 17) as 5-bit representation
Fixed Code = "10001"
Huffman Code for symbol for 'r' is "010001"
- For symbol 'd', k = 4.
NYT Code = "000" (traversing up to NYT Node)
For Fixed Code: As k < 2r i.e, 4 < 2*10, satisfy condition (1)
So Fixed Code is Binary Representation of (k-1 = 3) as 5-bit representation
Fixed Code = "00011"
Huffman Code = "00000011"
- For symbol 'v', k = 22.
NYT Code = "000" (traversing up to NYT Node)
For Fixed Code: As k > 2r i.e, 22 > 2*10, satisfy condition (2)
So Fixed Code is Binary Representation of (k-r-1 = 11) as 4-bit representation
Fixed Code = "1011"
Huffman Code = "0001011"
- Swap the node of left subtree and right as the tree is violating property
- For symbol 'a' which already exists in the tree. Traversing Tree up to symbol 'a', we get code = "0"
Huffman Code for symbol for 'a' is "0"
- For symbol 'r' which already exists in the tree. Traversing Tree up to symbol 'a', we get code = "10"
Huffman Code for symbol for 'r' is "10"
- For symbol 'k', k = 11.
NYT Code = "1100" (traversing up to NYT Node)
For Fixed Code: As k < 2r i.e, 11 < 2*10, satisfy condition (1)
So Fixed Code is Binary Representation of (k-1 = 10) as 5-bit representation
Fixed Code = "01010"
Huffman Code for symbol for 'r' is "110001010"
Decoding
Steps for Decoding:
- Read Binary string
- If encountered leaf node is NYT
- Read next e bits
- If e bit value < r, Then to get required symbol convert (e+1) bits to decimal value of (e+1) bits + 1
- If e bit value > r, Then to get required symbol convert e bits to decimal value of e bits + r + 1
Example:
code = "00000101000100000110001011010110001010"
We get final decoded code as
00000 1 0 10001 00 00011 000 1011 0 10 1100 01010
a a NYT r NYT d NYT v a r NYT k
Explanation:
- Begin decoding by reading first e bits. So the first 4 bits are 0000, converting into decimal = 0.
Now the value 0 < r , i.e, 0 < 10 satisfy condition (1).
Now according to the condition (1), convert first e+1 = 5 bit into decimal and add 1 to it.
00000 = 0
0 + 1 = 1, which is value for alphabet a.
Update the tree and add a node for the symbol 'a' in the tree- Read the next bit in the given code and traverse the tree. We reach the external leaf node 'a'. So the next decoded symbol is 'a'.
- Read the next set of bits given code and traverse the tree. We have 0 as NYT Node. After reaching the NYT Node, read e bits which are 1000. Convert 1000 to decimal is 8. As 8 < r satisfy condition (1).
Now Convert e+1 bits in decimal and add 1 to it.
10001 = 17
17 + 1 = 18, which is value for alphabet r.
Update the tree and add a node for the symbol 'r' in the tree.- Reading the next set of bits and traversing the Tree we reach NYT node at 00. Read e bits which are 0001. Convert 0001 to decimal is 1. As 1 < r satisfy condition (1).
Now Convert e+1 bits in decimal and add 1 to it.
00011 = 3
3 + 1 = 4, which is value for alphabet d.
Update the tree and add a node for the symbol 'd' in the tree.- Reading the next set of bits and traversing the Tree we reach NYT node at 000. Read e bits which are 1011. Convert 1011 to decimal is 11. As 11 > r satisfy condition (2).
Now Convert k+r+1 bits in decimal and decode the symbol.
10110 = 22, which is value for alphabet v.
Update the tree and add a node for the symbol 'v' in the tree.- Reading the next set of bits and traversing the Tree we get symbol 'a' at 0. Update the tree and add a node for the symbol 'a' in the tree.
- Reading the next set of bits and traversing the Tree we get symbol 'r' at 10. Update the tree and add a node for the symbol 'a' in the tree.
- Reading the next set of bits and traversing the Tree we reach NYT node at 1100. Read e bits which are 0101. Convert 0101 to decimal is 9. As 9 < r satisfy condition (1).
Now Convert e+1 bits in decimal and and add 1 to it.
01000 = 8,
8 + 1 = 9. which is value for alphabet k.
Update the tree and add a node for the symbol 'v' in the tree.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem