예제 #1
0
        public Node BuildTree(Stack <Node> stack)
        {
            //2
            //3. Create a new internal node with frequency equal to the sum of
            // the two nodes frequencies. Make the first extracted node
            // as its left child and the other extracted node as its right child. Add this node to the min heap.
            while (stack.Count > 1)
            {
                Node leftChild  = stack.Pop(); // Make the first extracted node as its left child
                Node rightChild = stack.Pop(); // Make the second extracted node as its rigth child

                // Adding this node to the min heap
                Node newInternalNode = new Node(leftChild, rightChild);
                stack.Push(newInternalNode);
                stack = StackManager.GetSortedStack(stack.ToList <Node>());
            }
            // 4. Make the root node, the tree is complete
            return(stack.Pop());
        }
예제 #2
0
        private void CompressData_Click(object sender, RoutedEventArgs e)
        {
            compress_btn.IsEnabled  = false;
            openFile_btn.IsEnabled  = false;
            decompres_btn.IsEnabled = true;

            string text = ReadInputService.LoadTxt(fileToCompress);

            txtbl_log.Text = textLog.ComposeNewStrRow("Reading file path: ", fileToCompress);

            List <Node> nodes = NodeList.GetCharFrequency(text);

            txtbl_log.Text += textLog.ComposeNewStrRow("Nodes with frequency: ", nodes.ToString());

            Stack <Node> minHeap = StackManager.GetSortedStack(nodes);

            txtbl_log.Text += textLog.ComposeNewStrRow("Min Heap: ", minHeap.ToString());

            treeOfCodes     = huffmanTree.BuildTree(minHeap);
            txtbl_log.Text += textLog.ComposeNewStrRow("Tree of nodes: ", minHeap.ToString());
            finalCode       = huffmanTree.GenerateCode(treeOfCodes, text);
            txtbl_log.Text += textLog.ComposeNewStrRow("Code generated: ", finalCode);
        }