Skip to content

Commit a4a2871

Browse files
committed
add lab8
1 parent b1adfbe commit a4a2871

15 files changed

+246
-0
lines changed

LAB8_Heap/.DS_Store

6 KB
Binary file not shown.

LAB8_Heap/.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LAB8_Heap/.idea/checkstyle-idea.xml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LAB8_Heap/.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LAB8_Heap/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LAB8_Heap/LAB8_Heap.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
2.46 KB
Binary file not shown.
Binary file not shown.

LAB8_Heap/png/addition_heap.png

241 KB
Loading
263 KB
Loading

LAB8_Heap/png/insert.png

208 KB
Loading

LAB8_Heap/png/reheapDown.png

290 KB
Loading

LAB8_Heap/png/reheapUp.png

199 KB
Loading

LAB8_Heap/src/Heap.java

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/**
2+
* The `Heap` class represents a simple heap data structure.
3+
* A heap is a specialized tree-based data structure that satisfies the heap property.
4+
* In this implementation, the heap is represented as an array.
5+
* The class provides methods for insertion, deletion, and heap sort.
6+
*
7+
*/
8+
public class Heap {
9+
10+
/** The current number of elements in the heap. */
11+
int load = 0;
12+
13+
/** The array representing the heap. */
14+
int[] hArray = new int[100];
15+
16+
/**
17+
* Constructs an empty heap.
18+
*/
19+
Heap() {
20+
}
21+
22+
/**
23+
* Performs the reheapUp operation to maintain the heap property after insertion.
24+
*
25+
* @param currentIndex The index of the current element.
26+
*/
27+
void reheapUp(int currentIndex) {
28+
// Exercise 1
29+
// add your code here
30+
if (currentIndex > 0){ //if index isn't a leaf node
31+
int parentIndex = (currentIndex - 1)/2;
32+
if (hArray[currentIndex] > hArray[parentIndex]){
33+
swap(hArray, currentIndex, parentIndex);
34+
}
35+
reheapUp(parentIndex);
36+
}
37+
}
38+
39+
/**
40+
* Inserts a new element into the heap and performs the necessary reheapUp operation.
41+
*
42+
* @param data The data to be inserted into the heap.
43+
*/
44+
void insert(int data) {
45+
// Exercise 2
46+
// add your code here
47+
hArray[load] = data; //add new data to the next current data
48+
load++;
49+
reheapUp(load - 1); //reheap up the data we inserted
50+
}
51+
52+
/**
53+
* Performs the reheapDown operation to maintain the heap property after deletion.
54+
*
55+
* @param currentIndex The index of the current element.
56+
*/
57+
void reheapDown(int currentIndex) {
58+
// Exercise 3
59+
// add your code here
60+
int lastIndex = load - 1; // Subtract 1 to get the index of the last element in the heap
61+
62+
// if (currentIndex < lastIndex) {
63+
// int largestChildIndex;
64+
// int leftNodeIndex = 2 * currentIndex + 1;
65+
// int rightNodeIndex = 2 * currentIndex + 2;
66+
//
67+
// if (rightNodeIndex <= lastIndex) { // Check if the right child exists within the bounds
68+
// if (hArray[rightNodeIndex] > hArray[leftNodeIndex]) { //if rightNode is the largest child
69+
// largestChildIndex = rightNodeIndex;
70+
// } else { //if leftNode is the largest child
71+
// largestChildIndex = leftNodeIndex;
72+
// }
73+
// } else { // If there's no right child, use the left child
74+
// largestChildIndex = leftNodeIndex;
75+
// }
76+
//
77+
// if (hArray[currentIndex] < hArray[largestChildIndex]) {
78+
// swap(hArray, currentIndex, largestChildIndex);
79+
// reheapDown(largestChildIndex);
80+
// }
81+
// }
82+
83+
//same method as in the lab's skeleton
84+
if (2*currentIndex + 1 <= lastIndex){
85+
int largestChildIndex;
86+
int leftNodeIndex = 2 * currentIndex + 1;
87+
int rightNodeIndex = 2 * currentIndex + 2;
88+
89+
if(rightNodeIndex > lastIndex){
90+
largestChildIndex = leftNodeIndex;
91+
} else if (hArray[rightNodeIndex] > hArray[leftNodeIndex]) {
92+
largestChildIndex = rightNodeIndex;
93+
} else{
94+
largestChildIndex = leftNodeIndex;
95+
}
96+
97+
if (hArray[currentIndex] < hArray[largestChildIndex]) {
98+
swap(hArray, currentIndex, largestChildIndex);
99+
reheapDown(largestChildIndex);
100+
}
101+
}
102+
}
103+
104+
/**
105+
* Deletes the root element of the heap and performs the necessary reheapDown operation.
106+
*
107+
* @return The value of the root element that was deleted.
108+
*/
109+
int deleteRoot() {
110+
// Exercise 4
111+
// add your code here
112+
int temp = hArray[0]; //temp = root(array index 0) for return
113+
if (load - 1 >= 0){ //if last index > 0 -> if level > 0 -> root has children
114+
hArray[0] = hArray[load-1]; //replace the data at root with the latest data
115+
hArray[load-1] = 0; //clear the last data
116+
load--; //update the load value
117+
reheapDown(0); //call reheap down starting at Index 0
118+
}
119+
return temp;
120+
}
121+
122+
/**
123+
* Sorts the heap in ascending order.
124+
*/
125+
void makeHeapSort() {
126+
// Exercise 5
127+
// add your code here
128+
while(load > 0){ //do it until no nodes left
129+
System.out.print(deleteRoot() + " ");
130+
}
131+
System.out.println();
132+
}
133+
134+
/**
135+
* Finds the level of a given node in the heap.
136+
*
137+
* @param nodeIndex The index of the node.
138+
* @return The level of the node in the heap.
139+
*/
140+
int findLevel(int nodeindex) {
141+
int parent = (nodeindex - 1) / 2;
142+
int level = 0;
143+
144+
if (parent > 0)
145+
level++;
146+
147+
while (parent > 0) {
148+
parent = (parent - 1) / 2;
149+
level++;
150+
}
151+
return level;
152+
}
153+
154+
/**
155+
* Swaps two elements in the heap array.
156+
*
157+
* @param A The heap array.
158+
* @param ind1 The index of the first element to be swapped.
159+
* @param ind2 The index of the second element to be swapped.
160+
*/
161+
void swap(int[] A, int ind1, int ind2) {
162+
int temp = A[ind1];
163+
A[ind1] = A[ind2];
164+
A[ind2] = temp;
165+
}
166+
167+
/**
168+
* Prints the elements of the heap array.
169+
*/
170+
void printHeapArray() {
171+
for (int i = 0; i < load; i++)
172+
System.out.print(hArray[i] + " ");
173+
System.out.println();
174+
}
175+
176+
}

LAB8_Heap/src/TestHeap.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class TestHeap {
2+
public static void main(String[] args) {
3+
Heap H = new Heap(); // create a heap with a max size 100 (default size)
4+
5+
System.out.println("Ex 1-2: Testing insert and reheapUp methods:");
6+
// The correct answer is 99 81 90 54 72 45 36 0 27 18 63 9
7+
System.out.println("Your HeapArray contains ");
8+
for (int i = 0; i < 100; i = i + 9) {
9+
H.insert(i);
10+
}
11+
H.printHeapArray();
12+
13+
// Uncomment the following code to test Ex3-4
14+
// The correct answer is 90 81 45 54 72 9 36 0 27 18 63
15+
System.out.println("\nEx 3-4: Testing deleteRoot and reheapDown methods:");
16+
H.deleteRoot();
17+
System.out.println("Your HeapArray contains ");
18+
H.printHeapArray();
19+
20+
// Uncomment the following code to test Ex5
21+
// The correct answer is 90 81 72 63 54 45 36 27 18 9 0
22+
System.out.println("\nEx 5: Testing HeapSort");
23+
System.out.println("Your sequence from HeapSort is ");
24+
H.makeHeapSort();
25+
}
26+
27+
}

0 commit comments

Comments
 (0)