public void Peek(int[] values) { ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values).Peek(); Assert.AreEqual(values.Min(), heap.Value); Assert.AreEqual(values.Length, heap.Size); }
public void Heapify(int[] values) { ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values); Assert.AreEqual(values.Length, heap.Size); Assert.IsTrue(isHeapValid(heap)); }
public Branch(ImmutableMinHeap left, ImmutableMinHeap right, int value, int size, int height) { this.left = left; this.right = right; this.value = value; this.size = size; this.height = height; }
public ImmutableMinHeap(int value, ImmutableMinHeap left, ImmutableMinHeap right) { this.left = left; this.right = right; this.value = value; this.size = left.size + right.size + 1; this.height = Math.Max(left.height, right.height) + 1; }
public void Pop(int[] values) { ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values).Pop(); Assert.AreEqual(values.Min(), heap.Value); Assert.AreEqual(values.Length - 1, heap.Size); Assert.IsTrue(isHeapValid(heap)); }
public void Insert(int[] values) { ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values).Insert(5); // var insert = heap.Insert(5); Assert.AreEqual(values.Length + 1, heap.Size); Assert.IsTrue(isHeapValid(heap)); }
private ImmutableMinHeap PercolateUp(int value, ImmutableMinHeap left, ImmutableMinHeap right) { if (left is Branch && value > left.value) { return(new ImmutableMinHeap(left.value, new ImmutableMinHeap(value, left.left, left.right), right)); } if (right is Branch && value > right.value) { return(new ImmutableMinHeap(right.value, new ImmutableMinHeap(value, right.left, right.right), right)); } return(new ImmutableMinHeap(value, left, right)); }
private bool isHeapValid(ImmutableMinHeap heap) { if (heap.Left.Equals(null) && heap.Right.Equals(null)) { return(true); } if (heap.Left != null) { return(heap.Left.Value < heap.Value && isHeapValid(heap.Left)); } if (heap.Right != null) { return(heap.Right.Value < heap.Value && isHeapValid(heap.Right)); } return(false); }
private ImmutableMinHeap MergeChildren(ImmutableMinHeap left, ImmutableMinHeap right) { if (left.isEmpty() && right.isEmpty()) { return(new Leaf()); } if (left.size < Math.Pow(left.height, 2) - 1) { return(FloatLeft(left.value, MergeChildren(left.left, left.right), right)); } if (right.size < Math.Pow(right.height, 2) - 1) { return(FloatRight(right.value, left, MergeChildren(right.left, right.right))); } if (right.height < left.height) { return(FloatLeft(left.value, MergeChildren(left.left, left.right), right)); } return(FloatRight(right.value, left, MergeChildren(right.left, right.right))); }
private ImmutableMinHeap PercolateRootDown(ImmutableMinHeap heap) { return(isEmpty() ? new Leaf() : heap.PercolateDown(heap.value, heap.left, heap.right)); }
private ImmutableMinHeap FloatRight(int value, ImmutableMinHeap left, ImmutableMinHeap right) { return(right is Branch ? new ImmutableMinHeap(right.value, left, new ImmutableMinHeap(value, right.left, right.right)) : new ImmutableMinHeap(value, left, right)); }
private ImmutableMinHeap FloatLeft(int value, ImmutableMinHeap left, ImmutableMinHeap right) { return(left is Branch ? new ImmutableMinHeap(left.value, new ImmutableMinHeap(value, left.left, left.right), right) : new ImmutableMinHeap(value, left, right)); }