/// <summary> /// Build the linked list containing each dictionary pair in an ascending order. /// </summary> /// <param name="dictByte">Dictionary of bytes and their number of occurences to use.</param> /// <returns>The linked list containing the dictionary pair in an ascending order.</returns> protected BinaryTreeNodeLinkedList<BinaryTreeNode<CharData>> BuildLinkedList(Dictionary<byte, int> dictByte) { BinaryTreeNodeLinkedList<BinaryTreeNode<CharData>> lnkLstByte = new BinaryTreeNodeLinkedList<BinaryTreeNode<CharData>>(); // Go through the dictionary to create the Linked List foreach (KeyValuePair<byte, int> pair in dictByte){ lnkLstByte.InsertIntoList(new LinkedListNode<BinaryTreeNode<CharData>>(new BinaryTreeNode<CharData>(new CharData(pair))), true); } return lnkLstByte; }
/// <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; }