Efficient Huffman Coding for sorted input
Time complexity from the algorithm is actually O(nLogn). In the event that we all know how the provided array is sorted, we areable to generate Huffman codes within O(n) time. Subsequent is really a O(n) algorithm with regard to sorted input.
- Generate two empty queues.
- Generate a leaf node for every unique character as well as Enqueue this towards the very first queue within non-decreasing order associated with frequency. In the beginning second queue is empty.
- Dequeue two nodes with all the minimum frequency simply by evaluating the particular front of both queues. Repeat following methods two times
- If second queue is actually empty, dequeue through first queue.
- f first queue is empty, dequeue through second queue.
- Else, evaluate the front associated with two queues as well as dequeue the minimum.
- Produce a new internal node along with frequency equal to the sum of the the actual two nodes frequencies. Help to make the first Dequeued node because it's left child and also the second Dequeued node as right child. Enqueue this node to second queue.
- Repeat steps3 and steps4 until there's several node within the queues. The remaining node may be the root node and also the tree is complete.