使用 minHeap 使用优先级队列实现的堆栈的预期行为
Expected behaviour of a stack implemented using Priority Queue using minHeap
我对堆和 PQ 的概念不熟悉。所以我试图使用最小堆使用 PQ 实现堆栈。
我正在尝试实现以下方法:
- 流行
- 流行
- 为空
- 顶部
- 尺码
代码如下:
import java.util.*;
import java.lang.System;
public class StackUsingMinPriorityQueue{
static int a[] = {3,7,2,11,9,4};
int size = 0;
PriorityQueue<Integer> pq;
static StackUsingMinPriorityQueue obj;
public static void main(String args[]){
obj = new StackUsingMinPriorityQueue(a.length);
for(int i=0;i<a.length;i++){
obj.push(a[i]);
}
System.out.println("Value popped: "+obj.pop());
System.out.println("Value at Top: "+obj.top());
System.out.println("PQ size: "+obj.size());
System.out.println("PQ IsEmpty: "+obj.isEmpty());
}
public StackUsingMinPriorityQueue(int size){
this.size = size;
pq = new PriorityQueue<Integer>();
}
/**
* 1. PUSH
* **/
public void push(int data){
obj.insert(-System.currentTimeMillis(),data);
}
public void insert(long time, int data) {
pq.offer(data);
}
/**
* 2. POP
*/
public int pop(){
return obj.extractMin();
}
public int extractMin() {
return pq.poll();
}
/**
* 3.TOP
*/
public int top(){
return pq.peek();
}
/**
* 4. SIZE
*/
public int size(){
return pq.size();
}
/**
* 5.IsEmpty
*/
public boolean isEmpty(){
return pq.isEmpty();
}
}
Output:
Value popped: 2
Value at Top: 3
PQ size: 5
PQ IsEmpty: false
现在,我的查询是顶部的值=3。这个对吗 ?它不应该是最后输入的值,即 4 吗?我的理解是,当我们正在实现一个堆栈时,一眼就能看到堆栈顶部的元素,该元素应该是 4 而不是 3。
如果我的实现不正确或者我的理解是否正确,有人可以告诉我吗?
pq.poll()
和pq.peek()
对pq
中的最小元素进行操作,而pop
和top
应该return最后插入的元素.
要使用优先级队列实现堆栈,您应该为每个值添加一个键 -<insertion time>
。您可以使用普通递增计数器作为 "timer"。请注意,此实现将非常低效。
我对堆和 PQ 的概念不熟悉。所以我试图使用最小堆使用 PQ 实现堆栈。 我正在尝试实现以下方法:
- 流行
- 流行
- 为空
- 顶部
- 尺码
代码如下:
import java.util.*;
import java.lang.System;
public class StackUsingMinPriorityQueue{
static int a[] = {3,7,2,11,9,4};
int size = 0;
PriorityQueue<Integer> pq;
static StackUsingMinPriorityQueue obj;
public static void main(String args[]){
obj = new StackUsingMinPriorityQueue(a.length);
for(int i=0;i<a.length;i++){
obj.push(a[i]);
}
System.out.println("Value popped: "+obj.pop());
System.out.println("Value at Top: "+obj.top());
System.out.println("PQ size: "+obj.size());
System.out.println("PQ IsEmpty: "+obj.isEmpty());
}
public StackUsingMinPriorityQueue(int size){
this.size = size;
pq = new PriorityQueue<Integer>();
}
/**
* 1. PUSH
* **/
public void push(int data){
obj.insert(-System.currentTimeMillis(),data);
}
public void insert(long time, int data) {
pq.offer(data);
}
/**
* 2. POP
*/
public int pop(){
return obj.extractMin();
}
public int extractMin() {
return pq.poll();
}
/**
* 3.TOP
*/
public int top(){
return pq.peek();
}
/**
* 4. SIZE
*/
public int size(){
return pq.size();
}
/**
* 5.IsEmpty
*/
public boolean isEmpty(){
return pq.isEmpty();
}
}
Output:
Value popped: 2
Value at Top: 3
PQ size: 5
PQ IsEmpty: false
现在,我的查询是顶部的值=3。这个对吗 ?它不应该是最后输入的值,即 4 吗?我的理解是,当我们正在实现一个堆栈时,一眼就能看到堆栈顶部的元素,该元素应该是 4 而不是 3。
如果我的实现不正确或者我的理解是否正确,有人可以告诉我吗?
pq.poll()
和pq.peek()
对pq
中的最小元素进行操作,而pop
和top
应该return最后插入的元素.
要使用优先级队列实现堆栈,您应该为每个值添加一个键 -<insertion time>
。您可以使用普通递增计数器作为 "timer"。请注意,此实现将非常低效。