자바 최대 더미 구현 (배열 방식)
1979 단어 알고리즘
package heap;
//
public class MaxHeap {
private int[] a; //
private int size; //
public MaxHeap(int initCapacity) {
a=new int[initCapacity+1]; // 0 " ", 1
a[0]=Integer.MAX_VALUE; //
size=0;
}
//
public boolean isEmpty() {
return size==0;
}
//
public boolean isFull() {
return size==a.length-1;
}
//
public boolean insert(int data) {
// ,
if(isFull()) {
System.out.println(" , ");
return false;
}
a[++size]=data;
for(int i=size;a[i]>a[i/2];i/=2) {
int t=a[i];
a[i]=a[i/2]; //
a[i/2]=t;
}
return true;
}
// ,
public int deleteMax() {
//
if(isEmpty()) {
System.out.println(" , ");
return -1;
}
//
int maxValue=a[1];
int tmp=a[size--];
// ,
int parent=1,child=2;
for(;parent*2<=size;parent=child) {
child=parent*2; //
// ,
if(childa[child]) {
child++; //child
}
// ,
if(tmp>a[child]) {
break;
}else {
int t=a[parent];
a[parent]=a[child];
a[child]=t;
}
}
// parent
a[parent]=tmp;
return maxValue;
}
//
public void print() {
if(isEmpty()) {
System.out.println(" ");
return;
}
for(int i=1;i<=size;i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
int[] b= {10,8,4,5,9,15,11,20};
MaxHeap heap=new MaxHeap(b.length);
for(int i=0;i
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
물체 검출의 평가 지표 IoU의 계산 방법Yolo나 SSD 등 물체 검출에서 평가 지표로 사용되는 IoU에 대해 조사했으므로 정리했습니다. IoU (Intersection over Union)는 두 영역이 얼마나 겹치는지를 나타내는 지표입니다. 두 영역의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.