Skip to content

Commit

Permalink
lab10
Browse files Browse the repository at this point in the history
  • Loading branch information
lhy9381 committed Dec 19, 2021
1 parent c271f95 commit c2742b2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lab10/ArrayHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void swim(int index) {
// Throws an exception if index is invalid. DON'T CHANGE THIS LINE.
validateSinkSwimArg(index);
// 0. check if the given node's parent is index 0 or null;
if (parentIndex(index) == 0) return;
if (index == 1) return;
// 1. check if the given node is smaller than its parent
// 2. if not, return
// 3. if yes, get the parent index,
Expand Down Expand Up @@ -173,11 +173,12 @@ public T peek() {
*/
@Override
public T removeMin() {
if (size == 0) return null;
T returnNode = peek();
swap(size, 1);
contents[size] = null;
size -= 1;
sink(1);
if (size > 0) sink(1);
return returnNode;
}

Expand Down Expand Up @@ -208,13 +209,15 @@ public void changePriority(T item, double priority) {
break;
}
}

// 2. update its priority
Node updatedNode = new Node(item, priority);
contents[targetIndex] = updatedNode;
// 3. compare the updated node with its parent and minChild
// to determine if it should swim (p) or sink(c)
int p = parentIndex(targetIndex);
int c = min(leftIndex(targetIndex), rightIndex(targetIndex));
if (p == 0 || c > size) return;
if (min(targetIndex, p) == targetIndex) swim(p);
else if (min(targetIndex, c) == c) sink(c);
else return;
Expand Down
Binary file modified lab10/out/production/lab10/ArrayHeap$Node.class
Binary file not shown.
Binary file modified lab10/out/production/lab10/ArrayHeap.class
Binary file not shown.

0 comments on commit c2742b2

Please sign in to comment.