我需要以不同的顺序处理一棵树,比方说BFS和DFS.通过使用队列或堆栈,两者都很容易,但是,我在Java中缺少适当的接口,允许执行类似的操作
QueueOrStack<N> pending = ...
while (!pending.isEmpty()) {
N node = pending.poll(); // <----- this is the problem
pending.addAll(node.children());
process(node);
}
Run Code Online (Sandbox Code Playgroud)
没有真正的问题,我可以将其封装ArrayList到实现Queue1的内容中,但是我敢打赌,我忽略了Java Collection Framework中的某些内容.还是真的不见了?
__
1或使用最新的第一个比较器PriorityQueue,这可能是一个愚蠢的想法