Skip to content

Commit 8a18d45

Browse files
authored
Enhance docs, add tests in HeapElement (#5981)
1 parent 08bd1ff commit 8a18d45

File tree

3 files changed

+126
-37
lines changed

3 files changed

+126
-37
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@
865865
* heaps
866866
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
867867
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
868+
* [HeapElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java)
868869
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
869870
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
870871
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package com.thealgorithms.datastructures.heaps;
22

33
/**
4-
* Class for heap elements.<br>
4+
* Class representing an element in a heap.
55
*
66
* <p>
7-
* A heap element contains two attributes: a key which will be used to build the
8-
* tree (int or double, either primitive type or object) and any kind of
9-
* IMMUTABLE object the user sees fit to carry any information he/she likes. Be
10-
* aware that the use of a mutable object might jeopardize the integrity of this
11-
* information.
7+
* A heap element contains two attributes: a key used for ordering in the heap
8+
* (which can be of type int or double, either as primitive types or as wrapper objects)
9+
* and an additional immutable object that can store any supplementary information the user desires.
10+
* Note that using mutable objects may compromise the integrity of this information.
11+
* </p>
12+
*
13+
* <p>
14+
* The key attribute is used to determine the order of elements in the heap,
15+
* while the additionalInfo attribute can carry user-defined data associated with the key.
16+
* </p>
17+
*
18+
* <p>
19+
* This class provides multiple constructors to accommodate various key types and includes
20+
* methods to retrieve the key and additional information.
21+
* </p>
1222
*
1323
* @author Nicolas Renard
1424
*/
@@ -19,71 +29,83 @@ public class HeapElement {
1929

2030
// Constructors
2131
/**
22-
* @param key : a number of primitive type 'double'
23-
* @param info : any kind of IMMUTABLE object. May be null, since the
24-
* purpose is only to carry additional information of use for the user
32+
* Creates a HeapElement with the specified key and additional information.
33+
*
34+
* @param key the key of the element (primitive type double)
35+
* @param info any immutable object containing additional information, may be null
2536
*/
2637
public HeapElement(double key, Object info) {
2738
this.key = key;
2839
this.additionalInfo = info;
2940
}
3041

3142
/**
32-
* @param key : a number of primitive type 'int'
33-
* @param info : any kind of IMMUTABLE object. May be null, since the
34-
* purpose is only to carry additional information of use for the user
43+
* Creates a HeapElement with the specified key and additional information.
44+
*
45+
* @param key the key of the element (primitive type int)
46+
* @param info any immutable object containing additional information, may be null
3547
*/
3648
public HeapElement(int key, Object info) {
3749
this.key = key;
3850
this.additionalInfo = info;
3951
}
4052

4153
/**
42-
* @param key : a number of object type 'Integer'
43-
* @param info : any kind of IMMUTABLE object. May be null, since the
44-
* purpose is only to carry additional information of use for the user
54+
* Creates a HeapElement with the specified key and additional information.
55+
*
56+
* @param key the key of the element (object type Integer)
57+
* @param info any immutable object containing additional information, may be null
4558
*/
4659
public HeapElement(Integer key, Object info) {
4760
this.key = key;
4861
this.additionalInfo = info;
4962
}
5063

5164
/**
52-
* @param key : a number of object type 'Double'
53-
* @param info : any kind of IMMUTABLE object. May be null, since the
54-
* purpose is only to carry additional information of use for the user
65+
* Creates a HeapElement with the specified key and additional information.
66+
*
67+
* @param key the key of the element (object type Double)
68+
* @param info any immutable object containing additional information, may be null
5569
*/
5670
public HeapElement(Double key, Object info) {
5771
this.key = key;
5872
this.additionalInfo = info;
5973
}
6074

6175
/**
62-
* @param key : a number of primitive type 'double'
76+
* Creates a HeapElement with the specified key.
77+
*
78+
* @param key the key of the element (primitive type double)
6379
*/
6480
public HeapElement(double key) {
6581
this.key = key;
6682
this.additionalInfo = null;
6783
}
6884

6985
/**
70-
* @param key : a number of primitive type 'int'
86+
* Creates a HeapElement with the specified key.
87+
*
88+
* @param key the key of the element (primitive type int)
7189
*/
7290
public HeapElement(int key) {
7391
this.key = key;
7492
this.additionalInfo = null;
7593
}
7694

7795
/**
78-
* @param key : a number of object type 'Integer'
96+
* Creates a HeapElement with the specified key.
97+
*
98+
* @param key the key of the element (object type Integer)
7999
*/
80100
public HeapElement(Integer key) {
81101
this.key = key;
82102
this.additionalInfo = null;
83103
}
84104

85105
/**
86-
* @param key : a number of object type 'Double'
106+
* Creates a HeapElement with the specified key.
107+
*
108+
* @param key the key of the element (object type Double)
87109
*/
88110
public HeapElement(Double key) {
89111
this.key = key;
@@ -92,46 +114,57 @@ public HeapElement(Double key) {
92114

93115
// Getters
94116
/**
95-
* @return the object containing the additional info provided by the user.
117+
* Returns the object containing the additional information provided by the user.
118+
*
119+
* @return the additional information
96120
*/
97121
public Object getInfo() {
98122
return additionalInfo;
99123
}
100124

101125
/**
102-
* @return the key value of the element
126+
* Returns the key value of the element.
127+
*
128+
* @return the key of the element
103129
*/
104130
public double getKey() {
105131
return key;
106132
}
107133

108134
// Overridden object methods
135+
/**
136+
* Returns a string representation of the heap element.
137+
*
138+
* @return a string describing the key and additional information
139+
*/
140+
@Override
109141
public String toString() {
110-
return "Key: " + key + " - " + additionalInfo.toString();
142+
return "Key: " + key + " - " + (additionalInfo != null ? additionalInfo.toString() : "No additional info");
111143
}
112144

113145
/**
114-
* @param otherHeapElement
115-
* @return true if the keys on both elements are identical and the
116-
* additional info objects are identical.
146+
* Compares this heap element to another object for equality.
147+
*
148+
* @param o the object to compare with
149+
* @return true if the keys and additional information are identical, false otherwise
117150
*/
118151
@Override
119152
public boolean equals(Object o) {
120-
if (o != null) {
121-
if (!(o instanceof HeapElement)) {
122-
return false;
123-
}
124-
HeapElement otherHeapElement = (HeapElement) o;
125-
return ((this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo)));
153+
if (o instanceof HeapElement otherHeapElement) {
154+
return this.key == otherHeapElement.key && (this.additionalInfo != null ? this.additionalInfo.equals(otherHeapElement.additionalInfo) : otherHeapElement.additionalInfo == null);
126155
}
127156
return false;
128157
}
129158

159+
/**
160+
* Returns a hash code value for the heap element.
161+
*
162+
* @return a hash code value for this heap element
163+
*/
130164
@Override
131165
public int hashCode() {
132-
int result = 0;
133-
result = 31 * result + (int) key;
134-
result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
166+
int result = 31 * (int) key;
167+
result += (additionalInfo != null) ? additionalInfo.hashCode() : 0;
135168
return result;
136169
}
137170
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.datastructures.heaps;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
class HeapElementTest {
10+
11+
@Test
12+
void testConstructorAndGetters() {
13+
HeapElement element = new HeapElement(5.0, "Info");
14+
assertEquals(5.0, element.getKey());
15+
assertEquals("Info", element.getInfo());
16+
}
17+
18+
@Test
19+
void testConstructorWithNullInfo() {
20+
HeapElement element = new HeapElement(10);
21+
assertEquals(10, element.getKey());
22+
assertNull(element.getInfo());
23+
}
24+
25+
@Test
26+
void testToString() {
27+
HeapElement element = new HeapElement(7.5, "TestInfo");
28+
assertEquals("Key: 7.5 - TestInfo", element.toString());
29+
30+
HeapElement elementWithoutInfo = new HeapElement(3);
31+
assertEquals("Key: 3.0 - No additional info", elementWithoutInfo.toString());
32+
}
33+
34+
@Test
35+
void testEquals() {
36+
HeapElement element1 = new HeapElement(2.5, "Data");
37+
HeapElement element2 = new HeapElement(2.5, "Data");
38+
HeapElement element3 = new HeapElement(3.0, "DifferentData");
39+
40+
assertEquals(element1, element2); // Same key and info
41+
assertNotEquals(element1, element3); // Different key
42+
assertNotEquals(null, element1); // Check for null
43+
assertNotEquals("String", element1); // Check for different type
44+
}
45+
46+
@Test
47+
void testHashCode() {
48+
HeapElement element1 = new HeapElement(4, "HashMe");
49+
HeapElement element2 = new HeapElement(4, "HashMe");
50+
HeapElement element3 = new HeapElement(4, "DifferentHash");
51+
52+
assertEquals(element1.hashCode(), element2.hashCode()); // Same key and info
53+
assertNotEquals(element1.hashCode(), element3.hashCode()); // Different info
54+
}
55+
}

0 commit comments

Comments
 (0)