// TODO: This would be more efficient as a member of the other class // ie avoid the recursion public LinkedNode Insert(LinkedNode other) { // 'Next' should have a lower weight // we should return the lower weight if (other.Weight <= Weight) { // insert before if (Next != null) { Next.Prev = other; other.Next = Next; } Next = other; other.Prev = this; return(other); } else { if (Prev == null) { // Insert after other.Prev = null; Prev = other; other.Next = this; } else { Prev.Insert(other); } } return(this); }
private static LinkedNode BuildList(byte[] primeData) { LinkedNode root; root = new LinkedNode(256, 1); root = root.Insert(new LinkedNode(257, 1)); for (int i = 0; i < primeData.Length; i++) { if (primeData[i] != 0) { root = root.Insert(new LinkedNode(i, primeData[i])); } } return(root); }
private static LinkedNode BuildTree(LinkedNode tail) { LinkedNode current = tail; while (current != null) { LinkedNode child0 = current; LinkedNode child1 = current.Prev; if (child1 == null) { break; } LinkedNode parent = new LinkedNode(0, child0.Weight + child1.Weight); parent.Child0 = child0; child0.Parent = parent; child1.Parent = parent; current.Insert(parent); current = current.Prev.Prev; } return(current); }