Java使用二进制堆错误实现优先级队列
我有一个家庭作业问题如下: 已创建将实现优先级队列的HEAPPERIORITYQUEUEJava使用二进制堆错误实现优先级队列,java,arrays,string,priority-queue,binary-heap,Java,Arrays,String,Priority Queue,Binary Heap,我有一个家庭作业问题如下: 已创建将实现优先级队列的HEAPPERIORITYQUEUE import java.util.Arrays; public class HeapPriorityQueue<K,V extends Comparable<K>> implements PriorityQueue<K, V> { private static final int DEFAULT_CAPACITY = 10; protecte
import java.util.Arrays;
public class HeapPriorityQueue<K,V extends Comparable<K>> implements PriorityQueue<K, V> {
private static final int DEFAULT_CAPACITY = 10;
protected K[] array;
protected int size;
/**
* Constructs a new BinaryHeap.
*/
@SuppressWarnings("unchecked")
public HeapPriorityQueue() {
// Java doesn't allow construction of arrays of placeholder data types
array = (K[])new Comparable[DEFAULT_CAPACITY];
size = 0;
}
/**
* Adds a value to the min-heap.
*/
public void add(K value) {
// grow array if needed
if (size >= array.length - 1) {
array = this.resize();
}
// place element into heap at bottom
size++;
int index = size;
array[index] = value;
bubbleUp();
}
/**
* Returns true if the heap has no elements; false otherwise.
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Returns (but does not remove) the minimum element in the heap.
*/
public K peek() {
if (this.isEmpty()) {
throw new InvalidKeyException("Invalid Key");
}
return array[1];
}
/**
* Removes and returns the minimum element in the heap.
*/
public K remove() {
// what do want return?
K result = peek();
// get rid of the last leaf/decrement
array[1] = array[size];
array[size] = null;
size--;
bubbleDown();
return result;
}
/**
* Returns a String representation of BinaryHeap with values stored with
* heap structure and order properties.
*/
public String toString() {
return Arrays.toString(array);
}
/**
* Performs the "bubble down" operation to place the element that is at the
* root of the heap in its correct place so that the heap maintains the
* min-heap order property.
*/
protected void bubbleDown() {
int index = 1;
// bubble down
while (hasLeftChild(index)) {
// which of my children is smaller?
int smallerChild = leftIndex(index);
// bubble with the smaller child, if I have a smaller child
if (hasRightChild(index)
&& array[leftIndex(index)].compareTo(array[rightIndex(index)]) > 0) {
smallerChild = rightIndex(index);
}
if (array[index].compareTo(array[smallerChild]) > 0) {
swap(index, smallerChild);
} else {
// otherwise, get outta here!
break;
}
// make sure to update loop counter/index of where last el is put
index = smallerChild;
}
}
/**
* Performs the "bubble up" operation to place a newly inserted element
* (i.e. the element that is at the size index) in its correct place so
* that the heap maintains the min-heap order property.
*/
protected void bubbleUp() {
int index = this.size;
while (hasParent(index)
&& (parent(index).compareTo(array[index]) > 0)) {
// parent/child are out of order; swap them
swap(index, parentIndex(index));
index = parentIndex(index);
}
}
protected boolean hasParent(int i) {
return i > 1;
}
protected int leftIndex(int i) {
return i * 2;
}
protected int rightIndex(int i) {
return i * 2 + 1;
}
protected boolean hasLeftChild(int i) {
return leftIndex(i) <= size;
}
protected boolean hasRightChild(int i) {
return rightIndex(i) <= size;
}
protected K parent(int i) {
return array[parentIndex(i)];
}
protected int parentIndex(int i) {
return i / 2;
}
protected K[] resize() {
return Arrays.copyOf(array, array.length * 2);
}
protected void swap(int index1, int index2) {
K tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Entry<K, V> max() throws EmptyPriorityQueueException {
// TODO Auto-generated method stub
return null;
}
@Override
public Entry<K, V> insert(K key, V value) throws InvalidKeyException {
// TODO Auto-generated method stub
return null;
}
@Override
public Entry<K, V> extractMax() throws EmptyPriorityQueueException {
// TODO Auto-generated method stub
return null;
}
}
导入java.util.array;
公共类HeapPriorityQueue实现PriorityQueue{
专用静态最终int默认_容量=10;
受保护的K[]数组;
保护整数大小;
/**
*构造一个新的二进制堆。
*/
@抑制警告(“未选中”)
公共HeapPriorityQueue(){
//Java不允许构造占位符数据类型的数组
数组=(K[])新的可比[默认容量];
尺寸=0;
}
/**
*将值添加到最小堆。
*/
公共无效添加(K值){
//如果需要,增加阵列
如果(size>=array.length-1){
array=this.resize();
}
//将元素放入底部的堆中
大小++;
int索引=大小;
数组[索引]=值;
泡泡蛋白();
}
/**
*如果堆没有元素,则返回true;否则返回false。
*/
公共布尔值isEmpty(){
返回大小==0;
}
/**
*返回(但不删除)堆中的最小元素。
*/
公共K peek(){
如果(this.isEmpty()){
抛出新的InvalidKeyException(“无效密钥”);
}
返回数组[1];
}
/**
*移除并返回堆中的最小元素。
*/
公共K remove(){
//你想要什么回报?
K结果=peek();
//去掉最后一片叶子/减量
数组[1]=数组[size];
数组[size]=null;
大小--;
泡泡镇();
返回结果;
}
/**
*返回二进制堆的字符串表示形式,其中的值存储在
*堆结构和顺序属性。
*/
公共字符串toString(){
返回array.toString(数组);
}
/**
*执行“气泡向下”操作以放置位于
*将堆的根放在正确的位置,以便堆维护
*最小堆顺序属性。
*/
受保护的void bubbleDown(){
int指数=1;
//冒泡
while(hasleeftchild(index)){
//我的哪个孩子比较小?
int smallerChild=leftIndex(index);
//如果我有一个小一点的孩子,和小一点的孩子一起泡泡
if(hasRightChild(索引)
&&数组[leftIndex(index)]。比较(数组[rightIndex(index)])>0{
smallerChild=右索引(索引);
}
如果(数组[index].compareTo(数组[smallerChild])>0{
掉期(指数,小型儿童);
}否则{
//否则,离开这里!
打破
}
//确保更新最后一个el放置位置的循环计数器/索引
指数=小儿童;
}
}
/**
*执行“气泡上升”操作以放置新插入的图元
*(即尺寸索引处的元素)在其正确位置,以便
*堆维护最小堆顺序属性。
*/
受保护的void bubbleUp(){
int index=this.size;
while(hasParent(index)
&&(父(索引).compareTo(数组[索引]>0)){
//父级/子级出现故障;请交换它们
掉期(指数、父指数(指数));
索引=父索引(索引);
}
}
受保护的布尔hasParent(int i){
返回i>1;
}
受保护的int-leftIndex(int-i){
返回i*2;
}
受保护的整数右索引(整数i){
返回i*2+1;
}
受保护的布尔hasLeftChild(int i){
返回leftIndex(i)您声明了heappropriorityqueue
,但您尝试将其用作:
PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();
PriorityQueue queue=new HeapPriorityQueue();
但是String
不能扩展Comparable
,因此它会给你带来编译错误。谢谢你的修复@benatespinaV=String值,我不知道如何将Comparable int应用于K,同时将V作为字符串。先生,你是什么意思?为什么需要V扩展Comparable?我的意思是,例如在PriorityQueue中,V是s直到那里,因为我必须把int或数字输入到数字队列的字中。插入(0,“零”);那么你为什么不使用?这很有效!谢谢!嗯,如果你不介意我问,为什么我运行程序时根本没有输出?
public class HeapPriorityQueueDriver {
public static void main(String[] args) {
PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();
queue.insert(0, "Zero");
queue.insert(10, "Ten");
queue.insert(1, "One");
queue.insert(5, "Five");
queue.insert(3, "Three");
queue.insert(7, "Seven");
queue.insert(9, "Nine");
while(!queue.isEmpty()) {
System.out.println(queue.extractMax());
} // end while
} // end main
}
Bound mismatch: The type String is not a valid substitute for the bounded parameter <V extends Comparable<K>> of the type HeapPriorityQueue<K,V> HeapPriorityQueueDriver.java /MP7/src/simon/mp7 line 6
Java Problem
Bound mismatch: The type K is not a valid substitute for the bounded parameter <K extends Comparable<K>> of the type Entry<K,V> HeapPriorityQueue.java /MP7/src/simon/mp7 line 199
Java Problem
Bound mismatch: The type K is not a valid substitute for the bounded parameter <K extends Comparable<K>> of the type Entry<K,V> HeapPriorityQueue.java /MP7/src/simon/mp7 line 193
Java Problem
Bound mismatch: The type K is not a valid substitute for the bounded parameter <K extends Comparable<K>> of the type Entry<K,V> HeapPriorityQueue.java /MP7/src/simon/mp7 line 187
Java Problem
The method compareTo(K) is undefined for the type K HeapPriorityQueue.java /MP7/src/simon/mp7 line 126
Java Problem
The method compareTo(K) is undefined for the type K HeapPriorityQueue.java /MP7/src/simon/mp7 line 104
Java Problem
The method compareTo(K) is undefined for the type K HeapPriorityQueue.java /MP7/src/simon/mp7 line 100
Java Problem
Bound mismatch: The type K is not a valid substitute for the bounded parameter <K extends Comparable<K>> of the type PriorityQueue<K,V> HeapPriorityQueue.java /MP7/src/simon/mp7 line 5
the message in the console is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Bound mismatch: The type String is not a valid substitute for the bounded parameter <V extends Comparable<K>> of the type HeapPriorityQueue<K,V>
at simon.mp7.HeapPriorityQueueDriver.main(HeapPriorityQueueDriver.java:6)
PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();