Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public T removeMin() {
- if (size() == 0) {
- throw new NoSuchElementException();
- }
- int index = 0;
- int lastItemIndex = items.size() - 1;
- PriorityNode<T> minNode = items.get(index);
- double priority = items.get(lastItemIndex).getPriority();
- indexMap.remove(items.get(index).getItem());
- items.set(index, items.get(lastItemIndex));
- items.remove(lastItemIndex);
- while (!isLeaf(index)) {
- int leftIndex = getLeftChildIndex(index);
- int rightIndex = getRightChildIndex(index);
- boolean leftExists = leftIndex <= size() - 1;
- boolean rightExists = rightIndex <= size() - 1;
- if (leftExists && rightExists) {
- double leftPriority = items.get(leftIndex).getPriority();
- double rightPriority = items.get(rightIndex).getPriority();
- if (leftPriority < priority && rightPriority < priority) {
- if (leftPriority < rightPriority) {
- swap(index, leftIndex);
- index = leftIndex;
- } else {
- swap(index, rightIndex);
- index = rightIndex;
- }
- } else if (leftPriority < priority) {
- swap(index, leftIndex);
- index = leftIndex;
- } else if (rightPriority < priority) {
- swap(index, rightIndex);
- index = rightIndex;
- } else {
- return minNode.getItem();
- }
- } else if (leftExists) {
- double leftPriority = items.get(leftIndex).getPriority();
- if (leftPriority < priority) {
- swap(index, leftIndex);
- index = leftIndex;
- } else {
- return minNode.getItem();
- }
- } else if (rightExists) {
- double rightPriority = items.get(rightIndex).getPriority();
- if (rightPriority < priority) {
- swap(index, rightIndex);
- index = rightIndex;
- } else {
- return minNode.getItem();
- }
- }
- }
- System.out.println(items);
- return minNode.getItem();
- }
- // private T percDown(int index, PriorityNode<T> minNode, double priority) {
- // while (!isLeaf(index)) {
- // int leftIndex = getLeftChildIndex(index);
- // int rightIndex = getRightChildIndex(index);
- // boolean leftExists = leftIndex <= size() - 1;
- // boolean rightExists = rightIndex <= size() - 1;
- //
- // if (rightExists) {
- // double leftPriority = items.get(leftIndex).getPriority();
- // double rightPriority = items.get(rightIndex).getPriority();
- //
- // if (rightPriority >= priority && leftPriority >= priority) { // new
- // return minNode.getItem();
- // }
- //
- // if (leftPriority < priority && rightPriority < priority) {
- // if (leftPriority <= rightPriority) {
- // swap(index, leftIndex);
- // index = leftIndex;
- // } else {
- // swap(index, rightIndex);
- // index = rightIndex;
- // }
- // } else if (leftPriority < priority) {
- // swap(index, leftIndex);
- // index = leftIndex;
- // } else {
- // swap(index, rightIndex);
- // index = rightIndex;
- // }
- // } else if (leftExists && items.get(leftIndex).getPriority() < priority) {
- // swap(index, leftIndex);
- // index = leftIndex;
- // } else {
- // return minNode.getItem();
- // }
- // }
- // return minNode.getItem();
- // }
Add Comment
Please, Sign In to add comment