mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Enhance docs, add tests in HeapElement (#5981)
This commit is contained in:
@ -865,6 +865,7 @@
|
|||||||
* heaps
|
* heaps
|
||||||
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
|
* [FibonacciHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/FibonacciHeapTest.java)
|
||||||
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
|
* [GenericHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/GenericHeapTest.java)
|
||||||
|
* [HeapElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java)
|
||||||
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
|
* [KthElementFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/KthElementFinderTest.java)
|
||||||
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
|
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
|
||||||
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
|
* [MedianFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MedianFinderTest.java)
|
||||||
|
@ -1,14 +1,24 @@
|
|||||||
package com.thealgorithms.datastructures.heaps;
|
package com.thealgorithms.datastructures.heaps;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class for heap elements.<br>
|
* Class representing an element in a heap.
|
||||||
*
|
*
|
||||||
* <p>
|
* <p>
|
||||||
* A heap element contains two attributes: a key which will be used to build the
|
* A heap element contains two attributes: a key used for ordering in the heap
|
||||||
* tree (int or double, either primitive type or object) and any kind of
|
* (which can be of type int or double, either as primitive types or as wrapper objects)
|
||||||
* IMMUTABLE object the user sees fit to carry any information he/she likes. Be
|
* and an additional immutable object that can store any supplementary information the user desires.
|
||||||
* aware that the use of a mutable object might jeopardize the integrity of this
|
* Note that using mutable objects may compromise the integrity of this information.
|
||||||
* information.
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* The key attribute is used to determine the order of elements in the heap,
|
||||||
|
* while the additionalInfo attribute can carry user-defined data associated with the key.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This class provides multiple constructors to accommodate various key types and includes
|
||||||
|
* methods to retrieve the key and additional information.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* @author Nicolas Renard
|
* @author Nicolas Renard
|
||||||
*/
|
*/
|
||||||
@ -19,9 +29,10 @@ public class HeapElement {
|
|||||||
|
|
||||||
// Constructors
|
// Constructors
|
||||||
/**
|
/**
|
||||||
* @param key : a number of primitive type 'double'
|
* Creates a HeapElement with the specified key and additional information.
|
||||||
* @param info : any kind of IMMUTABLE object. May be null, since the
|
*
|
||||||
* purpose is only to carry additional information of use for the user
|
* @param key the key of the element (primitive type double)
|
||||||
|
* @param info any immutable object containing additional information, may be null
|
||||||
*/
|
*/
|
||||||
public HeapElement(double key, Object info) {
|
public HeapElement(double key, Object info) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -29,9 +40,10 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of primitive type 'int'
|
* Creates a HeapElement with the specified key and additional information.
|
||||||
* @param info : any kind of IMMUTABLE object. May be null, since the
|
*
|
||||||
* purpose is only to carry additional information of use for the user
|
* @param key the key of the element (primitive type int)
|
||||||
|
* @param info any immutable object containing additional information, may be null
|
||||||
*/
|
*/
|
||||||
public HeapElement(int key, Object info) {
|
public HeapElement(int key, Object info) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -39,9 +51,10 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of object type 'Integer'
|
* Creates a HeapElement with the specified key and additional information.
|
||||||
* @param info : any kind of IMMUTABLE object. May be null, since the
|
*
|
||||||
* purpose is only to carry additional information of use for the user
|
* @param key the key of the element (object type Integer)
|
||||||
|
* @param info any immutable object containing additional information, may be null
|
||||||
*/
|
*/
|
||||||
public HeapElement(Integer key, Object info) {
|
public HeapElement(Integer key, Object info) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -49,9 +62,10 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of object type 'Double'
|
* Creates a HeapElement with the specified key and additional information.
|
||||||
* @param info : any kind of IMMUTABLE object. May be null, since the
|
*
|
||||||
* purpose is only to carry additional information of use for the user
|
* @param key the key of the element (object type Double)
|
||||||
|
* @param info any immutable object containing additional information, may be null
|
||||||
*/
|
*/
|
||||||
public HeapElement(Double key, Object info) {
|
public HeapElement(Double key, Object info) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -59,7 +73,9 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of primitive type 'double'
|
* Creates a HeapElement with the specified key.
|
||||||
|
*
|
||||||
|
* @param key the key of the element (primitive type double)
|
||||||
*/
|
*/
|
||||||
public HeapElement(double key) {
|
public HeapElement(double key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -67,7 +83,9 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of primitive type 'int'
|
* Creates a HeapElement with the specified key.
|
||||||
|
*
|
||||||
|
* @param key the key of the element (primitive type int)
|
||||||
*/
|
*/
|
||||||
public HeapElement(int key) {
|
public HeapElement(int key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -75,7 +93,9 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of object type 'Integer'
|
* Creates a HeapElement with the specified key.
|
||||||
|
*
|
||||||
|
* @param key the key of the element (object type Integer)
|
||||||
*/
|
*/
|
||||||
public HeapElement(Integer key) {
|
public HeapElement(Integer key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -83,7 +103,9 @@ public class HeapElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param key : a number of object type 'Double'
|
* Creates a HeapElement with the specified key.
|
||||||
|
*
|
||||||
|
* @param key the key of the element (object type Double)
|
||||||
*/
|
*/
|
||||||
public HeapElement(Double key) {
|
public HeapElement(Double key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
@ -92,46 +114,57 @@ public class HeapElement {
|
|||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
/**
|
/**
|
||||||
* @return the object containing the additional info provided by the user.
|
* Returns the object containing the additional information provided by the user.
|
||||||
|
*
|
||||||
|
* @return the additional information
|
||||||
*/
|
*/
|
||||||
public Object getInfo() {
|
public Object getInfo() {
|
||||||
return additionalInfo;
|
return additionalInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the key value of the element
|
* Returns the key value of the element.
|
||||||
|
*
|
||||||
|
* @return the key of the element
|
||||||
*/
|
*/
|
||||||
public double getKey() {
|
public double getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Overridden object methods
|
// Overridden object methods
|
||||||
|
/**
|
||||||
|
* Returns a string representation of the heap element.
|
||||||
|
*
|
||||||
|
* @return a string describing the key and additional information
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Key: " + key + " - " + additionalInfo.toString();
|
return "Key: " + key + " - " + (additionalInfo != null ? additionalInfo.toString() : "No additional info");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param otherHeapElement
|
* Compares this heap element to another object for equality.
|
||||||
* @return true if the keys on both elements are identical and the
|
*
|
||||||
* additional info objects are identical.
|
* @param o the object to compare with
|
||||||
|
* @return true if the keys and additional information are identical, false otherwise
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (o != null) {
|
if (o instanceof HeapElement otherHeapElement) {
|
||||||
if (!(o instanceof HeapElement)) {
|
return this.key == otherHeapElement.key && (this.additionalInfo != null ? this.additionalInfo.equals(otherHeapElement.additionalInfo) : otherHeapElement.additionalInfo == null);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
HeapElement otherHeapElement = (HeapElement) o;
|
|
||||||
return ((this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo)));
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a hash code value for the heap element.
|
||||||
|
*
|
||||||
|
* @return a hash code value for this heap element
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = 0;
|
int result = 31 * (int) key;
|
||||||
result = 31 * result + (int) key;
|
result += (additionalInfo != null) ? additionalInfo.hashCode() : 0;
|
||||||
result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.thealgorithms.datastructures.heaps;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class HeapElementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructorAndGetters() {
|
||||||
|
HeapElement element = new HeapElement(5.0, "Info");
|
||||||
|
assertEquals(5.0, element.getKey());
|
||||||
|
assertEquals("Info", element.getInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructorWithNullInfo() {
|
||||||
|
HeapElement element = new HeapElement(10);
|
||||||
|
assertEquals(10, element.getKey());
|
||||||
|
assertNull(element.getInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToString() {
|
||||||
|
HeapElement element = new HeapElement(7.5, "TestInfo");
|
||||||
|
assertEquals("Key: 7.5 - TestInfo", element.toString());
|
||||||
|
|
||||||
|
HeapElement elementWithoutInfo = new HeapElement(3);
|
||||||
|
assertEquals("Key: 3.0 - No additional info", elementWithoutInfo.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEquals() {
|
||||||
|
HeapElement element1 = new HeapElement(2.5, "Data");
|
||||||
|
HeapElement element2 = new HeapElement(2.5, "Data");
|
||||||
|
HeapElement element3 = new HeapElement(3.0, "DifferentData");
|
||||||
|
|
||||||
|
assertEquals(element1, element2); // Same key and info
|
||||||
|
assertNotEquals(element1, element3); // Different key
|
||||||
|
assertNotEquals(null, element1); // Check for null
|
||||||
|
assertNotEquals("String", element1); // Check for different type
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testHashCode() {
|
||||||
|
HeapElement element1 = new HeapElement(4, "HashMe");
|
||||||
|
HeapElement element2 = new HeapElement(4, "HashMe");
|
||||||
|
HeapElement element3 = new HeapElement(4, "DifferentHash");
|
||||||
|
|
||||||
|
assertEquals(element1.hashCode(), element2.hashCode()); // Same key and info
|
||||||
|
assertNotEquals(element1.hashCode(), element3.hashCode()); // Different info
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user