From 219e4bb0ffd1bb04bfa001299c3ea52a3d4bc97d Mon Sep 17 00:00:00 2001 From: Harry Dulaney Date: Sun, 17 Sep 2023 05:42:52 -0400 Subject: [PATCH] cleanup --- ch_01/Exercise01_04.java | 1 + ch_18/exercise18_31/TestDirectory/testFile3 | 11 +- ch_23/Exercise23_07.java | 11 -- ch_23/{resources => }/Heap.java | 4 +- ch_23/exercise23_05/ComparableHeap.java | 1 + ch_23/exercise23_07/Exercise23_07.java | 27 +++++ ch_23/exercise23_07/MinHeap.java | 119 ++++++++++++++++++++ ch_24/{resources => }/MyAbstractList.java | 2 +- ch_24/{resources => }/MyList.java | 2 +- ch_24/exercise24_01/Exercise24_01.java | 2 +- ch_24/exercise24_01/MyArrayList.java | 2 +- ch_24/exercise24_02/MyLinkedList.java | 2 +- 12 files changed, 165 insertions(+), 19 deletions(-) delete mode 100644 ch_23/Exercise23_07.java rename ch_23/{resources => }/Heap.java (96%) create mode 100644 ch_23/exercise23_07/Exercise23_07.java create mode 100644 ch_23/exercise23_07/MinHeap.java rename ch_24/{resources => }/MyAbstractList.java (99%) rename ch_24/{resources => }/MyList.java (98%) diff --git a/ch_01/Exercise01_04.java b/ch_01/Exercise01_04.java index c2aacb9..3676ea8 100644 --- a/ch_01/Exercise01_04.java +++ b/ch_01/Exercise01_04.java @@ -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); } diff --git a/ch_18/exercise18_31/TestDirectory/testFile3 b/ch_18/exercise18_31/TestDirectory/testFile3 index 9f0bd35..15984f6 100644 --- a/ch_18/exercise18_31/TestDirectory/testFile3 +++ b/ch_18/exercise18_31/TestDirectory/testFile3 @@ -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. \ No newline at end of file +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 .. \ No newline at end of file diff --git a/ch_23/Exercise23_07.java b/ch_23/Exercise23_07.java deleted file mode 100644 index ae4bdc0..0000000 --- a/ch_23/Exercise23_07.java +++ /dev/null @@ -1,11 +0,0 @@ -package ch_23; - -/** - * 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 { -} diff --git a/ch_23/resources/Heap.java b/ch_23/Heap.java similarity index 96% rename from ch_23/resources/Heap.java rename to ch_23/Heap.java index 0b56c3d..f6fbb40 100644 --- a/ch_23/resources/Heap.java +++ b/ch_23/Heap.java @@ -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 Type of Heap nodes diff --git a/ch_23/exercise23_05/ComparableHeap.java b/ch_23/exercise23_05/ComparableHeap.java index 9031fe7..9fd7190 100644 --- a/ch_23/exercise23_05/ComparableHeap.java +++ b/ch_23/exercise23_05/ComparableHeap.java @@ -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 *

diff --git a/ch_23/exercise23_07/Exercise23_07.java b/ch_23/exercise23_07/Exercise23_07.java new file mode 100644 index 0000000..fddf864 --- /dev/null +++ b/ch_23/exercise23_07/Exercise23_07.java @@ -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 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()); + + } +} diff --git a/ch_23/exercise23_07/MinHeap.java b/ch_23/exercise23_07/MinHeap.java new file mode 100644 index 0000000..bc36c3d --- /dev/null +++ b/ch_23/exercise23_07/MinHeap.java @@ -0,0 +1,119 @@ +package ch_23.exercise23_07; + +import java.util.NoSuchElementException; + +public class MinHeap> { + private java.util.ArrayList 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(); + } + +} \ No newline at end of file diff --git a/ch_24/resources/MyAbstractList.java b/ch_24/MyAbstractList.java similarity index 99% rename from ch_24/resources/MyAbstractList.java rename to ch_24/MyAbstractList.java index d2ddf56..b324eaa 100644 --- a/ch_24/resources/MyAbstractList.java +++ b/ch_24/MyAbstractList.java @@ -1,4 +1,4 @@ -package ch_24.resources; +package ch_24; public abstract class MyAbstractList implements MyList { protected int size = 0; // The size of the list diff --git a/ch_24/resources/MyList.java b/ch_24/MyList.java similarity index 98% rename from ch_24/resources/MyList.java rename to ch_24/MyList.java index 43bba64..e9e5c3a 100644 --- a/ch_24/resources/MyList.java +++ b/ch_24/MyList.java @@ -1,4 +1,4 @@ -package ch_24.resources; +package ch_24; public interface MyList extends java.lang.Iterable { /** diff --git a/ch_24/exercise24_01/Exercise24_01.java b/ch_24/exercise24_01/Exercise24_01.java index 6643691..9b8b71a 100644 --- a/ch_24/exercise24_01/Exercise24_01.java +++ b/ch_24/exercise24_01/Exercise24_01.java @@ -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 diff --git a/ch_24/exercise24_01/MyArrayList.java b/ch_24/exercise24_01/MyArrayList.java index b99956f..52fd302 100644 --- a/ch_24/exercise24_01/MyArrayList.java +++ b/ch_24/exercise24_01/MyArrayList.java @@ -1,6 +1,6 @@ package ch_24.exercise24_01; -import ch_24.resources.MyAbstractList; +import ch_24.MyAbstractList; public class MyArrayList extends MyAbstractList { diff --git a/ch_24/exercise24_02/MyLinkedList.java b/ch_24/exercise24_02/MyLinkedList.java index 15d434e..53aa596 100644 --- a/ch_24/exercise24_02/MyLinkedList.java +++ b/ch_24/exercise24_02/MyLinkedList.java @@ -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