コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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;
        }
コード例 #4
0
 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);
     }
 }
コード例 #5
0
 public bool IsLeaf(MinHeapNode root)
 {
     return(root.Left == null && root.Right == null);
 }