//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; }
public MinHeapNode GetItemByValue(MinHeapNode item) { foreach (MinHeapNode current in _list) { if (_wordcomparer.Compare(current, item) == 0) { return(current); } } return(default(MinHeapNode)); }
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)); }
//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); }
//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; } }
//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; } }
public MinHeapNode GetItemByValue(MinHeapNode item) { return(heap.GetItemByValue(item)); }
public int CustomInsert(MinHeapNode item) { return(heap.InsertAndGiveIndex(item)); }
public void Insert(MinHeapNode item) { heap.Insert(item); }