public HuffmanLinkedNode Insert(HuffmanLinkedNode other) { Contract.Requires(other != null); Contract.Ensures(Contract.Result<HuffmanLinkedNode>() != null); if (other.Weight <= Weight) { if (Next != null) { Next.Prev = other; other.Next = Next; } Next = other; other.Prev = this; return other; } if (Prev == null) { other.Prev = null; Prev = other; other.Next = this; } else Prev.Insert(other); return this; }
public HuffmanLinkedNode Insert(HuffmanLinkedNode other) { Contract.Requires(other != null); Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null); if (other.Weight <= Weight) { if (Next != null) { Next.Prev = other; other.Next = Next; } Next = other; other.Prev = this; return(other); } if (Prev == null) { other.Prev = null; Prev = other; other.Next = this; } else { Prev.Insert(other); } return(this); }
private static HuffmanLinkedNode InsertNode(HuffmanLinkedNode tail, int decomp) { Contract.Requires(tail != null); var parent = tail; var result = tail.Prev; var temp = new HuffmanLinkedNode(parent.DecompressedValue, parent.Weight); temp.Parent = parent; var newNode = new HuffmanLinkedNode(decomp, 0); newNode.Parent = parent; parent.Child0 = newNode; tail.Next = temp; temp.Prev = tail; newNode.Prev = temp; temp.Next = newNode; AdjustTree(newNode); AdjustTree(newNode); return(result); }
private static HuffmanLinkedNode BuildTree(HuffmanLinkedNode tail) { Contract.Requires(tail != null); Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null); while (tail != null) { var child0 = tail; var child1 = tail.Prev; if (child1 == null) { break; } var parent = new HuffmanLinkedNode(0, child0.Weight + child1.Weight); parent.Child0 = child0; child0.Parent = parent; child1.Parent = parent; tail.Insert(parent); tail = tail.Prev.Prev; } Contract.Assume(tail != null); return(tail); }
private static HuffmanLinkedNode BuildList(IList <byte> primeData) { Contract.Requires(primeData != null); Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null); var root = new HuffmanLinkedNode(byte.MaxValue, 1); root = root.Insert(new HuffmanLinkedNode(byte.MaxValue + 1, 1)); for (var i = 0; i < primeData.Count; i++) { if (primeData[i] != 0) { root = root.Insert(new HuffmanLinkedNode(i, primeData[i])); } } return(root); }
private static HuffmanLinkedNode Decode(BitStreamReader input, HuffmanLinkedNode head) { Contract.Requires(input != null); Contract.Requires(head != null); Contract.Ensures(Contract.Result <HuffmanLinkedNode>() != null); while (head.Child0 != null) { var bit = input.ReadBits(1); if (bit == -1) { throw new InvalidDataException("Unexpected end of file."); } head = bit == 0 ? head.Child0 : head.Child1; } return(head); }
private static void AdjustTree(HuffmanLinkedNode newNode) { while (newNode != null) { newNode.Weight++; HuffmanLinkedNode prev; // Go backwards through the list looking for the insertion point. var insertPoint = newNode; while (true) { prev = insertPoint.Prev; if (prev == null) { break; } if (prev.Weight >= newNode.Weight) { break; } insertPoint = prev; } // No insertion point found. if (insertPoint == newNode) { newNode = newNode.Parent; continue; } // Remove insert point. if (insertPoint.Prev != null) { insertPoint.Prev.Next = insertPoint.Next; } insertPoint.Next.Prev = insertPoint.Prev; // Insert insertPoint after current. insertPoint.Next = newNode.Next; insertPoint.Prev = newNode; if (newNode.Next != null) { newNode.Next.Prev = insertPoint; } newNode.Next = insertPoint; // Remove current. newNode.Prev.Next = newNode.Next; newNode.Next.Prev = newNode.Prev; // Insert current after prev. var temp = prev.Next; newNode.Next = temp; newNode.Prev = prev; temp.Prev = newNode; prev.Next = newNode; // Set up parent/child links. var currentparent = newNode.Parent; var insertParent = insertPoint.Parent; if (currentparent.Child0 == newNode) { currentparent.Child0 = insertPoint; } if (currentparent != insertParent && insertParent.Child0 == insertPoint) { insertParent.Child0 = newNode; } newNode.Parent = insertParent; insertPoint.Parent = currentparent; newNode = newNode.Parent; } }