Skip to content

Commit

Permalink
Merge pull request #60 from HarryDulaney/hgd4-new-solutions
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
HarryDulaney authored Sep 17, 2023
2 parents 450a931 + 219e4bb commit 5a9d01f
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 19 deletions.
1 change: 1 addition & 0 deletions ch_01/Exercise01_04.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Exercise01_04 {

public static void main(String[] args) {
int num = 4;
System.out.println("a a^2 a^3");
for (int row = 1; row <= num; row++) {
System.out.println(row + " " + row * row + " " + row * row * row);
}
Expand Down
11 changes: 10 additions & 1 deletion ch_18/exercise18_31/TestDirectory/testFile3
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 ..
11 changes: 0 additions & 11 deletions ch_23/Exercise23_07.java

This file was deleted.

4 changes: 2 additions & 2 deletions ch_23/resources/Heap.java → ch_23/Heap.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch_23.resources;
package ch_23;

/**
* Listing 23.9 Heap.java, head class provides operations for manipulating
* Listing 23.9 Heap.java, heap class provides operations for manipulating
* a heap
*
* @param <E> Type of Heap nodes
Expand Down
1 change: 1 addition & 0 deletions ch_23/exercise23_05/ComparableHeap.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch_23.exercise23_05;

/**
* Exercise23_05 - Generic heap sort
* A binary heap for inserting and deleting keys while maintaining
* the sorted order
* <p>
Expand Down
27 changes: 27 additions & 0 deletions ch_23/exercise23_07/Exercise23_07.java
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());

}
}
119 changes: 119 additions & 0 deletions ch_23/exercise23_07/MinHeap.java
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();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch_24.resources;
package ch_24;

public abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0; // The size of the list
Expand Down
2 changes: 1 addition & 1 deletion ch_24/resources/MyList.java → ch_24/MyList.java
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> {
/**
Expand Down
2 changes: 1 addition & 1 deletion ch_24/exercise24_01/Exercise24_01.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch_24.exercise24_01;

import ch_24.resources.MyList;
import ch_24.MyList;

/**
* 24.1 (Add set operations in MyList) Define the following methods in MyList and
Expand Down
2 changes: 1 addition & 1 deletion ch_24/exercise24_01/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch_24.exercise24_01;

import ch_24.resources.MyAbstractList;
import ch_24.MyAbstractList;

public class MyArrayList<E> extends MyAbstractList<E> {

Expand Down
2 changes: 1 addition & 1 deletion ch_24/exercise24_02/MyLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch_24.exercise24_02;

import ch_24.resources.MyAbstractList;
import ch_24.MyAbstractList;

/**
* *24.2 (Implement MyLinkedList) The implementations of the methods
Expand Down

0 comments on commit 5a9d01f

Please sign in to comment.