Redian新闻
>
priority_queue C++ implementation
avatar
priority_queue C++ implementation# JobHunting - 待字闺中
a*r
1
下面的代码work,但是至少有一个nasty bug,大家猜猜看?
还有,请对代码提意见:
avatar
a*m
2
sublime? //hand
avatar
a*r
3

ya, love it

【在 a********m 的大作中提到】
: sublime? //hand
avatar
a*m
4
嗯。俺重装系统以后就压根没装vs了,而且几个os都有,没有换编辑器的问题。

【在 a***r 的大作中提到】
:
: ya, love it

avatar
a*r
5
大牛能指教一下我的C++code吗?
我有一段时间没写C++code了,任何建议,我洗耳恭听.
avatar
a*m
6
随便扫了一眼,没啥特别明显问题。move_down看起来有点怪,你可以再检查一下。
avatar
s*s
7
推荐几个sublime最常用的插件吧?python, c++用的。
avatar
a*r
9

move_down处理的item比较多:
》有没有child:
没有,只有左,有左右(和左还是右对换)
》minQueue和maxQueue比较的不同
我加了些comment.还是有点乱。
大牛如果你写,会怎么写?

【在 a********m 的大作中提到】
: 随便扫了一眼,没啥特别明显问题。move_down看起来有点怪,你可以再检查一下。
avatar
a*m
10
写成递归可能代码简单点。

【在 a***r 的大作中提到】
:
: move_down处理的item比较多:
: 》有没有child:
: 没有,只有左,有左右(和左还是右对换)
: 》minQueue和maxQueue比较的不同
: 我加了些comment.还是有点乱。
: 大牛如果你写,会怎么写?

avatar
p*p
11
恕我眼拙,LZ说的BUG在哪,请明示
avatar
l*8
12
我写了个moveDown
avatar
a*r
13

不错,不过:
if(rightIdx>size())
shouldn't it be:
if(rightIdx<=size()-1)
?

【在 l*********8 的大作中提到】
: 我写了个moveDown
avatar
l*8
14
yes, you're right!
正如秋虫大牛所说,递归的话更简单:

【在 a***r 的大作中提到】
:
: 不错,不过:
: if(rightIdx>size())
: shouldn't it be:
: if(rightIdx<=size()-1)
: ?

avatar
l*8
15
同问,我也没看出这个bug

【在 p*****p 的大作中提到】
: 恕我眼拙,LZ说的BUG在哪,请明示
avatar
a*r
16

bug 在 size_t 上。
when comparing:
while(iwhen size()==0
size()-1 is (2^32-1)
very nasty.

【在 p*****p 的大作中提到】
: 恕我眼拙,LZ说的BUG在哪,请明示
avatar
l*8
17
哦,是啊。 的确是不容易注意的一个bug

【在 a***r 的大作中提到】
:
: bug 在 size_t 上。
: when comparing:
: while(i: when size()==0
: size()-1 is (2^32-1)
: very nasty.

avatar
l*8
18
if (child > size())
应该是
if (child >= size())

【在 l*********8 的大作中提到】
: yes, you're right!
: 正如秋虫大牛所说,递归的话更简单:

avatar
q*z
19
我看C++的文档说push是O(1)时间,这个实现的好像不是O(1)?
avatar
a*r
20

My implementation is based on BinaryHeap,insert's worst-case cost is O(logN)
Fibonacci Heap's insert amortized cost is O(1)
which C++ document are you referring to?

【在 q*******z 的大作中提到】
: 我看C++的文档说push是O(1)时间,这个实现的好像不是O(1)?
avatar
a*r
22

Complexity
Constant (in the priority_queue). Although notice that push_heap operates
in logarithmic time.
I think it means the time spent inside priority_queue is O(1),if adding in
push_heap, the overall complexity should be O(lgN).

【在 q*******z 的大作中提到】
: http://www.cplusplus.com/reference/queue/priority_queue/push/
:
: logN)

avatar
q*z
23
yes, it depends on the heap type

【在 a***r 的大作中提到】
:
: Complexity
: Constant (in the priority_queue). Although notice that push_heap operates
: in logarithmic time.
: I think it means the time spent inside priority_queue is O(1),if adding in
: push_heap, the overall complexity should be O(lgN).

avatar
b*7
24
与std::priority_queue语义不太一致。std::priority_queue,std::less
>得到的是max heap,而你的priority_queue >得到的是min heap

【在 a***r 的大作中提到】
: 下面的代码work,但是至少有一个nasty bug,大家猜猜看?
: 还有,请对代码提意见:

avatar
r*n
25
priority queue其实最好index从1开始,这样parent一个表达式就出来了:k/2,left
child是2k,right child2k+1
看下面链接的实现,比较简洁
http://algs4.cs.princeton.edu/24pq/
avatar
d*x
26
less的作用是提供一个偏序
在这个偏序下"最大"的元素将被放在堆顶。。。这有啥不能理解的啊!!!

where
,
operator
same
avatar
b*n
27
不好意思,问的太笼统了
我不能理解的是如果用less 而且" returns true if a is to be placed earlier
than b", 那么不应该是越小排在越前面,这样不就是min heap了? 为什么是max heap?
求指教
avatar
d*x
28
人家就不能用less找出最大的嘛!

earlier

【在 b*******n 的大作中提到】
: 不好意思,问的太笼统了
: 我不能理解的是如果用less 而且" returns true if a is to be placed earlier
: than b", 那么不应该是越小排在越前面,这样不就是min heap了? 为什么是max heap?
: 求指教

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。