public void ExtractMax()
        {
            _maxHeap = new MaxIntHeap();
            _maxHeap.Insert(42);
            _maxHeap.Insert(29);
            _maxHeap.Insert(18);
            //_maxHeap.Print();

            // Insert(35)
            _maxHeap.Insert(35);
            //_maxHeap.Print();



            // Test insert
            Assert.AreEqual(42, _maxHeap.Items[0]);
            Assert.AreEqual(35, _maxHeap.Items[1]);
            Assert.AreEqual(18, _maxHeap.Items[2]);
            Assert.AreEqual(29, _maxHeap.Items[3]);

            // Text extract max
            _maxHeap.Print();
            Assert.AreEqual(42, _maxHeap.ExtractMax());
            _maxHeap.Print();

            Assert.AreEqual(35, _maxHeap.ExtractMax());
            Assert.AreEqual(29, _maxHeap.ExtractMax());
            Assert.AreEqual(18, _maxHeap.ExtractMax());

            _maxHeap.Print();
        }
        public void ExtractMaxBigger()
        {
            _maxHeap = new MaxIntHeap();
            _maxHeap.Insert(42);
            _maxHeap.Insert(29);
            _maxHeap.Insert(18);
            _maxHeap.Insert(14);
            _maxHeap.Insert(7);
            _maxHeap.Insert(18);
            _maxHeap.Insert(12);
            _maxHeap.Insert(11);
            _maxHeap.Insert(13);
            _maxHeap.Print();

            Assert.AreEqual(42, _maxHeap.ExtractMax());
            _maxHeap.Print();


            Assert.AreEqual(29, _maxHeap.ExtractMax());
            Assert.AreEqual(18, _maxHeap.ExtractMax());
            Assert.AreEqual(18, _maxHeap.ExtractMax());
            Assert.AreEqual(14, _maxHeap.ExtractMax());
            Assert.AreEqual(13, _maxHeap.ExtractMax());
            Assert.AreEqual(12, _maxHeap.ExtractMax());
            Assert.AreEqual(11, _maxHeap.ExtractMax());
            Assert.AreEqual(7, _maxHeap.ExtractMax());
        }
    public void Peek_ZeroSizeTree_ThrowsArgumentOutOfRangeException()
    {
        MinIntHeap minIntHeap = new MinIntHeap(0);
        MaxIntHeap maxIntHeap = new MaxIntHeap(0);

        // Assert
        var ex = Assert.Throws <ArgumentOutOfRangeException>(() => minIntHeap.peek());

        Assert.Contains("Tree size is zero", ex.Message);

        var ex2 = Assert.Throws <ArgumentOutOfRangeException>(() => maxIntHeap.peek());

        Assert.Contains("Tree size is zero", ex2.Message);
    }
    public void MaxIntHashOverflowTest()
    {
        // Arrange
        MaxIntHeap maxIntHeap = new MaxIntHeap(4);

        int[] arrayAnswer = { 5, 10, 15, 20, 17 };
        int[] myArray     = new int[5];

        // Act
        maxIntHeap.add(10);
        maxIntHeap.add(15);
        maxIntHeap.add(20);
        maxIntHeap.add(17);
        maxIntHeap.add(5);

        // Assert
        Assert.Equal(8, maxIntHeap.GetHeapSize());
    }
    public void MaxHashNodePullFuncTests()
    {
        // Arrange
        MaxIntHeap maxIntHeap = new MaxIntHeap();

        // 10 15 20 17
        int[] arrayAnswer = { 15, 17, 20 };
        int[] myArray     = new int[4];
        maxIntHeap.add(10);
        maxIntHeap.add(15);
        maxIntHeap.add(20);
        maxIntHeap.add(17);

        // Act
        maxIntHeap.pull();

        // Assert
        Assert.Equal(17, maxIntHeap.pull());
    }
    public void MaxIntHeap_Test()
    {
        // Test for MinHash Creation, add, and pull
        // Arrange
        MaxIntHeap maxIntHeap = new MaxIntHeap();

        int[] arrayAnswer = { 10, 15, 20, 17 };
        int[] myArray     = new int[4];

        // Act
        maxIntHeap.add(10);
        maxIntHeap.add(15);
        maxIntHeap.add(20);
        maxIntHeap.add(17);

        // Assert
        Assert.Equal(20, maxIntHeap.pull());
        Assert.Equal(17, maxIntHeap.pull());
        Assert.Equal(15, maxIntHeap.pull());
        Assert.Equal(10, maxIntHeap.pull());
    }
Exemple #7
0
        public IList <string> TopKFrequent(string[] words, int k)
        {
            var result = new List <string>();
            var heap   = new MaxIntHeap(words.Length);

            var dictionary = BuildFrequencyDictionary(words);

            foreach (var pair in dictionary)
            {
                heap.Push(pair);
            }


            while (k > 0)
            {
                var nextResult = heap.Pop();
                result.Add(nextResult.Key + " " + nextResult.Value);
                k--;
            }
            return(result);
        }
Exemple #8
0
        public IList <IList <int> > GetSkyline(int[][] buildings)
        {
            var result = new List <IList <int> >();

            int[] x2Buildings = new int[buildings.GetLength(0)], heights = new int[buildings.GetLength(0)];
            for (int i = 0; i < x2Buildings.Length; i++)
            {
                x2Buildings[i] = buildings[i][1]; heights[i] = buildings[i][2];
            }
            Array.Sort(x2Buildings, heights);                     //Sort x2 and height seqences
            int[,] xAll = new int[buildings.GetLength(0) * 2, 3]; //0: x, 1: height, 2: left/right
            for (int i = 0, j = 0; i < buildings.GetLength(0) || j < x2Buildings.Length;)
            {                                                     // Merging Sort all edges
                int x1 = i < buildings.Length ? buildings[i][0] : int.MaxValue;
                int x2 = j < x2Buildings.Length ? x2Buildings[j] : int.MaxValue;
                if (j == x2Buildings.Length || i < buildings.GetLength(0) && x1 <= x2)
                {
                    xAll[i + j, 0] = buildings[i][0];
                    xAll[i + j, 1] = buildings[i][2];
                    xAll[i + j, 2] = 0;
                    i++;
                }
                else
                {
                    xAll[i + j, 0] = x2Buildings[j];
                    xAll[i + j, 1] = heights[j];
                    xAll[i + j, 2] = 1;
                    j++;
                }
            }

            var curHeights = new Dictionary <int, int>();
            var sH         = new MaxIntHeap();

            for (int i = 0; i < xAll.GetLength(0); i++)
            {
                if (xAll[i, 2] == 0)
                {
                    curHeights[xAll[i, 1]] = curHeights.ContainsKey(xAll[i, 1]) ? curHeights[xAll[i, 1]] + 1 : 1;
                    sH.Push(xAll[i, 1]);
                }
                else
                {
                    if (curHeights[xAll[i, 1]] > 1)
                    {
                        curHeights[xAll[i, 1]]--;
                    }
                    else
                    {
                        curHeights.Remove(xAll[i, 1]);
                        sH.Push(xAll[i, 1]);
                    }
                }
                int curMaxHeight = curHeights.Count == 0 ? 0 : sH.Peek();
                if (result.Count > 0 && result[result.Count - 1][1] == curMaxHeight)
                {
                    continue;
                }
                else if (result.Count > 0 && result[result.Count - 1][0] == xAll[i, 0])
                {
                    result[result.Count - 1][1] = curMaxHeight;
                }
                else
                {
                    result.Add(new int[] { xAll[i, 0], curMaxHeight });
                }
            }
            return(result);
        }