Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

MinHeap c#

public class MinHeap
        {
            private readonly int[] _elements;
            private int _size;

            public MinHeap(int size)
            {
                _elements = new int[size];
            }

            private int GetLeftChildIndex(int elementIndex) => 2 * elementIndex + 1;
            private int GetRightChildIndex(int elementIndex) => 2 * elementIndex + 2;
            private int GetParentIndex(int elementIndex) => (elementIndex - 1) / 2;

            private bool HasLeftChild(int elementIndex) => GetLeftChildIndex(elementIndex) < _size;
            private bool HasRightChild(int elementIndex) => GetRightChildIndex(elementIndex) < _size;
            private bool IsRoot(int elementIndex) => elementIndex == 0;

            private int GetLeftChild(int elementIndex) => _elements[GetLeftChildIndex(elementIndex)];
            private int GetRightChild(int elementIndex) => _elements[GetRightChildIndex(elementIndex)];
            private int GetParent(int elementIndex) => _elements[GetParentIndex(elementIndex)];

            private void Swap(int firstIndex, int secondIndex)
            {
                var temp = _elements[firstIndex];
                _elements[firstIndex] = _elements[secondIndex];
                _elements[secondIndex] = temp;
            }

            public bool IsEmpty()
            {
                return _size == 0;
            }

            public int Peek()
            {
                if (_size == 0)
                    throw new IndexOutOfRangeException();

                return _elements[0];
            }

            public int Pop()
            {
                if (_size == 0)
                    throw new IndexOutOfRangeException();

                var result = _elements[0];
                _elements[0] = _elements[_size - 1];
                _size--;

                ReCalculateDown();

                return result;
            }

            public void Add(int element)
            {
                if (_size == _elements.Length)
                    throw new IndexOutOfRangeException();

                _elements[_size] = element;
                _size++;

                ReCalculateUp();
            }

            private void ReCalculateDown()
            {
                int index = 0;
                while (HasLeftChild(index))
                {
                    var smallerIndex = GetLeftChildIndex(index);
                    if (HasRightChild(index) && GetRightChild(index) < GetLeftChild(index))
                    {
                        smallerIndex = GetRightChildIndex(index);
                    }

                    if (_elements[smallerIndex] >= _elements[index])
                    {
                        break;
                    }

                    Swap(smallerIndex, index);
                    index = smallerIndex;
                }
            }

            private void ReCalculateUp()
            {
                var index = _size - 1;
                while (!IsRoot(index) && _elements[index] < GetParent(index))
                {
                    var parentIndex = GetParentIndex(index);
                    Swap(parentIndex, index);
                    index = parentIndex;
                }
            }
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: Considerando um projeto C# e o uso do Entity Framework, após realizar uma alteração em um registro, qual o método que deve ser chamado para salvar as 
Csharp :: unity google play games plugin spam 
Csharp :: how check if variable is send to page or not in laravwel 
Csharp :: change a dropdown to a specific option via script 
Csharp :: unity destroy object on collision 
Csharp :: if exist TempData[] c# 
Csharp :: unity detect if version is a build or in editor 
Csharp :: how to find avareage of an array in c# 
Csharp :: How to Create Hint, PlaceHolder Text, Watermark In a TextBox vb.net 
Csharp :: how to stop player rotating when hit by object 
Csharp :: enable canvas unity 
Csharp :: which gas is at anode 
Csharp :: oncollisionenter 
Csharp :: c # c^b 
Csharp :: stopwatch c# 
Csharp :: c# dictionary get highest key 
Csharp :: unity get a position inside sphere 
Csharp :: vb.net open file with default program 
Csharp :: pass datatable to stored procedure c# dapper 
Csharp :: C# Cast double to float 
Csharp :: newtonsoft json conditionally ignore property 
Csharp :: c# keyboard enter 
Csharp :: c# calculate difference between two dates in days 
Csharp :: All SQL Server Tables in a Schema 
Csharp :: unity making a coroutine wait until another coroutine is done 
Csharp :: c# dictionary first 
Csharp :: c# OnMouseUp unity 
Csharp :: c# writeline list 
Csharp :: unity how get random color to material 
Csharp :: unity nested list 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =