Created
September 16, 2016 14:03
-
-
Save nichtemna/31f336c5ff313fd2aa06c561e662eba5 to your computer and use it in GitHub Desktop.
MinHeap - binary tree(each node has zero, one, or two children) where the value of each nodes at most the values its children.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MinPriorityQueue { | |
private List<Integer> list = new ArrayList<>(); | |
public MinPriorityQueue() { | |
} | |
public MinPriorityQueue(int capacity) { | |
list = new ArrayList<>(capacity); | |
} | |
public void insert(int value) { | |
list.add(value); | |
siftUp(list.size() - 1); | |
// print(); | |
} | |
public Integer extractMax() { | |
shift(0, list.size() - 1); | |
siftDown(0); | |
return list.remove(list.size() - 1); | |
} | |
public Integer getMax() { | |
if (list.size() > 0) { | |
return list.get(0); | |
} else { | |
throw new RuntimeException("Queue is empty"); | |
} | |
} | |
private void siftDown(int i) { | |
while ((getLeftChild(i) != Integer.MIN_VALUE && getLeftChild(i) < list.get(i)) | |
|| (getRightChild(i) != Integer.MIN_VALUE && getRightChild(i) < list.get(i))) { | |
if (getLeftChild(i) == Integer.MIN_VALUE){ | |
shift(i, getRightChildPosition(i)); | |
i = getRightChildPosition(i); | |
}else{ | |
if (getRightChild(i) > getLeftChild(i)){ | |
shift(i, getLeftChildPosition(i)); | |
i = getLeftChildPosition(i); | |
}else{ | |
shift(i, getRightChildPosition(i)); | |
i = getRightChildPosition(i); | |
} | |
} | |
} | |
} | |
private void siftUp(int i) { | |
while (i > 0 && list.get(i) < getParent(i)) { | |
shift(i, getParentPosition(i)); | |
i = getParentPosition(i); | |
} | |
} | |
private void shift(int i, int j) { | |
int temp = list.get(i); | |
list.set(i, list.get(j)); | |
list.set(j, temp); | |
} | |
private int getLeftChildPosition(int position) { | |
if (2 * position + 1 < list.size()) { | |
return 2 * position + 1; | |
} | |
return -1; | |
} | |
private int getRightChildPosition(int position) { | |
if (2 * position + 2 < list.size()) { | |
return 2 * position + 2; | |
} | |
return -1; | |
} | |
private int getLeftChild(int position) { | |
if (getLeftChildPosition(position) != -1) { | |
return list.get(getLeftChildPosition(position)); | |
} | |
return Integer.MIN_VALUE; | |
} | |
private int getRightChild(int position) { | |
if (getRightChildPosition(position) != -1) { | |
return list.get(getRightChildPosition(position)); | |
} | |
return Integer.MIN_VALUE; | |
} | |
private int getParentPosition(int i) { | |
return (i - 1) / 2; | |
} | |
private int getParent(int i) { | |
return list.get(getParentPosition(i)); | |
} | |
private void print() { | |
for (Integer integer : list) { | |
System.out.print(integer + " "); | |
} | |
System.out.println(); | |
} | |
public boolean isEmpty() { | |
return list == null || list.isEmpty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment