The Wayback Machine - https://web.archive.org/web/20220709022348/https://github.com/TheAlgorithms/Java/issues/3158
Skip to content
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

MinHeap and MaxHeap realisations are broken #3158

Open
Boiarshinov opened this issue Jun 21, 2022 · 2 comments
Open

MinHeap and MaxHeap realisations are broken #3158

Boiarshinov opened this issue Jun 21, 2022 · 2 comments

Comments

@Boiarshinov
Copy link
Contributor

@Boiarshinov Boiarshinov commented Jun 21, 2022

MinHeap and MaxHeap implementation are broken.
Instance of these heaps couldn't be instanciated with any not empty list of elements.

Here is a test code to reproduce:

@Test
void createFromList() {
    var elements = List.of(
            new HeapElement(5)
    );
    MaxHeap heap = new MaxHeap(elements);
    //similar with MinHeap
}

Stacktrace:

java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 1

	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at com.thealgorithms.datastructures.heaps.MaxHeap.getElementKey(MaxHeap.java:46)
	at com.thealgorithms.datastructures.heaps.MaxHeap.toggleUp(MaxHeap.java:59)
	at com.thealgorithms.datastructures.heaps.MaxHeap.insertElement(MaxHeap.java:97)
	at com.thealgorithms.datastructures.heaps.MaxHeap.<init>(MaxHeap.java:20)
@Boiarshinov
Copy link
Contributor Author

@Boiarshinov Boiarshinov commented Jun 21, 2022

This issue could be a good_first_issue for newcomers. I'm not pretend to solve it by myself 😄

@Jainam1401
Copy link

@Jainam1401 Jainam1401 commented Jul 8, 2022

@Boiarshinov Sir, I am new to the Open Source contribution, can you assign me the issue if no one is working on the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants