Exemplo n.º 1
0
        //MinHeapNodeo swap the value of the array for the given two array index
        private void SwapCells(int i, int j)
        {
            MinHeapNode temp = _list[i];

            _list[i] = _list[j];
            //Also update the index change
            _list[i].root._heapArrayIndex = j;

            _list[j] = temp;
            _list[j].root._heapArrayIndex = i;
        }
Exemplo n.º 2
0
        public MinHeapNode GetItemByValue(MinHeapNode item)
        {
            foreach (MinHeapNode current in _list)
            {
                if (_wordcomparer.Compare(current, item) == 0)
                {
                    return(current);
                }
            }

            return(default(MinHeapNode));
        }
Exemplo n.º 3
0
        public int InsertAndGiveIndex(MinHeapNode e)
        {
            //if the list has space
            if (Count >= _list.Count)
            {
                _list.Add(e);
            }
            else
            {
                _list[Count] = e;
            }

            Count++;

            return(CustomHeapUp(Count - 1));
        }
Exemplo n.º 4
0
        //Insert element into the heap
        //Logic
        //Add the element to the bottom level of the heap.
        //Compare the added element with its parent; if they are in the correct order, stop.
        //If not, swap the element with its parent and return to the previous step.
        public void Insert(MinHeapNode e)
        {
            //if the list has space
            if (Count >= _list.Count)
            {
                _list.Add(e);
            }
            else
            {
                _list[Count] = e;
            }

            Count++;

            HeapUp(Count - 1);
        }
Exemplo n.º 5
0
        //MinHeapNodeo swap the value of the array for the given two array index
        private void SwapCells(int i, int j)
        {
            MinHeapNode temp = _list[i];

            _list[i] = _list[j];
            //Also update the index change
            if (_list[i].root != null)
            {
                _list[i].root._heapArrayIndex = j;
            }

            _list[j] = temp;
            if (_list[j].root != null)
            {
                _list[j].root._heapArrayIndex = temp.root._heapArrayIndex;
            }
        }
Exemplo n.º 6
0
        //Heapup or bubble up is done to make sure the
        private void HeapUp(int i)
        {
            MinHeapNode elt = _list[i];

            while (true)
            {
                int parent = Parent(i);
                //if the parent value is lesser than o or
                //if the comparer value is greater than 0 then break...
                //eg in case of min-heap the parent should be smaller
                if (parent < 0 || _comparer.Compare(_list[parent], elt) > 0)
                {
                    break;
                }
                SwapCells(i, parent);
                //this loop will continue untill the parent heap condition is satified
                //ie in min-heap the parent should be the smallest
                i = parent;
            }
        }
Exemplo n.º 7
0
 public MinHeapNode GetItemByValue(MinHeapNode item)
 {
     return(heap.GetItemByValue(item));
 }
Exemplo n.º 8
0
 public int CustomInsert(MinHeapNode item)
 {
     return(heap.InsertAndGiveIndex(item));
 }
Exemplo n.º 9
0
 public void Insert(MinHeapNode item)
 {
     heap.Insert(item);
 }