예제 #1
0
        public override void DoPOT(IHuffmanTreeVisitor visitor)
        {
            HuffmanTreeNode node  = root;
            int             level = 0;

            Stack <HuffmanTreeNode> stack = new Stack <HuffmanTreeNode>();

            while (true)
            {
                if (node != null)
                {
                    stack.Push(node);
                    node = node.Left;
                    visitor.VisitLeftEdge(++level);
                }
                else
                {
                    if (stack.Count == 0)
                    {
                        return;
                    }

                    if (stack.Peek().Right == null)
                    {
                        visitor.VisitBackEdge(--level);
                        node = stack.Pop();
                        if (node.IsLeaf)
                        {
                            visitor.VisitNode(node);
                        }

                        while (stack.Count != 0 && node == stack.Peek().Right)
                        {
                            visitor.VisitBackEdge(--level);
                            node = stack.Pop();
                            if (node.IsLeaf)
                            {
                                visitor.VisitNode(node);
                            }
                        }
                    }
                    if (stack.Count != 0)
                    {
                        node = stack.Peek().Right;
                        visitor.VisitRightEdge(level);
                    }
                    else
                    {
                        node = null;
                    }
                }
            }
        }
예제 #2
0
        public override void DoBFS(IHuffmanTreeVisitor visitor)
        {
            Guard.IsNotNull(visitor, nameof(visitor));

            Queue <HuffmanTreeNode> bfsQueue = new Queue <HuffmanTreeNode>(64);

            bfsQueue.Enqueue(root);
            while (bfsQueue.Count != 0)
            {
                HuffmanTreeNode node = bfsQueue.Dequeue();
                visitor.VisitNode(node);
                if (node == null)
                {
                    continue;
                }
                bfsQueue.Enqueue(node.Left);
                bfsQueue.Enqueue(node.Right);
            }
        }
            public TreeWalkStep VisitNode(HuffmanTreeNode node)
            {
                if (node.IsLeaf)
                {
                    output.WriteSymbol(node.Value.Symbol);
                    if (tree.IsRoot(node))
                    {
                        if (input.ReadBit(out Bit _))
                        {
                            seqLength--;
                        }
                    }
                    return(TreeWalkStep.Finish);
                }

                if (!input.ReadBit(out Bit bit))
                {
                    throw new ArgumentException();
                }
                seqLength--;
                return(bit == Bit.Zero ? TreeWalkStep.ToLeft : TreeWalkStep.ToRight);
            }
        public HuffmanTreeBase BuildHuffmanTree(WeightsTable weightsTable)
        {
            Guard.IsNotNull(weightsTable, nameof(weightsTable));
            HuffmanTreeNodePriorityQueue priorityQueue = new HuffmanTreeNodePriorityQueue();

            foreach (WeightedSymbol weightedSymbol in weightsTable)
            {
                priorityQueue.Insert(weightedSymbol.Weight, new HuffmanTreeNode(weightedSymbol));
            }
            if (priorityQueue.Size == 0)
            {
                return(EmptyHuffmanTree.Instance);
            }

            while (priorityQueue.Size > 1)
            {
                var  lNode        = priorityQueue.DeleteMinimumValue();
                var  rNode        = priorityQueue.DeleteMinimumValue();
                long weight       = lNode.Value.Weight + rNode.Value.Weight;
                var  internalNode = new HuffmanTreeNode(new WeightedSymbol(0, weight), lNode, rNode);
                priorityQueue.Insert(weight, internalNode);
            }
            return(new HuffmanTree(priorityQueue.GetMinimumValue()));
        }
예제 #5
0
        public override bool Walk(IHuffmanTreeWalker walker)
        {
            HuffmanTreeNode node = root;

            while (node != null)
            {
                TreeWalkStep walkStep = walker.VisitNode(node);
                switch (walkStep)
                {
                case TreeWalkStep.ToLeft:
                    node = node.Left;
                    break;

                case TreeWalkStep.ToRight:
                    node = node.Right;
                    break;

                case TreeWalkStep.Finish: return(true);

                default: throw new StreamFormatException();
                }
            }
            return(false);
        }
 public void VisitNode(HuffmanTreeNode node)
 {
     codingTable[node.Value.Symbol] = bitSequence.Clone();
 }
예제 #7
0
 public abstract bool IsRoot(HuffmanTreeNode node);
예제 #8
0
 public override bool IsRoot(HuffmanTreeNode node)
 {
     return(ReferenceEquals(node, root));
 }
예제 #9
0
 public HuffmanTree(HuffmanTreeNode root)
 {
     Guard.IsNotNull(root, nameof(root));
     this.root = root;
 }
예제 #10
0
 public HuffmanTreeNode(WeightedSymbol value, HuffmanTreeNode left = null, HuffmanTreeNode right = null)
 {
     Value = value;
     Left  = left;
     Right = right;
 }
예제 #11
0
 public override bool IsRoot(HuffmanTreeNode node)
 {
     return(false);
 }