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 a7d59b5 commit c271f95
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 18 deletions.
8 changes: 8 additions & 0 deletions lab10/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions lab10/.idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lab10/.idea/libraries/javalib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lab10/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 54 additions & 18 deletions lab10/ArrayHeap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,21 @@ public ArrayHeap() {
* Returns the index of the node to the left of the node at i.
*/
private static int leftIndex(int i) {
/* TODO: Your code here! */
return 0;
return 2 * i;
}

/**
* Returns the index of the node to the right of the node at i.
*/
private static int rightIndex(int i) {
/* TODO: Your code here! */
return 0;
return 2 * i + 1;
}

/**
* Returns the index of the node that is the parent of the node at i.
*/
private static int parentIndex(int i) {
/* TODO: Your code here! */
return 0;
return i / 2;
}

/**
Expand Down Expand Up @@ -106,9 +103,15 @@ private int min(int index1, int index2) {
private void swim(int index) {
// Throws an exception if index is invalid. DON'T CHANGE THIS LINE.
validateSinkSwimArg(index);

/** TODO: Your code here. */
return;
// 0. check if the given node's parent is index 0 or null;
if (parentIndex(index) == 0) return;
// 1. check if the given node is smaller than its parent
// 2. if not, return
// 3. if yes, get the parent index,
// and swap parent node and this node then swim from there
if (min(index, parentIndex(index)) != index) return;
swap(index, parentIndex(index));
swim(parentIndex(index));
}

/**
Expand All @@ -117,8 +120,13 @@ private void swim(int index) {
private void sink(int index) {
// Throws an exception if index is invalid. DON'T CHANGE THIS LINE.
validateSinkSwimArg(index);

/** TODO: Your code here. */
// 1. if the current node < the min(children), swap it to that children
// check both children
int minChildIndex = min(leftIndex(index), rightIndex(index));
if (minChildIndex > size) return;
if (min(index, minChildIndex) == index) return;
swap(index, minChildIndex);
sink(minChildIndex);
return;
}

Expand All @@ -133,7 +141,12 @@ public void insert(T item, double priority) {
resize(contents.length * 2);
}

/* TODO: Your code here! */
// 1. add the item to the end of contents
// 2. swim it
Node newNode = new Node(item, priority);
size += 1;
contents[size] = newNode;
swim(size);
}

/**
Expand All @@ -142,8 +155,11 @@ public void insert(T item, double priority) {
*/
@Override
public T peek() {
/* TODO: Your code here! */
return null;
// what is the smallest priority value index?
// it must have the highest priority in the heap
// if correctly understood, the index should start from 1
// because there's nothing at index 0;
return getNode(1).item();
}

/**
Expand All @@ -157,8 +173,12 @@ public T peek() {
*/
@Override
public T removeMin() {
/* TODO: Your code here! */
return null;
T returnNode = peek();
swap(size, 1);
contents[size] = null;
size -= 1;
sink(1);
return returnNode;
}

/**
Expand All @@ -180,8 +200,24 @@ public int size() {
*/
@Override
public void changePriority(T item, double priority) {
/* TODO: Your code here! */
return;
// 1. find the node, otherwise return nothing;
int targetIndex = 0;
for (int i = 1; i <= size; i++) {
if (contents[i].item().equals(item)) {
targetIndex = i;
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 (min(targetIndex, p) == targetIndex) swim(p);
else if (min(targetIndex, c) == c) sink(c);
else return;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions lab10/lab10.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="javalib" level="project" />
</component>
</module>
8 changes: 8 additions & 0 deletions lab10/out/production/lab10/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/out/production/lab10/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions lab10/out/production/lab10/.idea/libraries/javalib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/out/production/lab10/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lab10/out/production/lab10/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lab10/out/production/lab10/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lab10/out/production/lab10/ArrayHeap$Node.class
Binary file not shown.
Binary file added lab10/out/production/lab10/ArrayHeap.class
Binary file not shown.
Binary file added lab10/out/production/lab10/ExtrinsicPQ.class
Binary file not shown.
12 changes: 12 additions & 0 deletions lab10/out/production/lab10/lab10.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="javalib" level="project" />
</component>
</module>

0 comments on commit c271f95

Please sign in to comment.