Ejemplo n.º 1
0
        }//end BuildCharOLL method

        //
        //
        //Compression #3
        //This creates a binary tree using the recieved OrderdLinkedList<BinaryTree<CharacterFrequency>> object. This binary tree combines the first two objects in the OrderedLinkedList and puts it back into the OLL in its appropriate spot.
        //When combining the first two BT objects, the first object becomes the left child, the second becomes the right child, and a new BT object is created that sums the character frequency of the children. This object becomes the root.
        static BinaryTree <CharacterFrequency> BuildEncTree(OrderedLinkedList <BinaryTree <CharacterFrequency> > oll)
        {
            //While there is more than one object in the OrderedLinkedList
            while (oll.Count() != 1)
            {
                //Creates two temporary BinaryTree<CharacterFrequency> objects
                BinaryTree <CharacterFrequency> bt1 = null;
                BinaryTree <CharacterFrequency> bt2 = null;

                //These two BT objects refererence the first two objects in the OrderedLinkedList and the first two objects in the OLL are removed
                bt1 = oll.First.Value;
                oll.RemoveFirst();
                bt2 = oll.First.Value;
                oll.RemoveFirst();

                //Creates a new CharacterFrequency object based on the two CF objects stored in the two BinaryTree objects taken from the OrderedLinkedList. Sets the character to '\0' and the frequency to the sum of both CF frequencies
                int newFrequency          = bt1.Current.Data.GetFrequency() + bt2.Current.Data.GetFrequency();
                CharacterFrequency rootCF = new CharacterFrequency('\0', newFrequency);

                //Creates a new BinaryTree object whose root value is the newly created CF object with the '\0' character
                //Adds the first BT object as the left child of this new BinaryTree object and adds the second BT object as the right child
                BinaryTree <CharacterFrequency> tree = new BinaryTree <CharacterFrequency>();
                tree.Insert(rootCF, BinaryTree <CharacterFrequency> .Relative.root);
                tree.Insert(bt1.Root, BinaryTree <CharacterFrequency> .Relative.leftChild);
                tree.Insert(bt2.Root, BinaryTree <CharacterFrequency> .Relative.rightChild);
                //Adds this new BT object that combined the first two objects in the OrderedLinkedList back into the OLL
                oll.Add(tree);

                //Resets the values
                rootCF = null;
                tree   = null;
                bt1    = null;
                bt2    = null;
            }
            //Returns the completed BinaryTree object holding all the CharacterFrequency objects.
            return(oll.First.Value); //The tree has the most frequent nodes on the left and the least frequent on the right
        }//end BuildEncTree method