4
\$\begingroup\$

I have implemented a min heap and looking for a simpler solution.

Is there anyone for suggesting a simpler solution for this?

Thanks.

import com.sun.org.apache.xalan.internal.xsltc.dom.MultiValuedNodeHeapIterator;
import java.util.Arrays;
public class MinHeap {
 private int capacity = 10;
 private int size = 0;
 int[] items = new int[capacity];
 private int getLeftChildIndex(int parentIndex) { return 2 * parentIndex + 1 ;}
 private int getRightChildIndex(int parentIndex) { return 2 * parentIndex + 2 ;}
 private int getParentIndex(int childIndex) { return (childIndex -1) / 2 ;}
 private boolean hasLeftChild(int index) { return getLeftChildIndex(index) < size; }
 private boolean hasRightChild(int index) { return getRightChildIndex(index) < size; }
 private boolean hasParent(int index) { return getParentIndex(index) >= 0; }
 private int leftChild(int index) { return items[getLeftChildIndex(index)]; }
 private int rightChild(int index) { return items[getRightChildIndex(index)]; }
 private int parent(int index) { return items[getParentIndex(index)]; }
 private void swap(int indexOne, int indexTwo){
 int temp = items[indexOne];
 items[indexOne] = items[indexTwo];
 items[indexTwo] = temp;
 }
 private void ensureExtraCapacity() {
 if (size == capacity) {
 items = Arrays.copyOf(items, capacity * 2);
 capacity *= 2;
 }
 }
 private int peek() {
 if (size == 0) throw new IllegalStateException();
 return items[0];
 }
 private int poll() {
 if (size == 0) throw new IllegalStateException();
 int item = items[0];
 items[0] = items[size - 1];
 size--;
 heapifyDown();
 return item;
 }
 public void add(int item) {
 ensureExtraCapacity();
 items[size] = item;
 size++;
 heapifyUp();
 }
 public void heapifyUp() {
 int index = size - 1;
 while ( hasParent(index) && parent(index) > items[index] ) {
 swap (getParentIndex(index),index);
 index = getParentIndex(index);
 }
 }
 public void heapifyDown() {
 int index = 0;
 while ( hasLeftChild(index) ) {
 int smallerChildIndex = getLeftChildIndex(index);
 if (hasRightChild(index) && rightChild(index) < leftChild(index)) {
 smallerChildIndex = getRightChildIndex(index);
 }
 if (items[index] < items[smallerChildIndex]) {
 break;
 } else {
 swap(index, smallerChildIndex);
 }
 index = smallerChildIndex;
 }
 }
}
asked Apr 27, 2020 at 18:25
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I have one suggestion for your code.

  1. Create a method to check if the current size is invalid, and reuse it (MinHeap#peek and MinHeap#poll methods).
private void assertCurrentSizeIsValid() {
 if (size == 0) {
 throw new IllegalStateException();
 }
}
answered Apr 28, 2020 at 17:42
\$\endgroup\$
1
  • \$\begingroup\$ Almost the same with the current code that's ok. \$\endgroup\$ Commented Apr 30, 2020 at 10:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.