Exemplo n.º 1
0
        public void Should_Check_ExtractTop_MaxHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(100);
            heap.Insert(19);
            heap.Insert(36);
            heap.Insert(17);
            heap.Insert(3);
            heap.Insert(25);
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(7);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(100);
            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);

            heap.Root.Data.ShouldBeEquivalentTo(36);
            heap.Root.Left.Data.ShouldBeEquivalentTo(19);
            heap.Root.Right.Data.ShouldBeEquivalentTo(25);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(1);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(2);
            heap.Root.Left.Left.Right.ShouldBeEquivalentTo(null);
        }
Exemplo n.º 2
0
        public void Should_Check_ExtractTop_MinHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(50);
            heap.Insert(23);
            heap.Insert(88);
            heap.Insert(90);
            heap.Insert(32);
            heap.Insert(74);
            heap.Insert(96);

            //act
            var result = heap.ExtractTop();

            //assert
            result.ShouldBeEquivalentTo(1);
            heap.Count.ShouldBeEquivalentTo(7);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);

            heap.Root.Data.ShouldBeEquivalentTo(23);
            heap.Root.Left.Data.ShouldBeEquivalentTo(50);
            heap.Root.Right.Data.ShouldBeEquivalentTo(32);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(88);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(90);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(96);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(74);
        }
Exemplo n.º 3
0
        private Dictionary <T, T?> ComputePaths(T source, MyGraphAdj <T> graph)
        {
            var vertexes = graph.GetAllVertexes().ToList();
            var edges    = graph.GetAllEdges().ToList();

            Dictionary <T, int> distances        = new Dictionary <T, int>();
            Dictionary <T, T?>  parent           = new Dictionary <T, T?>();
            Dictionary <T, int> heapMinDistances = new Dictionary <T, int>();

            var heap = new MyBinaryHeap <MyBinaryHeapKeyNode <T, int> >(MyBinaryHeapType.MinHeap);

            foreach (var vertex in vertexes)
            {
                heap.Insert(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Key.CompareTo(source) == 0 ? 0 : Int32.MaxValue));
                heapMinDistances.Add(vertex.Key, Int32.MaxValue);
            }
            parent[source]    = null;
            distances[source] = 0;

            while (heap.Count > 0)
            {
                var current = heap.ExtractTop();
                var nodes   = edges.Where(x => x[0].Equals(
                                              vertexes.First(y => y.Key.CompareTo(current.Key) == 0).Value)).ToList();
                if (nodes.Count == 0)
                {
                    break;
                }
                var parentNode = vertexes.First(x => x.Key.CompareTo(current.Key) == 0);
                foreach (var node in nodes)
                {
                    var vertex = vertexes.First(x => x.Value == node[1]);
                    if (!heap.Contains(new MyBinaryHeapKeyNode <T, int>(vertex.Key, vertex.Value)))
                    {
                        continue;
                    }

                    var currentDistance = edges.First(x =>
                                                      x[0] == parentNode.Value && x[1] == vertex.Value)[2];
                    distances[vertex.Key] = distances[current.Key] + currentDistance;

                    if (heapMinDistances[vertex.Key] >= distances[vertex.Key])
                    {
                        parent[vertex.Key] = current.Key;

                        heap.Decrease(new MyBinaryHeapKeyNode <T, int>(vertex.Key, Int32.MaxValue),
                                      new MyBinaryHeapKeyNode <T, int>(vertex.Key, currentDistance));
                        heapMinDistances[vertex.Key] = currentDistance;
                    }
                }
            }

            return(parent);
        }
Exemplo n.º 4
0
        public void Should_Check_ExtractTop_MinHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(1);
            heap.Insert(3);
            heap.Insert(6);
            heap.Insert(5);
            heap.Insert(9);
            heap.Insert(8);

            //act

            //assert
            heap.ExtractTop().ShouldBeEquivalentTo(1);
            heap.ExtractTop().ShouldBeEquivalentTo(3);
            heap.ExtractTop().ShouldBeEquivalentTo(5);
            heap.ExtractTop().ShouldBeEquivalentTo(6);
            heap.ExtractTop().ShouldBeEquivalentTo(8);
            heap.ExtractTop().ShouldBeEquivalentTo(9);

            heap.Root.ShouldBeEquivalentTo(null);
            heap.Count.ShouldBeEquivalentTo(0);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
Exemplo n.º 5
0
        public void Should_Check_ExtractTop_Insert_MaxHeap_Multiple()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();
            heap.ExtractTop();
            heap.ExtractTop();

            //act
            heap.Insert(1);
            heap.Insert(2);
            heap.Insert(3);
            heap.Insert(11);
            heap.Insert(6);

            //assert
            heap.Root.Data.ShouldBeEquivalentTo(11);
            heap.Root.Left.Data.ShouldBeEquivalentTo(7);
            heap.Root.Right.Data.ShouldBeEquivalentTo(10);
            heap.Root.Left.Left.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Right.Data.ShouldBeEquivalentTo(2);
            heap.Root.Right.Left.Data.ShouldBeEquivalentTo(3);
            heap.Root.Right.Right.Data.ShouldBeEquivalentTo(6);
            heap.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(1);

            heap.Count.ShouldBeEquivalentTo(8);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Exemplo n.º 6
0
        public void Should_Check_ExtractTop_MaxHeap_Throw_If_Empty()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            //act
            Action act = () => heap.ExtractTop();

            //assert
            act.ShouldThrow <InvalidOperationException>();

            heap.Root.ShouldBeEquivalentTo(null);
            heap.Count.ShouldBeEquivalentTo(0);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Exemplo n.º 7
0
        public IEnumerable <T> HeapSort(IEnumerable <T> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException();
            }
            var array = list.ToArray();

            var heap = new MyBinaryHeap <T>(MyBinaryHeapType.MinHeap);

            foreach (var item in array)
            {
                heap.Insert(item);
            }

            while (heap.Count > 0)
            {
                yield return(heap.ExtractTop());
            }
        }
Exemplo n.º 8
0
        public void Should_Check_Contains_False()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();
            //act
            var result = heap.Contains(17);

            //assert
            result.ShouldBeEquivalentTo(false);

            heap.Count.ShouldBeEquivalentTo(5);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }
Exemplo n.º 9
0
        public void Should_Throw_Decrease_If_Higher_Value_MinHeap()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MinHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();

            //act
            Action act = () => heap.Decrease(17, 20);

            //assert
            act.ShouldThrow <ArgumentException>();

            heap.Count.ShouldBeEquivalentTo(5);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MinHeap);
        }
Exemplo n.º 10
0
        public void Should_Throw_Decrease_If_Not_Contains()
        {
            //arrange
            var heap = new MyBinaryHeap <int>(MyBinaryHeapType.MaxHeap);

            heap.Insert(17);
            heap.Insert(15);
            heap.Insert(10);
            heap.Insert(6);
            heap.Insert(10);
            heap.Insert(7);

            heap.ExtractTop();

            //act
            Action act = () => heap.Decrease(17, 11);

            //assert
            act.ShouldThrow <InvalidOperationException>();

            heap.Count.ShouldBeEquivalentTo(5);
            heap.HeapType.ShouldBeEquivalentTo(MyBinaryHeapType.MaxHeap);
        }