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; } } } }
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())); }
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(); }
public abstract bool IsRoot(HuffmanTreeNode node);
public override bool IsRoot(HuffmanTreeNode node) { return(ReferenceEquals(node, root)); }
public HuffmanTree(HuffmanTreeNode root) { Guard.IsNotNull(root, nameof(root)); this.root = root; }
public HuffmanTreeNode(WeightedSymbol value, HuffmanTreeNode left = null, HuffmanTreeNode right = null) { Value = value; Left = left; Right = right; }
public override bool IsRoot(HuffmanTreeNode node) { return(false); }