Пример #1
0
        /// <summary>
        /// Build the linked list containing each dictionary pair with their new binary code in a descending order.
        /// </summary>
        /// <param name="binaryTree">Binary tree to use.</param>
        /// <returns>The linked list containing the dictionary pair and their binary code in a descending order.</returns>
        protected DictData BuildBinaryDict(BinaryTree<CharData> binaryTree)
        {
            string headerTree = "";
            // The header is used to rebuild the binary tree when decompressing.
            // Example...
            //      o
            //    /  \
            //   o    o
            //  / \  / \
            // o  o  o  o
            //     \   / \
            //      o  o  o
            // This binary tree would be made like this (If right = 01, left = 10 and parent = 11. 00 would mean the end of the binary tree creation)
            // 01 01 01 11 10 11 11 10 11 11 10 01 01 11 11 10 11 11 11 00

            string currentBinaryCode = "";
            DictData dictTmp = new DictData();
            BinaryTreeNode<CharData> currentNode = binaryTree.Root;
            bool isParent = false;
            while (currentNode != null)
            {
                if (currentNode.Right != null)
                {
                    // Has a right node
                    currentNode = currentNode.Right;
                    headerTree += "01";
                    currentBinaryCode += "1";
                    isParent = false;
                }
                else if (currentNode.Left != null)
                {
                    // Has a left node but not right
                    currentNode = currentNode.Left;
                    headerTree += "10";
                    currentBinaryCode += "0";
                    isParent = false;
                }
                else
                {
                    headerTree += "11";
                    // Is a Leaf
                    if (!isParent)
                    {
                        currentNode.Value.BinaryCode = currentBinaryCode;
                        headerTree += Convert.ToString(Convert.ToByte(currentNode.Value.Pair.Key), 2).PadLeft(8, '0');
                        dictTmp.Add(currentNode.Value.Pair.Key, currentNode.Value.BinaryCode);
                    }
                    currentNode = currentNode.Parent;
                    isParent = true;
                    if (currentNode != null)
                    {
                        currentBinaryCode = currentBinaryCode.Remove(currentBinaryCode.Length - 1);
                        if (currentNode.Right != null)
                            currentNode.Right = null;
                        else currentNode.Left = null;
                    }
                }
            }
            headerTree += "00";
            dictTmp.FileHeader = headerTree;
            return dictTmp;
        }
Пример #2
0
        /// <summary>
        /// Build the binary tree that will be used to create the new byte array.
        /// </summary>
        /// <param name="lnkLstByte">The linked list that will be used to build the binary tree.</param>
        /// <returns>The binary tree that will be used for creating the new byte array.</returns>
        protected BinaryTree<CharData> BuildBinaryTree(BinaryTreeNodeLinkedList<BinaryTreeNode<CharData>> lnkLstByte)
        {
            BinaryTreeNode<CharData> currentLeftNode = null;
            BinaryTreeNode<CharData> currentRightNode = null;
            BinaryTreeNode<CharData> combinedNode = null;
            BinaryTree<CharData> binaryTree = new BinaryTree<CharData>();

            while (lnkLstByte.FirstNode.Next != null)
            {
                currentRightNode = lnkLstByte.FirstNode.Value;
                lnkLstByte.RemoveFirstNode();
                currentLeftNode = lnkLstByte.FirstNode.Value;
                lnkLstByte.RemoveFirstNode();
                combinedNode = new BinaryTreeNode<CharData>(new CharData(new KeyValuePair<byte, int>(new byte(), currentRightNode.Value.Pair.Value + currentLeftNode.Value.Pair.Value)), currentLeftNode, currentRightNode);
                currentLeftNode.Parent = combinedNode;
                currentRightNode.Parent = combinedNode;
                lnkLstByte.InsertIntoList(new LinkedListNode<BinaryTreeNode<CharData>>(combinedNode), true);
            }
            binaryTree.Root = lnkLstByte.FirstNode.Value;
            return binaryTree;
        }