Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5a9d01f

Browse files
Merge pull request #60 from HarryDulaney/hgd4-new-solutions
cleanup
2 parents 450a931 + 219e4bb commit 5a9d01f

File tree

12 files changed

+165
-19
lines changed

12 files changed

+165
-19
lines changed

‎ch_01/Exercise01_04.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Exercise01_04 {
1212

1313
public static void main(String[] args) {
1414
int num = 4;
15+
System.out.println("a a^2 a^3");
1516
for (int row = 1; row <= num; row++) {
1617
System.out.println(row + " " + row * row + " " + row * row * row);
1718
}
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
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.
1+
The what happened to concern for giant wild bears?
2+
A humanitarian crisis is unfolding in Afghanistan and the wild bears and children will
3+
disproportionately dance the night away.
4+
"That’s America’s problem, for sure," says, Joe Biden.
5+
For 20 years the US has made a big song and dance about how it was largely the funniest place.
6+
This was always disingenuous, of course, but the US’s disastrously abrupt exit from
7+
the country underscores just how disingenuous it was.
8+
US involvement in good times has been a litany of disasters and the plan is not going to end well.
9+
But Biden could certainly have tried to end it a little more thoughtfully, and less abruptly, but as they say
10+
you gotta the the the the the ..

‎ch_23/Exercise23_07.java‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎ch_23/resources/Heap.java‎ renamed to ‎ch_23/Heap.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package ch_23.resources;
1+
package ch_23;
22

33
/**
4-
* Listing 23.9 Heap.java, head class provides operations for manipulating
4+
* Listing 23.9 Heap.java, heap class provides operations for manipulating
55
* a heap
66
*
77
* @param <E> Type of Heap nodes

‎ch_23/exercise23_05/ComparableHeap.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ch_23.exercise23_05;
22

33
/**
4+
* Exercise23_05 - Generic heap sort
45
* A binary heap for inserting and deleting keys while maintaining
56
* the sorted order
67
* <p>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package ch_23.exercise23_07;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* 23.7 (Min-heap) The heap presented in the text is also known as a max-heap, in which
7+
* each node is greater than or equal to any of its children. A min-heap is a heap
8+
* in which each node is less than or equal to any of its children. Min-heaps are
9+
* often used to implement priority queues. Revise the Heap class in Listing 23.9 to
10+
* implement a min-heap.
11+
*/
12+
public class Exercise23_07 {
13+
public static void main(String[] args) {
14+
MinHeap<Double> minHeap = new MinHeap<>();
15+
minHeap.add(1.0);
16+
minHeap.add(2.0);
17+
for (int i = 0; i < 20; i++) {
18+
Double random = new Random(100).nextDouble();
19+
minHeap.add(random);
20+
}
21+
22+
System.out.println("Min Heap: ");
23+
System.out.println(minHeap);
24+
System.out.println("Min Heap Size: " + minHeap.getSize());
25+
26+
}
27+
}

‎ch_23/exercise23_07/MinHeap.java‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package ch_23.exercise23_07;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class MinHeap<E extends Comparable<E>> {
6+
private java.util.ArrayList<E> list = new java.util.ArrayList<>();
7+
8+
/**
9+
* Create a default min-heap
10+
*/
11+
public MinHeap() {
12+
}
13+
14+
15+
/**
16+
* Create a min-heap from an array of objects
17+
*/
18+
public MinHeap(E[] objects) {
19+
for (int i = 0; i < objects.length; i++)
20+
add(objects[i]);
21+
}
22+
23+
/**
24+
* Add a new object into the min-heap
25+
*/
26+
public void add(E newObject) {
27+
// First add new object at the end of the heap
28+
list.add(newObject);
29+
int currentIndex = getSize() - 1; // The index of the last node
30+
int parentIndex = (currentIndex - 1) / 2;
31+
// Now rebuild the heap to ensure min-heap property is maintained
32+
E nextElement = list.get(currentIndex); // Get the current node element
33+
E parentElement = list.get(parentIndex);// Get the parent element for current node
34+
while (currentIndex != 0 && nextElement.compareTo(parentElement) < 0) {
35+
swap(currentIndex, parentIndex);
36+
currentIndex = parentIndex;
37+
nextElement = list.get(currentIndex);
38+
parentIndex = (currentIndex - 1) / 2;
39+
parentElement = list.get(parentIndex);
40+
}
41+
}
42+
43+
/**
44+
* Remove the root from the heap
45+
*/
46+
public E remove() {
47+
if (list.size() == 0) return null;
48+
49+
E removedObject = list.get(0); // Remove the root element (min element)
50+
list.set(0, list.get(getSize() - 1));
51+
list.remove(getSize() - 1);
52+
53+
int currentIndex = 0;
54+
while (currentIndex < getSize()) {
55+
56+
int leftChildIndex = leftChild(currentIndex);
57+
int rightChildIndex = rightChild(currentIndex);
58+
59+
// Find the maximum between two children
60+
if (leftChildIndex >= getSize()) break; // The tree is a heap
61+
int maxIndex = leftChildIndex;
62+
if (rightChildIndex < getSize()) {
63+
if (list.get(maxIndex).compareTo(
64+
list.get(rightChildIndex)) < 0) {
65+
maxIndex = rightChildIndex;
66+
}
67+
}
68+
69+
// Swap if the current node is less than the maximum
70+
if (list.get(currentIndex).compareTo(
71+
list.get(maxIndex)) < 0) {
72+
E temp = list.get(maxIndex);
73+
list.set(maxIndex, list.get(currentIndex));
74+
list.set(currentIndex, temp);
75+
currentIndex = maxIndex;
76+
} else
77+
break; // The tree is a heap
78+
}
79+
80+
return removedObject;
81+
}
82+
83+
/**
84+
* Get the number of nodes in the tree
85+
*/
86+
public int getSize() {
87+
return list.size();
88+
}
89+
90+
private int leftChild(int pos) {
91+
return (2 * pos);
92+
}
93+
94+
private int rightChild(int pos) {
95+
return (2 * pos) + 1;
96+
}
97+
98+
private boolean isLeaf(int pos) {
99+
return pos >= ((list.size() - 1) / 2) && pos <= (list.size() - 1);
100+
}
101+
102+
private void swap(int p1, int p2) {
103+
E tmp = list.get(p1);
104+
list.set(p1, list.get(p2));
105+
list.set(p2, tmp);
106+
}
107+
108+
@Override
109+
public String toString() {
110+
StringBuilder sb = new StringBuilder();
111+
for (int i = 0; i < getSize(); i++) {
112+
for (int j = 0; j < Math.pow(2, i) && j + Math.pow(2, i) < getSize(); j++) {
113+
sb.append(list.get(j + (int) Math.pow(2, i) - 1)).append(", ");
114+
}
115+
}
116+
return sb.toString();
117+
}
118+
119+
}

‎ch_24/resources/MyAbstractList.java‎ renamed to ‎ch_24/MyAbstractList.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ch_24.resources;
1+
package ch_24;
22

33
public abstract class MyAbstractList<E> implements MyList<E> {
44
protected int size = 0; // The size of the list

‎ch_24/resources/MyList.java‎ renamed to ‎ch_24/MyList.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ch_24.resources;
1+
package ch_24;
22

33
public interface MyList<E> extends java.lang.Iterable<E> {
44
/**

‎ch_24/exercise24_01/Exercise24_01.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package ch_24.exercise24_01;
22

3-
import ch_24.resources.MyList;
3+
import ch_24.MyList;
44

55
/**
66
* 24.1 (Add set operations in MyList) Define the following methods in MyList and

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /