使用JavaScript与C++实现的堆差优先级队列 我在JavaScript中用类似于C++编写的类似实现重写了HeAP优先队列实现。我完全不知道为什么C++实现工作,而JS一个则不行。有人能告诉我造成JS实现错误的差异吗
C++代码:使用JavaScript与C++实现的堆差优先级队列 我在JavaScript中用类似于C++编写的类似实现重写了HeAP优先队列实现。我完全不知道为什么C++实现工作,而JS一个则不行。有人能告诉我造成JS实现错误的差异吗,javascript,c++,heap,priority-queue,Javascript,C++,Heap,Priority Queue,C++代码: #include <iostream> #include <iomanip> #include <ctime> #include <cstdlib> using namespace std; const int MAXINT = -2147483647; struct qelement { int prio, data; }; class queue { private: qelement * T;
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAXINT = -2147483647;
struct qelement
{
int prio, data;
};
class queue
{
private:
qelement * T; // kopiec dynamiczny
int n; // liczba elementów
public:
queue(int max_n);
~queue();
bool empty();
int front();
int frontprio();
void push(int prio, int v);
void pop();
};
queue::queue(int max_n)
{
T = new qelement[max_n]; // tworzymy tablicę dynamiczną
n = 0; // kopiec jest pusty
}
queue::~queue()
{
delete [] T;
}
bool queue::empty()
{
return !n;
}
int queue::front()
{
return n ? T[0].data : -MAXINT;
}
int queue::frontprio()
{
return n ? T[0].prio : -MAXINT;
}
{
int i,j;
i = n++;
j = (i - 1) / 2;
while(i > 0 && T[j].prio < prio)
{
T[i] = T[j];
i = j;
j = (i - 1) / 2;
}
T[i].prio = prio;
T[i].data = v;
}
void queue::pop()
{
int i,j,v,p;
if(n--)
{
p = T[n].prio;
v = T[n].data;
i = 0;
j = 1;
while(j < n)
{
if(j + 1 < n && T[j + 1].prio > T[j].prio) j++;
if(p >= T[j].prio) break;
T[i] = T[j];
i = j;
j = 2 * j + 1;
}
T[i].prio = p;
T[i].data = v;
}
}
int main()
{
queue Q(10); // kolejka 10-cio elementowa
int i,p,v;
srand(time(NULL));
for(i = 0; i < 10; i++)
{
v = rand() % 100;
p = rand() % 10;
cout << setw(2) << v << ":" << p << endl;
Q.push(p,v);
}
cout << "----\n";
while(!Q.empty())
{
cout << setw(2) << Q.front() << ":" << Q.frontprio() << endl;
Q.pop();
}
}
队列如下所示:
JS: C++:
3 : 9 3 : 9
99 : 8 99 : 8
24 : 7 70 : 8
70 : 8 24 : 7
32 : 6 32 : 6
40 : 1 4 : 6
4 : 6 30 : 5
88 : 3 88 : 3
40 : 1 40 : 1
30 : 5 40 : 1
你忘了换衣服了
T[i] = T[j];
与
在while循环内的pop方法
干杯。问题在于推送和弹出方法。我重写了它们,优先级队列正常工作:
var MAXINT = -2147483647;
function qelement(data, prio) {
this.prio = prio;
this.data = data;
}
function queue(l) {
var T = new Array(l);
var n = 0;
for (var i = 0; i < l; i++) T[i] = new qelement(null, 0);
this.empty = function () {
return !n;
}
this.writeq = function () {
var i = 0;
console.log('This is the whole heap');
while (i < l) {
console.log(T[i].data + ' : ' + T[i].prio);
i++;
}
}
this.front = function () {
return n ? T[0].data : -MAXINT;
}
this.frontPrio = function () {
return n ? T[0].prio : -MAXINT;
}
this.push = (prio, v) => {
var i, j;
i = n++;
T[i].prio = prio;
T[i].data = v;
T.sort((qEl1, qEl2) => {
if(qEl1.prio < qEl2.prio) return 1;
else return 0;
});
}
this.pop = () => {
var i, j, v, p;
if (n--) {
T[0].prio = 0;
T[0].data = null;
T.sort((qEl1, qEl2) => {
if(qEl1.prio < qEl2.prio) return 1;
else return 0;
});
}
}
}
var test = new queue(10);
var i, p, v;
for (i = 0; i < 10; i++) {
v = Math.round(Math.random() * 100);
p = Math.round(Math.random() * 10);
console.log(v + ' : ' + p);
test.push(p, v);
}
console.log('-------');
test.writeq();
console.log('-------');
while (!test.empty()) {
console.log(test.front() + ' : ' + test.frontPrio());
test.pop();
}
如果每个人都知道自己的代码做什么,那么两个bug报告都不存在。什么是不工作的?测试时会发生什么?有什么错误吗?当使用相同的输入进行测试时,两种实现的预期值和实际值是什么?预期行为是调试自己的代码。当你遇到一个你不理解的特定问题时,你会发布一个关于这个问题的问题。在我们身上扔了几百行,然后说,为什么这项工作不可能得到有用的答案。我已经更新了这个问题,用每次推送和弹出对数组进行排序当然有效,但效率非常低。有一个更好的方法。您可能想研究二进制堆。事实上,这只是一个概念证明:尽管这不是完成任务的最有效方式,但它表明问题在于这两种方法
T[i] = T[j];
T[i].prio = T[j].prio;
T[i].data = T[j].data;
var MAXINT = -2147483647;
function qelement(data, prio) {
this.prio = prio;
this.data = data;
}
function queue(l) {
var T = new Array(l);
var n = 0;
for (var i = 0; i < l; i++) T[i] = new qelement(null, 0);
this.empty = function () {
return !n;
}
this.writeq = function () {
var i = 0;
console.log('This is the whole heap');
while (i < l) {
console.log(T[i].data + ' : ' + T[i].prio);
i++;
}
}
this.front = function () {
return n ? T[0].data : -MAXINT;
}
this.frontPrio = function () {
return n ? T[0].prio : -MAXINT;
}
this.push = (prio, v) => {
var i, j;
i = n++;
T[i].prio = prio;
T[i].data = v;
T.sort((qEl1, qEl2) => {
if(qEl1.prio < qEl2.prio) return 1;
else return 0;
});
}
this.pop = () => {
var i, j, v, p;
if (n--) {
T[0].prio = 0;
T[0].data = null;
T.sort((qEl1, qEl2) => {
if(qEl1.prio < qEl2.prio) return 1;
else return 0;
});
}
}
}
var test = new queue(10);
var i, p, v;
for (i = 0; i < 10; i++) {
v = Math.round(Math.random() * 100);
p = Math.round(Math.random() * 10);
console.log(v + ' : ' + p);
test.push(p, v);
}
console.log('-------');
test.writeq();
console.log('-------');
while (!test.empty()) {
console.log(test.front() + ' : ' + test.frontPrio());
test.pop();
}