Traversing container/heap

273 views
Skip to first unread message

Eduard Castany

unread,
Mar 4, 2014, 9:53:16 AM3/4/14
to golan...@googlegroups.com
I'm in the need of looping through all the elements of a container/heap. How can I do this efficiently and guarantying that currentElement < nextElement  without modifying the heap?

Matthew Zimmerman

unread,
Mar 4, 2014, 10:35:52 AM3/4/14
to Eduard Castany, golang-nuts
I haven't used the container/heap package before but looking at it you
can't do what you're asking. The heap interface doesn't define an
"iterator" or any other similar method to fetch the data in "order".
You could pop and push them all back on, but that's modifying the
heap.

So you either need to go one level down and do it on the type that's
implementing the container/heap interface or write your own
implementation.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Péter Szilágyi

unread,
Mar 4, 2014, 11:03:15 AM3/4/14
to Matthew Zimmerman, Eduard Castany, golang-nuts
You cannot. By the very nature of the heap data structure.

A heap guarantees ordered pops, but without effectively simulating all the individual pops, you won't be able to get them ordered.

Jan Mercl

unread,
Mar 4, 2014, 11:27:51 AM3/4/14
to Eduard Castany, golang-nuts
You might want to consider a naturally ordered data structure, for
example a B-tree. One such: http://godoc.org/github.com/cznic/b

-j

dja...@gmail.com

unread,
Mar 4, 2014, 11:36:44 AM3/4/14
to golan...@googlegroups.com
Hi,
The only way is to make copy of data (usually  slice).
Djadala
Reply all
Reply to author
Forward
0 new messages