Esempio n. 1
0
        public void Peek(int[] values)
        {
            ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values).Peek();

            Assert.AreEqual(values.Min(), heap.Value);
            Assert.AreEqual(values.Length, heap.Size);
        }
Esempio n. 2
0
        public void Heapify(int[] values)
        {
            ImmutableMinHeap heap = new ImmutableMinHeap().Heapify(values);

            Assert.AreEqual(values.Length, heap.Size);
            Assert.IsTrue(isHeapValid(heap));
        }
Esempio n. 3
0
 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;
 }
Esempio n. 4
0
 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;
 }
Esempio n. 5
0
        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));
        }
Esempio n. 6
0
        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));
        }
Esempio n. 7
0
 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));
 }
Esempio n. 8
0
 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);
 }
Esempio n. 9
0
 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)));
 }
Esempio n. 10
0
 private ImmutableMinHeap PercolateRootDown(ImmutableMinHeap heap)
 {
     return(isEmpty()
         ? new Leaf()
         : heap.PercolateDown(heap.value, heap.left, heap.right));
 }
Esempio n. 11
0
 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));
 }
Esempio n. 12
0
 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));
 }