Skip to content

Commit 28338e5

Browse files
committed
Fix
1 parent a73e373 commit 28338e5

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

src/main/java/com/thealgorithms/datastructures/heaps/HeapElement.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public String toString() {
111111
}
112112

113113
/**
114-
* @param otherHeapElement
114+
* @param o : an object to compare with the current element
115115
* @return true if the keys on both elements are identical and the
116116
* additional info objects are identical.
117117
*/
@@ -134,4 +134,8 @@ public int hashCode() {
134134
result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
135135
return result;
136136
}
137+
138+
public String getValue() {
139+
return additionalInfo.toString();
140+
}
137141
}

src/main/java/com/thealgorithms/datastructures/heaps/MaxHeap.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,54 @@ public MaxHeap(List<HeapElement> listElements) {
4545
}
4646

4747
maxHeap = new ArrayList<>();
48+
49+
// Safe initialization: directly add elements first
4850
for (HeapElement heapElement : listElements) {
4951
if (heapElement != null) {
50-
insertElement(heapElement);
52+
maxHeap.add(heapElement);
5153
} else {
5254
System.out.println("Null element. Not added to heap");
5355
}
5456
}
57+
58+
// Then heapify the array bottom-up
59+
for (int i = maxHeap.size() / 2; i >= 0; i--) {
60+
heapifyDown(i + 1); // +1 because heapifyDown expects 1-based index
61+
}
62+
5563
if (maxHeap.isEmpty()) {
5664
System.out.println("No element has been added, empty heap.");
5765
}
5866
}
5967

68+
/**
69+
* Maintains heap properties by moving an element down the heap.
70+
* Similar to toggleDown but used specifically during initialization.
71+
*
72+
* @param elementIndex 1-based index of the element to heapify
73+
*/
74+
private void heapifyDown(int elementIndex) {
75+
int largest = elementIndex - 1;
76+
int leftChild = 2 * elementIndex - 1;
77+
int rightChild = 2 * elementIndex;
78+
79+
if (leftChild < maxHeap.size() && maxHeap.get(leftChild).getKey() > maxHeap.get(largest).getKey()) {
80+
largest = leftChild;
81+
}
82+
83+
if (rightChild < maxHeap.size() && maxHeap.get(rightChild).getKey() > maxHeap.get(largest).getKey()) {
84+
largest = rightChild;
85+
}
86+
87+
if (largest != elementIndex - 1) {
88+
HeapElement swap = maxHeap.get(elementIndex - 1);
89+
maxHeap.set(elementIndex - 1, maxHeap.get(largest));
90+
maxHeap.set(largest, swap);
91+
92+
heapifyDown(largest + 1);
93+
}
94+
}
95+
6096
/**
6197
* Retrieves the element at the specified index without removing it.
6298
* Note: The index is 1-based for consistency with heap operations.
@@ -147,7 +183,7 @@ private HeapElement extractMax() throws EmptyHeapException {
147183
if (maxHeap.isEmpty()) {
148184
throw new EmptyHeapException("Cannot extract from empty heap");
149185
}
150-
HeapElement result = maxHeap.get(0);
186+
HeapElement result = maxHeap.getFirst();
151187
deleteElement(1);
152188
return result;
153189
}
@@ -177,8 +213,8 @@ public void deleteElement(int elementIndex) throws EmptyHeapException {
177213
}
178214

179215
// Replace with last element and remove last position
180-
maxHeap.set(elementIndex - 1, maxHeap.get(maxHeap.size() - 1));
181-
maxHeap.remove(maxHeap.size() - 1);
216+
maxHeap.set(elementIndex - 1, maxHeap.getLast());
217+
maxHeap.removeLast();
182218

183219
// No need to toggle if we just removed the last element
184220
if (!maxHeap.isEmpty() && elementIndex <= maxHeap.size()) {

src/test/java/com/thealgorithms/datastructures/heaps/MaxHeapTest.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.thealgorithms.datastructures.heaps;
22

3-
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.BeforeEach;
5-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
67

7-
import java.util.Arrays;
88
import java.util.ArrayList;
9+
import java.util.Arrays;
910
import java.util.List;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
1014

1115
/**
1216
* Unit tests for MaxHeap implementation
@@ -18,13 +22,7 @@ class MaxHeapTest {
1822
@BeforeEach
1923
void setUp() {
2024
// Create a fresh heap for each test
21-
List<HeapElement> elements = Arrays.asList(
22-
new HeapElement(5.0, "Five"),
23-
new HeapElement(2.0, "Two"),
24-
new HeapElement(8.0, "Eight"),
25-
new HeapElement(1.0, "One"),
26-
new HeapElement(9.0, "Nine")
27-
);
25+
List<HeapElement> elements = Arrays.asList(new HeapElement(5.0, "Five"), new HeapElement(2.0, "Two"), new HeapElement(8.0, "Eight"), new HeapElement(1.0, "One"), new HeapElement(9.0, "Nine"));
2826
heap = new MaxHeap(elements);
2927
}
3028

@@ -41,11 +39,7 @@ void testConstructorWithEmptyList() {
4139

4240
@Test
4341
void testConstructorWithNullElements() {
44-
List<HeapElement> elements = Arrays.asList(
45-
new HeapElement(1.0, "One"),
46-
null,
47-
new HeapElement(2.0, "Two")
48-
);
42+
List<HeapElement> elements = Arrays.asList(new HeapElement(1.0, "One"), null, new HeapElement(2.0, "Two"));
4943
MaxHeap heap = new MaxHeap(elements);
5044
assertEquals(2, heap.size());
5145
}
@@ -76,7 +70,7 @@ void testGetElementAtInvalidIndex() {
7670
}
7771

7872
@Test
79-
void testDeleteElement() {
73+
void testDeleteElement() throws EmptyHeapException {
8074
heap.deleteElement(1);
8175
assertEquals(8.0, heap.getElement(1).getKey());
8276
assertEquals(4, heap.size());
@@ -140,7 +134,7 @@ void testSizeAndEmpty() {
140134
try {
141135
heap.getElement();
142136
} catch (EmptyHeapException e) {
143-
fail("Should not throw EmptyHeapException while heap is not empty");
137+
Assertions.fail("Should not throw EmptyHeapException while heap is not empty");
144138
}
145139
}
146140

0 commit comments

Comments
 (0)