public void HuffmanCodes(char[] data, int[] freq, int size) { // Construct Huffman Tree MinHeapNode root = BuildHuffmanTree(data, freq, size); int[] arr = new int[100]; int top = 0; PrintCodes(root, arr, top); }
public MinHeapNode NewNode(char data, int freq) { var node = new MinHeapNode(); node.Left = null; node.Right = null; node.Data = data; node.Freq = freq; Nodes = new List <MinHeapNode>(); return(node); }
public void InsertMinHeap(MinHeap minHeap, MinHeapNode minHeapNode) { minHeap.Size++; minHeap.Nodes.Add(new MinHeapNode()); int i = minHeap.Size - 1; while (i > 0 && minHeapNode.Freq < minHeap.Nodes[(i - 1) / 2].Freq) { minHeap.Nodes[i] = minHeap.Nodes[(i - 1) / 2]; i = (i - 1) / 2; } minHeap.Nodes[i] = minHeapNode; }
public void PrintCodes(MinHeapNode root, int[] arr, int top) { // assign 0 to left edge and recurse if (root.Left != null) { arr[top] = 0; PrintCodes(root.Left, arr, top + 1); } if (root.Right != null) { arr[top] = 1; PrintCodes(root.Right, arr, top + 1); } if (IsLeaf(root)) { Debug.Write(root.Data + ": "); Print(arr, top); } }
public bool IsLeaf(MinHeapNode root) { return(root.Left == null && root.Right == null); }