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

        //
        //Compression #2
        //This uses the array of character frequency objects and encapsolates each object in a BinaryTree object that is in an OrderedLinkedList object. (i.e. OrderedLinkedList<BinaryTree<CharacterFrequency>>)
        //The output of this method is an OrderedLinkedList of BinaryTree objects containing CharacterFrequency objects. The list is ordered from most infrequent characters to most frequent.
        static OrderedLinkedList <BinaryTree <CharacterFrequency> > BuildCharOLL(Array cfArray)
        {
            OrderedLinkedList <BinaryTree <CharacterFrequency> > oll = new OrderedLinkedList <BinaryTree <CharacterFrequency> >();
            BinaryTree <CharacterFrequency> bt = new BinaryTree <CharacterFrequency>();

            //This loops once for every element in the Array filled withCharacterFrequency
            foreach (CharacterFrequency element in cfArray)
            {
                //If the CF element's frequency is zero, skip adding it to the OrderedLinkedList
                if (element.GetFrequency() != 0)
                {
                    //Create a new tree with the CharacterFrequency object. First parameter - what is being inserted. Second parameter - where in the tree it should be inserted to.
                    bt = new BinaryTree <CharacterFrequency>();
                    bt.Insert(element, BinaryTree <CharacterFrequency> .Relative.root);
                    //Add this new CharacterFrequency object to the list (this object is a Binary tree with only a value for root. They will be combined later).
                    oll.Add(bt);
                }
            }
            return(oll);
        }//end BuildCharOLL method
Ejemplo n.º 2
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