I am implementing a binary heap in Java. I've come to the point where I have to fix the heap ordering whenever a new node is being inserted to the heap. The code below fixes the heap ordering. I have a gut feeling that the code can be improved.
private void fixHeapOrder(Node anomalousNode)
{
if (this.heapType == 0) {
// Min-heaps
while (anomalousNode.getParent().getValue() > anomalousNode.getValue()) {
// In min-heaps, the parent's value must be less than or equal to the
// value of its children.
Node.swapValues(anomalousNode.getParent(), anomalousNode);
anomalousNode = anomalousNode.getParent();
}
} else if (this.heapType == 1) {
// Max-heaps
while (anomalousNode.getParent().getValue() < anomalousNode.getValue()) {
// In max-heaps, the parent's value must be greater than or equal to the
// value of its children.
Node.swapValues(anomalousNode.getParent(), anomalousNode);
anomalousNode = anomalousNode.getParent();
}
}
}
How else can I improve the code?