http://www.darkridge.com/~jpr5/mirror/alg/node56.html
Start at rightmost array position that has a child. Index is n/2.
http://www.geeksforgeeks.org/g-fact-85/
void buildHeap() {
for (int i = (this.numberOfNodes / 2 - 1); i >= 0; i--) {
this.correctNodeIndexByShifting(i);
}
}
void correctNodeIndexByShifting(int arrayIndex) {
int changingArrayIndex = arrayIndex;
if ((changingArrayIndex < 0)
|| (changingArrayIndex >= this.numberOfNodes)) {
throw new IllegalArgumentException(
"In method shiftDown of class "
+ "MaxHeap the value: "
+ changingArrayIndex
+ " represents a node that does not exist in the current heap");
}
while (!this.isLeafNode(changingArrayIndex)) {
int childIndex = this.getLeftChildIndex(changingArrayIndex);
if ((childIndex < (this.numberOfNodes - 1))
&& (this.heap[childIndex]
.compareTo(this.heap[childIndex + 1]) < 0)) {
childIndex++; // childIndex is not at index of child with
// greater node value
}
if (this.heap[changingArrayIndex].compareTo(this.heap[childIndex]) >= 0) {
return;
}
this.swap(changingArrayIndex, childIndex);
changingArrayIndex = childIndex; // node shifted down
}
}
public void insert(E nodeValue) {
if (this.capacity <= this.numberOfNodes) {
throw new IllegalArgumentException("In method insert of class "
+ "MaxHeap the element: " + nodeValue
+ " could not be inserted because the max-heap is full");
}
int currentNodePosition = this.numberOfNodes++;
this.heap[currentNodePosition] = nodeValue;
// start at the end of most bottom right leaf node and shift up
// until the nodeValue has a parent with a greater or equal value
while ((currentNodePosition != 0)
&& (this.heap[currentNodePosition].compareTo(this.heap[this
.getParentIndex(currentNodePosition)]) > 0)) {
this.swap(currentNodePosition,
this.getParentIndex(currentNodePosition));
currentNodePosition = this.getParentIndex(currentNodePosition);
}
}
public E remove(int arrayIndex) {
int changingArrayIndex = arrayIndex;
if ((changingArrayIndex < 0)
|| (changingArrayIndex >= this.numberOfNodes)) {
throw new IllegalArgumentException("In method remove of class "
+ "MaxHeap the input node postion to be removed is invalid");
}
// if the most bottom right node is being removed there is no work to be
// done
if (changingArrayIndex == (this.numberOfNodes - 1)) {
this.numberOfNodes--;
} else {
// swap node to be removed with most bottom right node
this.swap(changingArrayIndex, --this.numberOfNodes);
// if swapped node is large, shift it up the tree
while ((changingArrayIndex > 0)
&& (this.heap[changingArrayIndex].compareTo(this.heap[this
.getParentIndex(changingArrayIndex)]) > 0)) {
this.swap(changingArrayIndex,
this.getParentIndex(changingArrayIndex));
changingArrayIndex = this.getParentIndex(changingArrayIndex);
}
if (this.numberOfNodes != 0) {
// if swapped node is small, shift it down the tree
this.correctNodeIndexByShifting(changingArrayIndex);
}
}
return this.heap[changingArrayIndex];
}
http://analgorithmaday.blogspot.com/2011/01/build-max-heap-with-input-array.html
We can build this heap by putting our data items into the array in any order and then ``heapifying'' the array. Examine the first non-leaf node in the heap-array and compare its key value against that of its greatest child. If it is greater than its greater children, proceed to the next non-leaf node and repeat this process. However, if it is not greater than its greater children then swap these elements. Before continuing to the next non-leaf node and repeating the process the algorithm must be certain that the newly demoted value is in the correct spot. Thus the process must recurse on this demoted value before it can continue with the next leaf on its way to the root. By moving up the whole heap-array in this manner all nodes will end up being greater than their children.
http://www.cise.ufl.edu/~sahni/cop3530/slides/lec242.pdfStart at rightmost array position that has a child. Index is n/2.
http://www.geeksforgeeks.org/g-fact-85/
BUILD-HEAP(A) heapsize := size(A); for i := floor(heapsize/2) downto 1 do HEAPIFY(A, i); end for END
What is the worst case time complexity of the above algo?
Although the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity.
https://github.com/quinnliu/DataStructuresAlgorithmsDesignPatterns/blob/master/src/dataStructures/MaxHeap.javaAlthough the worst case complexity looks like O(nLogn), upper bound of time complexity is O(n). See following links for the proof of time complexity.
void buildHeap() {
for (int i = (this.numberOfNodes / 2 - 1); i >= 0; i--) {
this.correctNodeIndexByShifting(i);
}
}
void correctNodeIndexByShifting(int arrayIndex) {
int changingArrayIndex = arrayIndex;
if ((changingArrayIndex < 0)
|| (changingArrayIndex >= this.numberOfNodes)) {
throw new IllegalArgumentException(
"In method shiftDown of class "
+ "MaxHeap the value: "
+ changingArrayIndex
+ " represents a node that does not exist in the current heap");
}
while (!this.isLeafNode(changingArrayIndex)) {
int childIndex = this.getLeftChildIndex(changingArrayIndex);
if ((childIndex < (this.numberOfNodes - 1))
&& (this.heap[childIndex]
.compareTo(this.heap[childIndex + 1]) < 0)) {
childIndex++; // childIndex is not at index of child with
// greater node value
}
if (this.heap[changingArrayIndex].compareTo(this.heap[childIndex]) >= 0) {
return;
}
this.swap(changingArrayIndex, childIndex);
changingArrayIndex = childIndex; // node shifted down
}
}
public void insert(E nodeValue) {
if (this.capacity <= this.numberOfNodes) {
throw new IllegalArgumentException("In method insert of class "
+ "MaxHeap the element: " + nodeValue
+ " could not be inserted because the max-heap is full");
}
int currentNodePosition = this.numberOfNodes++;
this.heap[currentNodePosition] = nodeValue;
// start at the end of most bottom right leaf node and shift up
// until the nodeValue has a parent with a greater or equal value
while ((currentNodePosition != 0)
&& (this.heap[currentNodePosition].compareTo(this.heap[this
.getParentIndex(currentNodePosition)]) > 0)) {
this.swap(currentNodePosition,
this.getParentIndex(currentNodePosition));
currentNodePosition = this.getParentIndex(currentNodePosition);
}
}
public E remove(int arrayIndex) {
int changingArrayIndex = arrayIndex;
if ((changingArrayIndex < 0)
|| (changingArrayIndex >= this.numberOfNodes)) {
throw new IllegalArgumentException("In method remove of class "
+ "MaxHeap the input node postion to be removed is invalid");
}
// if the most bottom right node is being removed there is no work to be
// done
if (changingArrayIndex == (this.numberOfNodes - 1)) {
this.numberOfNodes--;
} else {
// swap node to be removed with most bottom right node
this.swap(changingArrayIndex, --this.numberOfNodes);
// if swapped node is large, shift it up the tree
while ((changingArrayIndex > 0)
&& (this.heap[changingArrayIndex].compareTo(this.heap[this
.getParentIndex(changingArrayIndex)]) > 0)) {
this.swap(changingArrayIndex,
this.getParentIndex(changingArrayIndex));
changingArrayIndex = this.getParentIndex(changingArrayIndex);
}
if (this.numberOfNodes != 0) {
// if swapped node is small, shift it down the tree
this.correctNodeIndexByShifting(changingArrayIndex);
}
}
return this.heap[changingArrayIndex];
}
http://analgorithmaday.blogspot.com/2011/01/build-max-heap-with-input-array.html