Пример #1
0
 public void Print(HuffmanTree tree, TextWriter writer)
 {
     if (!tree.IsLeaf())
     {
         writer.Write("{0} ", tree.root.weight);
         writer.Flush();
     }
     else
     {
         writer.Write("*{0}:{1} ", tree.root.symbol, tree.root.weight);
         writer.Flush();
     }
     if (tree.root.Left != null)
     {
         Print(tree.root.Left, writer);
     }
     if (tree.root.Right != null)
     {
         Print(tree.root.Right, writer);
     }
 }
Пример #2
0
        /// <summary>
        /// Process forest, so only one tree will remain in list after funcion returns.
        /// </summary>
        public void CutDown()
        {
            while (forest.Count > 1)
            {
                forest.Sort();

                HuffmanTree newRoot = new HuffmanTree();
                HuffmanTree tree1   = forest[0];
                HuffmanTree tree2   = forest[1];

                tree1.root.Parent   = newRoot;
                tree2.root.Parent   = newRoot;
                newRoot.root.Left   = tree1;
                newRoot.root.Right  = tree2;
                newRoot.root.weight = tree2.root.weight + tree1.root.weight;
                newRoot.root.symbol = -1;

                forest.RemoveAt(0);
                forest.RemoveAt(0);

                forest.Add(newRoot);
            }
        }
Пример #3
0
 public Node(short symbol)
 {
     Left        = Right = Parent = null;
     this.symbol = symbol;
     weight      = 0;
 }
Пример #4
0
 public Node(short symbol, long weight)
 {
     Left        = Right = Parent = null;
     this.symbol = symbol;
     this.weight = weight;
 }
Пример #5
0
 public Node()
 {
     Left   = Right = Parent = null;
     weight = symbol = 0;
 }