在 java 中使用数组实现最小堆
Min Heap implementation using Arrays in java
我正在尝试使用数组实现最小堆。我面临的问题是,当我从技术上轮询一个元素时,它应该 return 堆中的最小元素。
但是使用我写的代码并没有给出最小元素
输出:
已插入堆:1 3 2 6 4 5
轮询值:1
轮询后的新堆:3 5 2 6 4
轮询值:3
轮询后的新堆:4 5 2 6
轮询值:4
轮询后的新堆:5 6 2
需要的输出:
轮询时它应该给出最小值
主要class代码:
public class HeapApp {
public static void main(String[] args) {
Heap hg = new Heap(6);
hg.insert(6);
hg.insert(5);
hg.insert(4);
hg.insert(3);
hg.insert(2);
hg.insert(1);
System.out.print("inserted heap: ");
hg.print();
System.out.println();
System.out.println("Polledvlaue : "+hg.poll());
System.out.print("New Heap after poll : ");hg.print();
System.out.println();
System.out.println("Polled value :"+hg.poll());
System.out.print("New Heap after poll : "); hg.print();
System.out.println();
System.out.println("Polled value :"+hg.poll());
System.out.print("New Heap after poll : ");hg.print();
}
}
堆Class:
public class Heap {
private int heapSize;
private int arr[];
Heap(int capacity){
heapSize = 0;
arr = new int[capacity];
}
public boolean isFull() {
return heapSize == arr.length;
}
public boolean isEmpty() {
return heapSize == 0;
}
public void insert(int element) {
if(isFull()) {
throw new RuntimeException("Heap is full");
}
arr[heapSize] = element;
heapSize++;
heapifyUp();
}
private void heapifyUp() {
int index = heapSize-1;
while(index > 0 ) {
if( arr[(index-1)/2] > arr[index]) {
int temp = arr[index];
arr[index] = arr[(index-1)/2];
arr[(index-1)/2] = temp;
index = (index-1)/2;
}else {
break;
}
}
}
public int poll() {
if(isEmpty())
throw new RuntimeException("Heap is empty");
int ans = arr[0];
arr[0] = arr[heapSize-1];
heapSize--;
heapifyDown();
return ans;
}
private void heapifyDown() {
int index = 0;
int left = 2*index+1;
while(left < heapSize) {
int min = left ; int right = ++left;
if(right < heapSize) {
if(arr[left] > arr[right]) {
min++;
}
}
if(arr[index] > arr[min]) {
int temp = arr[min];
arr[min] = arr[index];
arr[index] = temp;
}
index = min;
left = 2*index+1;
}
}
public void print() {
for(int i = 0 ; i < heapSize ; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
private void heapifyDown() {
int index = 0;
int left = 2*index+1;
while(left < heapSize) {
int min = left ; int right = ++left; // <------ Here's your bug.
if(right < heapSize) {
if(arr[left] > arr[right]) {
min++;
}
}
if(arr[index] > arr[min]) {
int temp = arr[min];
arr[min] = arr[index];
arr[index] = temp;
}
index = min;
left = 2*index+1;
}
}
++left 将左右递增到相同的值。
将其更改为 int right = left + 1;
我正在尝试使用数组实现最小堆。我面临的问题是,当我从技术上轮询一个元素时,它应该 return 堆中的最小元素。
但是使用我写的代码并没有给出最小元素
输出:
已插入堆:1 3 2 6 4 5
轮询值:1
轮询后的新堆:3 5 2 6 4
轮询值:3
轮询后的新堆:4 5 2 6
轮询值:4
轮询后的新堆:5 6 2
需要的输出:
轮询时它应该给出最小值
主要class代码:
public class HeapApp {
public static void main(String[] args) {
Heap hg = new Heap(6);
hg.insert(6);
hg.insert(5);
hg.insert(4);
hg.insert(3);
hg.insert(2);
hg.insert(1);
System.out.print("inserted heap: ");
hg.print();
System.out.println();
System.out.println("Polledvlaue : "+hg.poll());
System.out.print("New Heap after poll : ");hg.print();
System.out.println();
System.out.println("Polled value :"+hg.poll());
System.out.print("New Heap after poll : "); hg.print();
System.out.println();
System.out.println("Polled value :"+hg.poll());
System.out.print("New Heap after poll : ");hg.print();
}
}
堆Class:
public class Heap {
private int heapSize;
private int arr[];
Heap(int capacity){
heapSize = 0;
arr = new int[capacity];
}
public boolean isFull() {
return heapSize == arr.length;
}
public boolean isEmpty() {
return heapSize == 0;
}
public void insert(int element) {
if(isFull()) {
throw new RuntimeException("Heap is full");
}
arr[heapSize] = element;
heapSize++;
heapifyUp();
}
private void heapifyUp() {
int index = heapSize-1;
while(index > 0 ) {
if( arr[(index-1)/2] > arr[index]) {
int temp = arr[index];
arr[index] = arr[(index-1)/2];
arr[(index-1)/2] = temp;
index = (index-1)/2;
}else {
break;
}
}
}
public int poll() {
if(isEmpty())
throw new RuntimeException("Heap is empty");
int ans = arr[0];
arr[0] = arr[heapSize-1];
heapSize--;
heapifyDown();
return ans;
}
private void heapifyDown() {
int index = 0;
int left = 2*index+1;
while(left < heapSize) {
int min = left ; int right = ++left;
if(right < heapSize) {
if(arr[left] > arr[right]) {
min++;
}
}
if(arr[index] > arr[min]) {
int temp = arr[min];
arr[min] = arr[index];
arr[index] = temp;
}
index = min;
left = 2*index+1;
}
}
public void print() {
for(int i = 0 ; i < heapSize ; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
private void heapifyDown() {
int index = 0;
int left = 2*index+1;
while(left < heapSize) {
int min = left ; int right = ++left; // <------ Here's your bug.
if(right < heapSize) {
if(arr[left] > arr[right]) {
min++;
}
}
if(arr[index] > arr[min]) {
int temp = arr[min];
arr[min] = arr[index];
arr[index] = temp;
}
index = min;
left = 2*index+1;
}
}
++left 将左右递增到相同的值。
将其更改为 int right = left + 1;