Example #1
0
        public void PopMaintainsHeapProperty()
        {
            IntBinaryMaxHeap heap = new IntBinaryMaxHeap();

            heap.Push(5);
            heap.Push(10);
            heap.Push(20);
            Assert.Equal(20, heap.Pop());
            Assert.Equal(10, heap.Pop());
            Assert.Equal(5, heap.Pop());
            Assert.Equal(0, heap.Count);
        }
Example #2
0
        public void AddMaintainsHeapProperty()
        {
            IntBinaryMaxHeap heap = new IntBinaryMaxHeap();

            heap.Push(5);
            Assert.Equal(1, heap.Count);
            Assert.Null(heap.Root.Parent);
            Assert.Null(heap.Root.Left);
            Assert.Null(heap.Root.Right);

            heap.Push(10);
            Assert.Equal(2, heap.Count);
            Assert.Null(heap.Root.Parent);
            Assert.Null(heap.Root.Right);
            Assert.Equal(10, heap.Root.Value);
            Assert.Equal(5, heap.Root.Left.Value);
            Assert.Equal(heap.Root, heap.Root.Left.Parent);

            heap.Push(20);
            Assert.Equal(3, heap.Count);
            Assert.Null(heap.Root.Parent);
            Assert.Equal(20, heap.Root.Value);
            Assert.Equal(5, heap.Root.Left.Value);
            Assert.Equal(10, heap.Root.Right.Value);
            Assert.Equal(heap.Root, heap.Root.Left.Parent);
            Assert.Equal(heap.Root, heap.Root.Right.Parent);
            Assert.Null(heap.Root.Left.Left);
            Assert.Null(heap.Root.Left.Right);
            Assert.Null(heap.Root.Right.Left);
            Assert.Null(heap.Root.Right.Right);

            heap.Push(99);
            heap.Push(35);
            heap.Push(777);
            heap.Push(1);
            Assert.Equal(7, heap.Count);
            Assert.Null(heap.Root.Parent);
            Assert.Equal(777, heap.Root.Value);
            Assert.Equal(35, heap.Root.Left.Value);
            Assert.Equal(99, heap.Root.Right.Value);
            Assert.Equal(5, heap.Root.Left.Left.Value);
            Assert.Equal(20, heap.Root.Left.Right.Value);
            Assert.Equal(10, heap.Root.Right.Left.Value);
            Assert.Equal(1, heap.Root.Right.Right.Value);
            Assert.Null(heap.Root.Left.Left.Left);
            Assert.Null(heap.Root.Left.Left.Right);
            Assert.Null(heap.Root.Left.Right.Left);
            Assert.Null(heap.Root.Left.Right.Right);
            Assert.Null(heap.Root.Right.Left.Left);
            Assert.Null(heap.Root.Right.Left.Right);
            Assert.Null(heap.Root.Right.Right.Left);
            Assert.Null(heap.Root.Right.Right.Right);
        }
Example #3
0
        public void PopBasics()
        {
            IntBinaryMaxHeap heap = new IntBinaryMaxHeap();

            heap.Push(5);
            Assert.Equal(1, heap.Count);
            Assert.Null(heap.Root.Parent);
            Assert.Null(heap.Root.Left);
            Assert.Null(heap.Root.Right);

            Assert.Equal(5, heap.Pop());
            Assert.Equal(0, heap.Count);
            Assert.Null(heap.Root);
        }
Example #4
0
        public void PopMaintainsHeapProperty02()
        {
            IntBinaryMaxHeap heap = new IntBinaryMaxHeap();

            heap.Push(5);
            heap.Push(10);
            heap.Push(20);
            heap.Push(99);
            heap.Push(35);
            heap.Push(777);
            heap.Push(1);
            Assert.Equal(777, heap.Pop());
            Assert.Equal(99, heap.Pop());
            Assert.Equal(35, heap.Pop());
            Assert.Equal(20, heap.Pop());
            Assert.Equal(10, heap.Pop());
            Assert.Equal(5, heap.Pop());
            Assert.Equal(1, heap.Pop());
            Assert.Equal(0, heap.Count);
        }