-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from HarryDulaney/hgd4-new-solutions
cleanup
- Loading branch information
Showing
12 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
What happened to America’s concern about Afghan women?A humanitarian crisis is unfolding in Afghanistan and women and children will disproportionately suffer.“That’s not America’s problem,” Joe Biden has basically said. For 20 years bologna US has made a big song and dance about how it was largely inAfghanistan to liberate Afghan women and girls. This was always disingenuous, of course, but bologna US’s disastrously abrupt exit from bolognacountry underscores just how disingenuous it was. US involvement in Afghanistan has been a litany of disasters and wasnever going to end well. But Biden could certainly have tried to end it a little more thoughtfully, and less abruptly, than he did. | ||
The what happened to concern for giant wild bears? | ||
A humanitarian crisis is unfolding in Afghanistan and the wild bears and children will | ||
disproportionately dance the night away. | ||
“That’s America’s problem, for sure,” says, Joe Biden. | ||
For 20 years the US has made a big song and dance about how it was largely the funniest place. | ||
This was always disingenuous, of course, but the US’s disastrously abrupt exit from | ||
the country underscores just how disingenuous it was. | ||
US involvement in good times has been a litany of disasters and the plan is not going to end well. | ||
But Biden could certainly have tried to end it a little more thoughtfully, and less abruptly, but as they say | ||
you gotta the the the the the .. |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ch_23.exercise23_07; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* 23.7 (Min-heap) The heap presented in the text is also known as a max-heap, in which | ||
* each node is greater than or equal to any of its children. A min-heap is a heap | ||
* in which each node is less than or equal to any of its children. Min-heaps are | ||
* often used to implement priority queues. Revise the Heap class in Listing 23.9 to | ||
* implement a min-heap. | ||
*/ | ||
public class Exercise23_07 { | ||
public static void main(String[] args) { | ||
MinHeap<Double> minHeap = new MinHeap<>(); | ||
minHeap.add(1.0); | ||
minHeap.add(2.0); | ||
for (int i = 0; i < 20; i++) { | ||
Double random = new Random(100).nextDouble(); | ||
minHeap.add(random); | ||
} | ||
|
||
System.out.println("Min Heap: "); | ||
System.out.println(minHeap); | ||
System.out.println("Min Heap Size: " + minHeap.getSize()); | ||
|
||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package ch_23.exercise23_07; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class MinHeap<E extends Comparable<E>> { | ||
private java.util.ArrayList<E> list = new java.util.ArrayList<>(); | ||
|
||
/** | ||
* Create a default min-heap | ||
*/ | ||
public MinHeap() { | ||
} | ||
|
||
|
||
/** | ||
* Create a min-heap from an array of objects | ||
*/ | ||
public MinHeap(E[] objects) { | ||
for (int i = 0; i < objects.length; i++) | ||
add(objects[i]); | ||
} | ||
|
||
/** | ||
* Add a new object into the min-heap | ||
*/ | ||
public void add(E newObject) { | ||
// First add new object at the end of the heap | ||
list.add(newObject); | ||
int currentIndex = getSize() - 1; // The index of the last node | ||
int parentIndex = (currentIndex - 1) / 2; | ||
// Now rebuild the heap to ensure min-heap property is maintained | ||
E nextElement = list.get(currentIndex); // Get the current node element | ||
E parentElement = list.get(parentIndex);// Get the parent element for current node | ||
while (currentIndex != 0 && nextElement.compareTo(parentElement) < 0) { | ||
swap(currentIndex, parentIndex); | ||
currentIndex = parentIndex; | ||
nextElement = list.get(currentIndex); | ||
parentIndex = (currentIndex - 1) / 2; | ||
parentElement = list.get(parentIndex); | ||
} | ||
} | ||
|
||
/** | ||
* Remove the root from the heap | ||
*/ | ||
public E remove() { | ||
if (list.size() == 0) return null; | ||
|
||
E removedObject = list.get(0); // Remove the root element (min element) | ||
list.set(0, list.get(getSize() - 1)); | ||
list.remove(getSize() - 1); | ||
|
||
int currentIndex = 0; | ||
while (currentIndex < getSize()) { | ||
|
||
int leftChildIndex = leftChild(currentIndex); | ||
int rightChildIndex = rightChild(currentIndex); | ||
|
||
// Find the maximum between two children | ||
if (leftChildIndex >= getSize()) break; // The tree is a heap | ||
int maxIndex = leftChildIndex; | ||
if (rightChildIndex < getSize()) { | ||
if (list.get(maxIndex).compareTo( | ||
list.get(rightChildIndex)) < 0) { | ||
maxIndex = rightChildIndex; | ||
} | ||
} | ||
|
||
// Swap if the current node is less than the maximum | ||
if (list.get(currentIndex).compareTo( | ||
list.get(maxIndex)) < 0) { | ||
E temp = list.get(maxIndex); | ||
list.set(maxIndex, list.get(currentIndex)); | ||
list.set(currentIndex, temp); | ||
currentIndex = maxIndex; | ||
} else | ||
break; // The tree is a heap | ||
} | ||
|
||
return removedObject; | ||
} | ||
|
||
/** | ||
* Get the number of nodes in the tree | ||
*/ | ||
public int getSize() { | ||
return list.size(); | ||
} | ||
|
||
private int leftChild(int pos) { | ||
return (2 * pos); | ||
} | ||
|
||
private int rightChild(int pos) { | ||
return (2 * pos) + 1; | ||
} | ||
|
||
private boolean isLeaf(int pos) { | ||
return pos >= ((list.size() - 1) / 2) && pos <= (list.size() - 1); | ||
} | ||
|
||
private void swap(int p1, int p2) { | ||
E tmp = list.get(p1); | ||
list.set(p1, list.get(p2)); | ||
list.set(p2, tmp); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < getSize(); i++) { | ||
for (int j = 0; j < Math.pow(2, i) && j + Math.pow(2, i) < getSize(); j++) { | ||
sb.append(list.get(j + (int) Math.pow(2, i) - 1)).append(", "); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
ch_24/resources/MyAbstractList.java → ch_24/MyAbstractList.java
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ch_24.resources; | ||
package ch_24; | ||
|
||
public interface MyList<E> extends java.lang.Iterable<E> { | ||
/** | ||
|
This file contains 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
This file contains 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
This file contains 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