我不确定如何将一个项目插入到我的最大堆中,然后涓涓细流,以便最大堆属性保持不变。 如果heapArray已满,则抛出异常,因此无法插入项目。
我没有使用JCF类或此程序的优先级队列。 我还抛出了我的deleteMax方法,该方法删除堆的最大值并恢复堆,以便最大堆属性保持不变。
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;
}
}
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
你的班级设计看起来不错。 在堆中:
leftChild = 2*parent +1
rightChild = 2*parent + 2
parent = (childindex-1)/2
对于maxheap,
重复直到你到达根目录。
MaxHeapImpl:
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++;
}
}
识别TestClass:
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]);
}
}
}
答案 1 :(得分:0)
为解决以上问题,当前值不等于0,而是堆的大小(带有值)。
from symfit import parameters, variables, sin, cos, Fit
import numpy as np
import matplotlib.pyplot as plt
import math as m
from random import random
def f(x,a,b,omega):
output = a[0]/2
for k in range(1,len(a)-1):
output = output + a[k]*m.cos(k*omega*x) + b[k]*m.sin(k*omega*x)
return output
def monteCarlo(min,max,a,b,omega):
x = min + random()*(max-min)
return (x,f(x,a,b,omega))
xdata=[]
ydata=[]
#for step function between -3 and 3
#a=[5.000000e-01,1.106075e-10,2.364473e-11,1.697959e-10,2.822410e-11,3.860925e-11,-1.522386e-10]
#b=[0,6.267589e-01,2.020945e-02,1.846406e-01,3.623074e-02,8.726419e-02,4.518721e-02]
#omega=8.615115e-01
#for our spectrum with n=6
a = [-1.755069e+02,-1.097051e+02,1.571135e+02,6.491389e+01,1.220463e+02,1.778165e+02,4.564577e+01]
b = [0,-5.523502e+01,1.517397e+02,1.325198e+02,-1.015198e+02,-3.255781e+01,2.495038e+01]
omega = -5.353751e-03
#for our spectrum with n=10
#a = [-1.206104e+03,1.799223e+03,-8.465307e+02,6.798590e+02,-9.287399e+02,7.388156e+02,-2.099387e+02,-9.838123e+01,\
# 9.821148e+01,-2.827002e+01,2.393892e+00]
#b = [0,-6.084419e+02,9.311030e+02,-8.877734e+02,8.810145e+02,-1.288586e+03,1.734395e+03,-1.583775e+03,9.115344e+02,\
# -3.045882e+02,4.586827e+01]
#omega = -1.370387e-02
#a = [0.983, 0.292, 0.110, 0.006, -0.022, -0.018, 0.006]
#b = [0.000, 0.246, 0.002, -0.021, 0.017, 0.046, 0.019]
#omega = 0.045
for n in range(0,1000):
coor = monteCarlo(375,525,a,b,omega)
xdata.append(coor[0])
ydata.append(coor[1])
plt.plot(xdata, ydata, 'bo')