# Huffman Algorithm for t-ary Trees in Data Structure

## A simple algorithm

• A collection of n initial Huffman trees is prepared, each of which is a single leaf node. Keep the n trees onto a priority queue organized by weight (frequency).
• Remove or delete the first two trees (the ones with smallest weight). Combine these two trees to create a new tree whose root is associated with the two trees as children, and whose weight is the sum of the weights of the two children trees.
• Keep this new tree into the priority queue.
• Repeat steps 2-3 until and unless all of the partial Huffman trees have been joined into one.

It's a greedy algorithm: at each iteration, the algorithm creates a "greedy" decision to merge the two subtrees with smallest weight. Is it possible for algorithm to give the desired result?

• Lemma: Let x and y be the two lowest frequent characters. There is an optimal code tree in which x and y are siblings whose depth is minimum as any other leaf nodes in the tree.
• Theorem: Huffman codes are treated as optimal prefix-free binary codes (The greedy algorithm constructs the Huffman tree with the minimum external path weight for a given set of letters).