Esempio n. 1
0
        private CharacterFrequency[] SortFrequencies(CharacterFrequency[] array)
        {
            int  size   = array.Length;
            bool sorted = false;

            while (!sorted)
            {
                foreach (CharacterFrequency freq in array)
                {
                    if (freq != null)
                    {
                        for (int i = 1; i < size; i++)
                        {
                            int freq1 = array[i].Frequency;
                            int freq2 = array[i - 1].Frequency;
                            if (freq1 <= freq2)
                            {
                                CharacterFrequency temp = array[i];
                                array[i]     = array[i - 1];
                                array[i - 1] = temp;
                                sorted       = true;
                            }
                        }

                        size = size - 1;
                    }
                }
            }
            _characterFrequencyObjectArray = array;
            return(array);
        }
Esempio n. 2
0
        private void HandleInput(string[] s)
        {
            _characterFrequencyObjectArray = new CharacterFrequency[256];
            CharacterFrequency characterFrequencyObject;

            foreach (string aString in s)
            {
                int asciiVal = (int)char.Parse(aString);

                if (_characterFrequencyObjectArray[asciiVal] == null)
                {
                    _characterFrequencyObjectArray[asciiVal] = new CharacterFrequency(aString);
                }
                else if (_characterFrequencyObjectArray[asciiVal].Frequency > 0)
                {
                    _characterFrequencyObjectArray[asciiVal].IncrementFrequency();
                }
                else
                {
                    _characterFrequencyObjectArray[asciiVal] = new CharacterFrequency();
                }
            }
        }
Esempio n. 3
0
        private void HandleInput(char[] chars)
        {
            char[] foundChars = new char[256];
            _characterFrequencyObjectArray = new CharacterFrequency[256];

            foreach (char aChar in chars)
            {
                for (int i = 0; i < _characterFrequencyObjectArray.Length; i++)
                {
                    int asciiVal = (int)aChar;
                    if (_characterFrequencyObjectArray[asciiVal] == null)
                    {
                        _characterFrequencyObjectArray[asciiVal] = new CharacterFrequency(aChar);
                        break;
                    }
                    else if (_characterFrequencyObjectArray[asciiVal].Equals(aChar))
                    {
                        _characterFrequencyObjectArray[asciiVal].IncrementFrequency();
                        break;
                    }
                }
            }
        }
Esempio n. 4
0
        private void BuildBinaryTree(LinkedList <Node <CharacterFrequency> > sortedNodes)
        {
            Node <CharacterFrequency> leftChild;
            Node <CharacterFrequency> rightChild;
            Node <CharacterFrequency> emptyHead;
            CharacterFrequency        noData = new CharacterFrequency();

            emptyHead = new Node <CharacterFrequency>(noData);

            for (int i = 0; i < sortedNodes.Count; i++)
            {
                leftChild = sortedNodes.First.Value;
                sortedNodes.RemoveFirst();
                rightChild = sortedNodes.First.Value;
                sortedNodes.RemoveFirst();
                emptyHead.Left              = leftChild;
                emptyHead.Right             = rightChild;
                emptyHead.Element.Frequency = leftChild.Element.Frequency + rightChild.Element.Frequency;
                emptyHead.Element.Character = '\\';

                foreach (Node <CharacterFrequency> aNode in sortedNodes)
                {
                    //if (emptyHead.Element.Frequency > aNode.Element.Frequency)
                    // add the new node at that location
                }
            }
            //LinkedList<Node<CharacterFrequency>> nodeList = sortedNodes;
            //LinkedList<Node<CharacterFrequency>> tempList = new LinkedList<Node<CharacterFrequency>>();

            //foreach (Node<CharacterFrequency> node in sortedNodes)
            //{
            //    tempList.AddLast(new Node<CharacterFrequency>(noData));
            //    sortedNodes.RemoveFirst();
            //}

            //int combinedFrequencies = 0;
            //LinkedList<Node<CharacterFrequency>> tempList = sortedNodes;
            //Node<CharacterFrequency> nextNode;
            //Node<CharacterFrequency> currentNode;
            //Node<CharacterFrequency> newNode = null;
            //CharacterFrequency emptyNodeChar = new CharacterFrequency("\\");
            //emptyNodeChar.CharacterAsAString = "\\";
            //newNode =

            //int combinedFrequencies = 0;
            //LinkedList<Node<CharacterFrequency>> tempList = sortedNodes;
            //Node<CharacterFrequency> nextNode;
            //Node<CharacterFrequency> currentNode;
            //Node<CharacterFrequency> newNode = null;
            //CharacterFrequency emptyNodeChar = new CharacterFrequency("\\");

            //foreach (Node<CharacterFrequency> node in sortedNodes)
            //{
            //    nodeList.AddLast(node);
            //}

            //while (tempList.Count > 1)
            //{
            //    newNode = new Node<CharacterFrequency>(emptyNodeChar);
            //    for (int i = 0; i < sortedNodes.Count; i++)
            //    {
            //        currentNode = node;
            //        nextNode = sortedNodes[i + 1];
            //        newNode.Left = currentNode;
            //        newNode.Right = nextNode;
            //        newNode.Element = emptyNodeChar;
            //        newNode.Element.Frequency = newNode.Left.Element.Frequency + newNode.Right.Element.Frequency;
            //        tempList.Remove(currentNode);
            //        tempList.Remove(nextNode);
            //        for (int j = 0; j < tempList.Count; j++)
            //        {
            //            if (newNode.Element.Frequency < tempList[j].Element.Frequency)
            //            {
            //                tempList.Insert(j, newNode);
            //                break;
            //            }
            //        }
            //    }
            //}
        }