Esempio n. 1
0
        public void BinaryHeapGetRightChildTest()
        {
            var        heapData  = GetMaxHeapTestData();
            List <int> childList = new List <int>();

            for (int i = 0; i < heapData.Length; i++)
            {
                childList.Add(BinaryHeapBase <int, int> .GetRightChild(heapData.Length, i));
            }
            CollectionAssert.AreEqual(new int[] { 2, 4, 6, -1, -1, -1, -1 }, childList);
        }
Esempio n. 2
0
        public void BinaryHeapGetParentTest()
        {
            var        heapData   = GetMaxHeapTestData();
            List <int> parentList = new List <int>();

            for (int i = 0; i < heapData.Length; i++)
            {
                parentList.Add(BinaryHeapBase <int, int> .GetParent(heapData.Length, i));
            }
            CollectionAssert.AreEqual(new int[] { -1, 0, 0, 1, 1, 2, 2 }, parentList);
        }
Esempio n. 3
0
        public void MaxBinaryHeapPercolateUpTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 0));
            Assert.AreEqual(1, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 1));
            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 2));
            heapData[5] = new BinaryHeapBase <int, int> .KeyValuePair(20, 20);

            Assert.AreEqual(0, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 5));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 17, 1, 4, 6, 5 }, heapData.Select(x => x.Key));
            heapData[6] = new BinaryHeapBase <int, int> .KeyValuePair(18, 18);

            Assert.AreEqual(2, MaxBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 6));
            CollectionAssertEx.AreEqual(new int[] { 20, 13, 18, 1, 4, 6, 17 }, heapData.Select(x => x.Key));
        }
Esempio n. 4
0
        public void MaxBinaryHeapPercolateDownTest()
        {
            var heapData = GetMaxHeapTestData();

            Assert.AreEqual(3, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 3));
            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 4));
            Assert.AreEqual(5, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 5));
            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 6));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(0, 0);

            Assert.AreEqual(4, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 13, 4, 6, 1, 0, 2, 5 }, heapData.Select(x => x.Key));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(3, 3);

            Assert.AreEqual(6, MaxBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 6, 4, 5, 1, 0, 2, 3 }, heapData.Select(x => x.Key));
        }
Esempio n. 5
0
        public void MinBinaryHeapPercolateUpTest()
        {
            var heapData = GetMinHeapTestData();

            Assert.AreEqual(0, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 0));
            Assert.AreEqual(1, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 1));
            Assert.AreEqual(2, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 2));
            heapData[3] = new BinaryHeapBase <int, int> .KeyValuePair(0, 0);

            Assert.AreEqual(0, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 3));
            CollectionAssertEx.AreEqual(new int[] { 0, 1, 2, 15, 17, 4, 3 }, heapData.Select(x => x.Key));
            heapData[6] = new BinaryHeapBase <int, int> .KeyValuePair(1, 1);

            Assert.AreEqual(2, MinBinaryHeap <int, int> .PercolateUp(heapData, heapData.Length, 6));
            Assert.AreEqual(1, heapData[2].Key);
            CollectionAssertEx.AreEqual(new int[] { 0, 1, 1, 15, 17, 4, 2 }, heapData.Select(x => x.Key));
        }
Esempio n. 6
0
        public void MinBinaryHeapPercolateDownTest()
        {
            var heapData = GetMinHeapTestData();

            Assert.AreEqual(3, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 3));
            Assert.AreEqual(4, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 4));
            Assert.AreEqual(5, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 5));
            Assert.AreEqual(6, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 6));
            heapData[0] = new BinaryHeapBase <int, int> .KeyValuePair(40, 40);

            Assert.AreEqual(6, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 0));
            CollectionAssertEx.AreEqual(new int[] { 2, 15, 3, 16, 17, 4, 40 }, heapData.Select(x => x.Key));
            heapData[2] = new BinaryHeapBase <int, int> .KeyValuePair(20, 20);

            Assert.AreEqual(5, MinBinaryHeap <int, int> .PercolateDown(heapData, heapData.Length, 2));
            CollectionAssertEx.AreEqual(new int[] { 2, 15, 4, 16, 17, 20, 40 }, heapData.Select(x => x.Key));
        }
        /// <summary>
        /// Checks whether the element at the given index follows heap properties.
        /// Checking the MaxHeap ordering (node relations) for the node at the given index, to make sure the correct relations between the node and its parent and children holds.
        /// </summary>
        /// <typeparam name="TKey">Type of the keys stored in the heap. </typeparam>
        /// <typeparam name="TValue">Type of the values stored in the heap. </typeparam>
        /// <param name="heap">A max binary heap. </param>
        /// <param name="nodeIndex">Index of a heap node in heap array. </param>
        /// <returns></returns>
        public static bool HasMaxOrderPropertyForNode <TKey, TValue>(BinaryHeapBase <TKey, TValue> heap, int nodeIndex) where TKey : IComparable <TKey>
        {
            int leftChildIndex  = heap.GetLeftChildIndexInHeapArray(nodeIndex);
            int rightChildIndex = heap.GetRightChildIndexInHeapArray(nodeIndex);
            int parentindex     = heap.GetParentIndex(nodeIndex);

            if (leftChildIndex >= 0 && leftChildIndex < heap.HeapArray.Count)
            {
                Assert.IsTrue(heap.HeapArray[nodeIndex].Key.CompareTo(heap.HeapArray[leftChildIndex].Key) >= 0);
            }
            if (rightChildIndex >= 0 && rightChildIndex < heap.HeapArray.Count)
            {
                Assert.IsTrue(heap.HeapArray[nodeIndex].Key.CompareTo(heap.HeapArray[rightChildIndex].Key) >= 0);
            }
            if (parentindex >= 0 && parentindex < heap.HeapArray.Count)
            {
                Assert.IsTrue(heap.HeapArray[nodeIndex].Key.CompareTo(heap.HeapArray[parentindex].Key) <= 0);
            }
            return(true);
        }
Esempio n. 8
0
 public void BinaryHeapGetRightChildGuardCase2Test()
 {
     var heapData = GetMaxHeapTestData();
     var result   = BinaryHeapBase <int, int> .GetRightChild(heapData.Length, 7);
 }
Esempio n. 9
0
 public void BinaryHeapGetParentGuardCase2Test()
 {
     var heapData = GetMinHeapTestData();
     var result   = BinaryHeapBase <int, int> .GetParent(heapData.Length, 7);
 }