示例#1
0
        /// <summary>
        /// Factory for building a HuffmanTree from a frequency list.
        /// Also builds up a Dictionary of the nodes for encoding.
        /// </summary>
        /// <param name="freqlist">List of frequencies of characters.</param>
        /// <param name="htDict">Empty dictionary to be filled.</param>
        /// <returns>A Composite HuffmanTreeNode which is the root of the tree. Also passes a dictionary of the nodes by reference.</returns>
        public static HuffmanTreeNodeComposite HuffmanTreeFactory(List <Frequency> freqlist, Dictionary <char, HuffmanTreeNodeLeaf> htDict)
        {
            foreach (Frequency element in freqlist)
            {
                HuffmanTreeNodeLeaf newLeaf = HuffmanTreeFactory(element.symbol, element.frequency);
                htDict.Add(newLeaf.symbol, newLeaf);
            }
            HuffmanTreeNode[] nodeArray = htDict.Values.ToArray();
            //swapped for more dynamic function which uses the array

            /*Heap heap = new Heap();
             * for (int ii = 0; ii < nodeArray.Length; ii++)
             * {
             *  if (nodeArray[ii] != null)
             *      heap.insert(nodeArray[ii]);
             * }*/
            Heap heap = new Heap(nodeArray);
            HuffmanTreeNodeComposite root = null;
            HuffmanTreeNode          left;
            HuffmanTreeNode          right;

            while (heap.size > 1)
            {
                left  = heap.extractMin();
                right = heap.extractMin();
                root  = HuffmanTreeFactory(left, right);
                if (heap.size > 0)
                {
                    heap.insert(root);
                }
            }
            root.parent = null;
            return(root);
        }
示例#2
0
        public static int start()
        {
            Debug.Print("TESTING HEAP");

            Random rand = new Random();
            List<HuffmanTreeNode> huff = new List<HuffmanTreeNode>();
            for (int ii = 0; ii < 100; ii++)
                huff.Add(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256)));

            Heap h = new Heap(huff.ToArray());
            for (int ii = 0; ii < 40; ii++)
                h.extractMin();
            Debug.Print(h.size.ToString());

            for (int ii = 0; ii < 40; ii++)
                h.insert(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256)));
            Debug.Print(h.size.ToString());

            double one = h.extractMin().freq;
            double two;
            while (h.size > 0)
            {
                two = h.extractMin().freq;
                if (one > two)
                {
                    Debug.Print("FAILED " + one.ToString() + " " + two.ToString());
                }
                one = two;
            }
            Debug.Print("PASSED");
            return 0;
        }
示例#3
0
        public static int start()
        {
            Debug.Print("TESTING HEAP");

            Random rand = new Random();
            List <HuffmanTreeNode> huff = new List <HuffmanTreeNode>();

            for (int ii = 0; ii < 100; ii++)
            {
                huff.Add(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256)));
            }

            Heap h = new Heap(huff.ToArray());

            for (int ii = 0; ii < 40; ii++)
            {
                h.extractMin();
            }
            Debug.Print(h.size.ToString());

            for (int ii = 0; ii < 40; ii++)
            {
                h.insert(HuffmanTreeNode.HuffmanTreeFactory('x', rand.Next(256)));
            }
            Debug.Print(h.size.ToString());

            double one = h.extractMin().freq;
            double two;

            while (h.size > 0)
            {
                two = h.extractMin().freq;
                if (one > two)
                {
                    Debug.Print("FAILED " + one.ToString() + " " + two.ToString());
                }
                one = two;
            }
            Debug.Print("PASSED");
            return(0);
        }
示例#4
0
 /// <summary>
 /// Factory for building a HuffmanTree from a frequency list. 
 /// Also builds up a Dictionary of the nodes for encoding.
 /// </summary>
 /// <param name="freqlist">List of frequencies of characters.</param>
 /// <param name="htDict">Empty dictionary to be filled.</param>
 /// <returns>A Composite HuffmanTreeNode which is the root of the tree. Also passes a dictionary of the nodes by reference.</returns>
 public static HuffmanTreeNodeComposite HuffmanTreeFactory(List<Frequency> freqlist, Dictionary<char, HuffmanTreeNodeLeaf> htDict)
 {
     foreach (Frequency element in freqlist)
     {
         HuffmanTreeNodeLeaf newLeaf = HuffmanTreeFactory(element.symbol, element.frequency);
         htDict.Add(newLeaf.symbol, newLeaf);
     }
     HuffmanTreeNode[] nodeArray = htDict.Values.ToArray();
     //swapped for more dynamic function which uses the array
     /*Heap heap = new Heap();
     for (int ii = 0; ii < nodeArray.Length; ii++)
     {
         if (nodeArray[ii] != null)
             heap.insert(nodeArray[ii]);
     }*/
     Heap heap = new Heap(nodeArray);
     HuffmanTreeNodeComposite root = null;
     HuffmanTreeNode left;
     HuffmanTreeNode right;
     while (heap.size > 1)
     {
         left = heap.extractMin();
         right = heap.extractMin();
         root = HuffmanTreeFactory(left, right);
         if (heap.size > 0)
             heap.insert(root);
     }
     root.parent = null;
     return root;
 }