Kuroshi1

Untitled

May 9th, 2022 (edited)
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. @Override
  2.     public T removeMin() {
  3.         if (size() == 0) {
  4.             throw new NoSuchElementException();
  5.         }
  6.         int index = 0;
  7.         int lastItemIndex = items.size() - 1;
  8.         PriorityNode<T> minNode = items.get(index);
  9.         double priority = items.get(lastItemIndex).getPriority();
  10.         indexMap.remove(items.get(index).getItem());
  11.         items.set(index, items.get(lastItemIndex));
  12.         items.remove(lastItemIndex);
  13.  
  14.         while (!isLeaf(index)) {
  15.             int leftIndex = getLeftChildIndex(index);
  16.             int rightIndex = getRightChildIndex(index);
  17.             boolean leftExists = leftIndex <= size() - 1;
  18.             boolean rightExists = rightIndex <= size() - 1;
  19.  
  20.             if (leftExists && rightExists) {
  21.                 double leftPriority = items.get(leftIndex).getPriority();
  22.                 double rightPriority = items.get(rightIndex).getPriority();
  23.  
  24.                 if (leftPriority < priority && rightPriority < priority) {
  25.                     if (leftPriority < rightPriority) {
  26.                         swap(index, leftIndex);
  27.                         index = leftIndex;
  28.                     } else {
  29.                         swap(index, rightIndex);
  30.                         index = rightIndex;
  31.                     }
  32.                 } else if (leftPriority < priority) {
  33.                     swap(index, leftIndex);
  34.                     index = leftIndex;
  35.                 } else if (rightPriority < priority) {
  36.                     swap(index, rightIndex);
  37.                     index = rightIndex;
  38.                 } else {
  39.                     return minNode.getItem();
  40.                 }
  41.             } else if (leftExists) {
  42.                 double leftPriority = items.get(leftIndex).getPriority();
  43.  
  44.                 if (leftPriority < priority) {
  45.                     swap(index, leftIndex);
  46.                     index = leftIndex;
  47.                 } else {
  48.                     return minNode.getItem();
  49.                 }
  50.             } else if (rightExists) {
  51.                 double rightPriority = items.get(rightIndex).getPriority();
  52.                 if (rightPriority < priority) {
  53.                     swap(index, rightIndex);
  54.                     index = rightIndex;
  55.                 } else {
  56.                     return minNode.getItem();
  57.                 }
  58.             }
  59.         }
  60.         System.out.println(items);
  61.         return minNode.getItem();
  62.     }
  63.  
  64.     // private T percDown(int index, PriorityNode<T> minNode, double priority) {
  65.     //     while (!isLeaf(index)) {
  66.     //         int leftIndex = getLeftChildIndex(index);
  67.     //         int rightIndex = getRightChildIndex(index);
  68.     //         boolean leftExists = leftIndex <= size() - 1;
  69.     //         boolean rightExists = rightIndex <= size() - 1;
  70.     //
  71.     //         if (rightExists) {
  72.     //             double leftPriority = items.get(leftIndex).getPriority();
  73.     //             double rightPriority = items.get(rightIndex).getPriority();
  74.     //
  75.     //             if (rightPriority >= priority && leftPriority >= priority) { // new
  76.     //                 return minNode.getItem();
  77.     //             }
  78.     //
  79.     //             if (leftPriority < priority && rightPriority < priority) {
  80.     //                 if (leftPriority <= rightPriority) {
  81.     //                     swap(index, leftIndex);
  82.     //                     index = leftIndex;
  83.     //                 } else {
  84.     //                     swap(index, rightIndex);
  85.     //                     index = rightIndex;
  86.     //                 }
  87.     //             } else if (leftPriority < priority) {
  88.     //                 swap(index, leftIndex);
  89.     //                 index = leftIndex;
  90.     //             } else {
  91.     //                 swap(index, rightIndex);
  92.     //                 index = rightIndex;
  93.     //             }
  94.     //         } else if (leftExists && items.get(leftIndex).getPriority() < priority) {
  95.     //             swap(index, leftIndex);
  96.     //             index = leftIndex;
  97.     //         } else {
  98.     //             return minNode.getItem();
  99.     //         }
  100.     //     }
  101.     //     return minNode.getItem();
  102.     // }
Add Comment
Please, Sign In to add comment