Skip to content

refactor: Enhance docs, add tests in HeapElement #5981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@
* heaps
* [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)
* [HeapElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/HeapElementTest.java)
* [LeftistHeapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/LeftistHeapTest.java)
* [MinPriorityQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/heaps/MinPriorityQueueTest.java)
* lists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.thealgorithms.datastructures.heaps;

/**
* Class for heap elements.<br>
* Class representing an element in a heap.
*
* <p>
* A heap element contains two attributes: a key which will be used to build the
* tree (int or double, either primitive type or object) and any kind of
* IMMUTABLE object the user sees fit to carry any information he/she likes. Be
* aware that the use of a mutable object might jeopardize the integrity of this
* information.
* A heap element contains two attributes: a key used for ordering in the heap
* (which can be of type int or double, either as primitive types or as wrapper objects)
* and an additional immutable object that can store any supplementary information the user desires.
* Note that using mutable objects may compromise the integrity of this 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
*/
Expand All @@ -19,71 +29,83 @@ public class HeapElement {

// Constructors
/**
* @param key : a number of primitive type 'double'
* @param info : any kind of IMMUTABLE object. May be null, since the
* purpose is only to carry additional information of use for the user
* Creates a HeapElement with the specified key and additional information.
*
* @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) {
this.key = key;
this.additionalInfo = info;
}

/**
* @param key : a number of primitive type 'int'
* @param info : any kind of IMMUTABLE object. May be null, since the
* purpose is only to carry additional information of use for the user
* Creates a HeapElement with the specified key and additional information.
*
* @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) {
this.key = key;
this.additionalInfo = info;
}

/**
* @param key : a number of object type 'Integer'
* @param info : any kind of IMMUTABLE object. May be null, since the
* purpose is only to carry additional information of use for the user
* Creates a HeapElement with the specified key and additional information.
*
* @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) {
this.key = key;
this.additionalInfo = info;
}

/**
* @param key : a number of object type 'Double'
* @param info : any kind of IMMUTABLE object. May be null, since the
* purpose is only to carry additional information of use for the user
* Creates a HeapElement with the specified key and additional information.
*
* @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) {
this.key = key;
this.additionalInfo = info;
}

/**
* @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) {
this.key = key;
this.additionalInfo = null;
}

/**
* @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) {
this.key = key;
this.additionalInfo = null;
}

/**
* @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) {
this.key = key;
this.additionalInfo = null;
}

/**
* @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) {
this.key = key;
Expand All @@ -92,46 +114,57 @@ public HeapElement(Double key) {

// 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() {
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() {
return key;
}

// Overridden object methods
/**
* Returns a string representation of the heap element.
*
* @return a string describing the key and additional information
*/
@Override
public String toString() {
return "Key: " + key + " - " + additionalInfo.toString();
return "Key: " + key + " - " + (additionalInfo != null ? additionalInfo.toString() : "No additional info");
}

/**
* @param otherHeapElement
* @return true if the keys on both elements are identical and the
* additional info objects are identical.
* Compares this heap element to another object for equality.
*
* @param o the object to compare with
* @return true if the keys and additional information are identical, false otherwise
*/
@Override
public boolean equals(Object o) {
if (o != null) {
if (!(o instanceof HeapElement)) {
return false;
}
HeapElement otherHeapElement = (HeapElement) o;
return ((this.key == otherHeapElement.key) && (this.additionalInfo.equals(otherHeapElement.additionalInfo)));
if (o instanceof HeapElement otherHeapElement) {
return this.key == otherHeapElement.key && (this.additionalInfo != null ? this.additionalInfo.equals(otherHeapElement.additionalInfo) : otherHeapElement.additionalInfo == null);
}
return false;
}

/**
* Returns a hash code value for the heap element.
*
* @return a hash code value for this heap element
*/
@Override
public int hashCode() {
int result = 0;
result = 31 * result + (int) key;
result = 31 * result + (additionalInfo != null ? additionalInfo.hashCode() : 0);
int result = 31 * (int) key;
result += (additionalInfo != null) ? additionalInfo.hashCode() : 0;
return result;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}