Beispiel #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
Beispiel #2
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
Beispiel #3
0
        }//end BuildEncTable method

        //
        //
        //Decompression #2
        //This method creates the binary tree based on the character encoding. Basically, if the current character in the character encoding is a 1, move left in the tree. If it is a 0, move right in the tree.
        //If the node doesn't exist, create it.
        static BinaryTree <char> BuildEncTree(List <CharacterEncoding> encodingTable)
        {
            BinaryTree <char> encodingTree = new BinaryTree <char>('\0'); //the new binary tree that will be developed later in this method.

            //for every CharacterEncoding object in the encoding table
            foreach (CharacterEncoding charEnc in encodingTable)
            {
                //each object being worked with to build the tree has to be instantiated, otherwise it is skipped.
                if (!(charEnc == null))
                {
                    encodingTree.Current = encodingTree.Root;     //start at the top of the tree
                    int encLength = charEnc.GetEncoding().Length; //the length of the encoding string

                    //use every encoding character (i.e. string of 1s and 0s) in the current CharacterEncoding object to create the tree.
                    for (int i = 0; i + 1 <= encLength; ++i) //for loop executes for every character in the encoding string
                    {
                        char c = charEnc.GetEncoding()[i];   //the current character in the encoding string.

                        //1 - move left in the tree.
                        if (c == '1')
                        {
                            //if left doesn't exist, create it.
                            if (encodingTree.Current.Left == null)
                            {
                                //If the node should be a root node (i.e. if the end of the character encoding string has been reached)
                                if (i + 1 == encLength)
                                {
                                    encodingTree.Insert(charEnc.GetCharacter(), BinaryTree <char> .Relative.leftChild);
                                }
                                //If node shouldn't be a root node
                                else
                                {
                                    encodingTree.Insert('\0', BinaryTree <char> .Relative.leftChild);
                                }
                            }
                            //move left
                            encodingTree.moveTo(BinaryTree <char> .Relative.leftChild);
                        }

                        //0 - move right in the tree.
                        else if (c == '0')
                        {
                            //if right doesn't exist, create it.
                            if (encodingTree.Current.Right == null)
                            {
                                //If the node should be a root node (i.e. if the end of the character encoding string has been reached)
                                if (i + 1 == encLength)
                                {
                                    encodingTree.Insert(charEnc.GetCharacter(), BinaryTree <char> .Relative.rightChild);
                                }
                                //If node shouldn't be a root node
                                else
                                {
                                    encodingTree.Insert('\0', BinaryTree <char> .Relative.rightChild);
                                }
                            }
                            //move right
                            encodingTree.moveTo(BinaryTree <char> .Relative.rightChild);
                        }
                    } //end charEncoding.GetEncoding() for loop
                }
            }         //end encodingTable foreach loop

            return(encodingTree);
        }//end BuildEncTree method