Exemple #1
0
        public HuffmanTree(String code)
        {
            CodeWord = code;
            char[] letters   = code.ToCharArray();
            int[]  frequency = new int[53];
            for (int i = 0; i < code.Length; i++)        // adds once to the corresponding letters index each time it occurs in the codeword
            {
                if (letters[i] > 64 && letters[i] < 91)  // capital letters numeric value
                {
                    frequency[letters[i] - 65]++;        // indexs 0-25
                }
                if (letters[i] > 96 && letters[i] < 123) // lower case letters numeric value
                {
                    frequency[letters[i] + 26 - 97]++;   // index 0-25 used
                }
                if (letters[i] == 32)                    // space numeric value
                {
                    frequency[52]++;                     // last unused index;
                }
            }

            PriorityQueue <BinaryTree <Data> > pQueue = new PriorityQueue <BinaryTree <Data> >(53);
            Data data;
            BinaryTree <Data> BT;

            for (int i = 0; i < 53; i++)                       // for each possible char
            {
                if (frequency[i] != 0)                         // if the frequency is not null
                {
                    if (i < 26)                                // if capital letter create binary tree with its value and frequency inside the root node
                    {
                        data = new Data(i + 65, frequency[i]); //(i +65 gives numeric value of char)
                        BT   = new BinaryTree <Data>();
                        BT.Add(data);
                        pQueue.Add(BT);
                    }
                    if (i > 25 && i < 52)                           // if lower case letter create ""
                    {
                        data = new Data(i + 97 - 26, frequency[i]); //(i+97-26) gives numeric value of char
                        BT   = new BinaryTree <Data>();
                        BT.Add(data);
                        pQueue.Add(BT);
                    }
                    if (i == 52)                           // if space
                    {
                        data = new Data(32, frequency[i]); // 32 is numeric value of space
                        BT   = new BinaryTree <Data>();
                        BT.Add(data);
                        pQueue.Add(BT);
                    }
                }
            }
            Build(pQueue); // build Huffman tree from pQueue of BT<Data>
        }
Exemple #2
0
        private void Build(PriorityQueue <BinaryTree <Data> > pQueue)
        {
            BinaryTree <Data> BT = new BinaryTree <Data>();;
            BinaryTree <Data> BT1;
            BinaryTree <Data> BT2;
            Data data;

            while (pQueue.Size() > 1)                                   // when only 1 value is in pQueue then it is the completed huffman tree
            {
                BT1 = pQueue.Front();                                   // get first element from pQueue
                pQueue.Remove();
                BT2 = pQueue.Front();                                   // get Second element from pQueue
                pQueue.Remove();
                BT   = new BinaryTree <Data>();                         // create a new binary tree
                data = new Data(BT1.Root.Item.Frequency + BT2.Root.Item.Frequency);
                BT.Add(data);                                           // set its root to be the value of BT1 and BT2 combined frequency

                if (BT1.Root.Item.Frequency == BT2.Root.Item.Frequency) // doesnt matter but place larger trees on left
                {
                    if (BT1.Size() < BT2.Size())                        // compare size of trees with equal frequency
                    {
                        BT.Root.Left  = BT2.Root;                       // large tree left
                        BT.Root.Right = BT1.Root;                       // smaller tree right
                    }
                    else
                    {
                        BT.Root.Left  = BT1.Root; // larger tree right
                        BT.Root.Right = BT2.Root; // smalller tree left
                    }
                }
                else // not equal frequency
                {
                    BT.Root.Left  = BT1.Root; // first tree removed has smaller frequency and is placed on left
                    BT.Root.Right = BT2.Root; // second tree removed has larger frequency and is placed on right
                }
                pQueue.Add(BT);               // add the new Binary tree to the Priority Queue
            }
            this.Root = BT.Root;              // set Huffman tree root the the new BT that now is a completed huffman tree
        }