Java 将项插入最大堆
我不知道如何将一个项目插入到我的最大堆中,然后向上滴入,以便最大堆属性保持不变。 如果heapArray已满,因此无法插入项目,则引发异常 我没有为这个程序使用JCF类或优先级队列。 我还引入了deleteMax方法,该方法删除堆的最大值,并恢复堆,使max heap属性保持不变Java 将项插入最大堆,java,insert,heapsort,max-heap,Java,Insert,Heapsort,Max Heap,我不知道如何将一个项目插入到我的最大堆中,然后向上滴入,以便最大堆属性保持不变。 如果heapArray已满,因此无法插入项目,则引发异常 我没有为这个程序使用JCF类或优先级队列。 我还引入了deleteMax方法,该方法删除堆的最大值,并恢复堆,使max heap属性保持不变 public class MaxIntHeap { //my global var's private int[] heapArray; // default size of 20 in constructor pr
public class MaxIntHeap {
//my global var's
private int[] heapArray; // default size of 20 in constructor
private int lastOne; //equal to size of the array(heap)
private int size;
private int k;
public void heapInsert(int v) throws HeapException {
if(lastOne == heapArray.length - 1 ){
throw new HeapException("The value " + v + " cannot be inserted because the heap is FULL!");
}
else{ //This is where i am lost as to how i should insert
lastOne++; //size of lastOne is increased.
}
这是我的removeMax方法
public int removeMax ()throws HeapException {
if(size == 0){
throw new HeapException("The Heap is empty, therefore the Max value of the heap cannot be
removed.");
}
else{
int max = heapArray[0];
heapArray[0] = heapArray[lastOne];
lastOne--;
k = 0;
while(k > 0){
int j = k / 2;
if(heapArray[k] < heapArray[j]){
swap(heapArray[k], heapArray[j]); //swap method...
k = j;
}
else
break;
}
return max;
}
}
public int removeMax()抛出HeapException{
如果(大小==0){
抛出新的HeapException(“堆是空的,因此不能指定堆的最大值
删除。”);
}
否则{
int max=heapArray[0];
heapArray[0]=heapArray[lastOne];
最后一个--;
k=0;
而(k>0){
int j=k/2;
if(heapArray[k]
任何帮助都将不胜感激。谢谢大家! 你的课堂设计看起来不错。 在堆中:
leftChild = 2*parent +1
rightChild = 2*parent + 2
parent = (childindex-1)/2
对于maxheap
public class MaxHeap {
public int[] myHeap = new int[20];
public int begin = 0;
public int current = 0;
public int getParent(int index){
return (index - 1)/2;
}
public int getLeftChild(int index){
return 2*index+1;
}
public int getRighChild(int index){
return 2*index+2;
}
public void insert(int data) {
myHeap[current] = data;
int i = current;
int tmp;
int parent;
while(i > 0){
parent = getParent(i);
System.out.println(" I value"+i+" parent"+parent+" data"+data);
if(myHeap[parent] < myHeap[i]){
tmp = myHeap[parent];
myHeap[parent] = myHeap[i];
myHeap[i] = tmp;
} else{
break;
}
i = parent;
}
current++;
}
公共类MaxHeap{
public int[]myHeap=new int[20];
公共int begin=0;
公共int电流=0;
公共整数getParent(整数索引){
收益率(指数-1)/2;
}
公共整型getLeftChild(整型索引){
返回2*index+1;
}
公共整数getRighChild(整数索引){
返回2*index+2;
}
公共空白插入(整型数据){
myHeap[当前]=数据;
int i=电流;
int tmp;
int父代;
而(i>0){
父项=获取父项(i);
系统输出打印项次(“I值”+I+“父项”+父项+“数据”+数据);
if(myHeap[parent]
}
public class MaxHeapTest {
@Test
public void test() {
MaxHeap myHeap = new MaxHeap();
myHeap.insert(40);
myHeap.insert(20);
myHeap.insert(10);
myHeap.insert(25);
myHeap.insert(30);
myHeap.insert(100);
for(int i = 0; i < myHeap.current;i++){
System.out.println(" "+myHeap.myHeap[i]);
}
}
}
公共类MaxHeapTest{
@试验
公开无效测试(){
MaxHeap myHeap=新的MaxHeap();
myHeap.insert(40);
myHeap.insert(20);
myHeap.insert(10);
myHeap.insert(25);
myHeap.insert(30);
myHeap.insert(100);
for(int i=0;i
要解决上述问题,current不等于0,而是等于堆的大小(带值)
intgetparent(intindex){
收益率(指数-1)/2;
}
void insertHeap(数组选项卡){
int current=tab.size()-1;
int i=电流;
int tmp;
int父代;
而(i>0){
父项=获取父项(i);
如果(tab[parent]