[英]ArrayDeque vs LinkedList as Queue for level-order traversal
Is it true that ArrayDeque
should be preferred over LinkedList
in this scenario? 在这种情况下, ArrayDeque
是否比LinkedList
受青睐吗? Why is ArrayDeque better than LinkedList . 为什么ArrayDeque比LinkedList更好 ?
In my opinion, I should be using LinkedList instead of ArrayDeque, as there are quite a lot of poll
and offer
operation going on in this algorithm, and there is no random access to elements. 在我看来,我应该使用LinkedList而不是ArrayDeque,因为此算法中正在进行很多poll
和offer
操作,并且没有对元素的随机访问。
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode a) {
Queue<TreeNode> q = new LinkedList<>(); // new ArrayDeque<>() ???
q.offer(a);
ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
while (q.peek() != null){ //returns null if empty
ArrayList<Integer> list = new ArrayList<>();
int n = q.size();
for (int i = 0; i < n; i++){
TreeNode node = q.poll();
list.add(node.val);
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
ans.add(list);
}
return ans;
}
There are a number of tradeoffs to consider when deciding between ArrayDeque
and LinkedList
when treating the two as a doubly-ended queue. 在将ArrayDeque
和LinkedList
视为双端队列时,需要在两者之间ArrayDeque
权衡。
Generally speaking, the LinkedList
type has the following advantages: 一般来说, LinkedList
类型具有以下优点:
Generally speaking, the LinkedList
type has the following major disadvantage: 一般来说, LinkedList
类型具有以下主要缺点:
LinkedList
are not necessarily stored contiguously in memory, the LinkedList
has poor locality of reference, and accesses may result in more cache misses, decreasing performance. 由于LinkedList
中的元素不一定必须连续存储在内存中,因此LinkedList
的引用位置很差,并且访问可能会导致更多的高速缓存未命中,从而降低性能。 Generally speaking, the ArrayDeque
type has the following advantages: 一般来说, ArrayDeque
类型具有以下优点:
ArrayDeque
has great locality of reference, so element accesses will typically create few cache misses and thus lead to excellent performance. 因为元素是连续存储的,所以ArrayDeque
的引用位置很大,因此元素访问通常将创建很少的缓存未命中,从而导致出色的性能。 Generally speaking, the ArrayDeque
has the following disadvantage: 一般来说, ArrayDeque
具有以下缺点:
ArrayDeque
will take time O(n), when the array capacity is exceeded on an insertion, the ArrayDeque
may have to do O(n) work to copy elements. 尽管对ArrayDeque
进行n个操作的任何序列ArrayDeque
将花费时间O(n),但是当插入时超出数组容量时, ArrayDeque
可能必须执行O(n)才能复制元素。 As a result, the worst-case performance of an ArrayDeque
per operation is slower than a LinkedList
. 结果,每个操作ArrayDeque
的最坏情况性能要比LinkedList
慢。 ArrayDeque
will take time O(n). 同样,如果删除时阵列容量下降得太低,则调整阵列大小可能需要花费时间O(n),尽管像以前一样,对ArrayDeque
任何n序列操作都会花费时间O(n)。 In your particular use case, since all you care about is the end-to-end efficiency of the level-order traversal, not the cost of any individual addition or removal of elements from the queue, it will probably be faster to use an ArrayDeque
than a LinkedList
. 在您的特定用例中,由于您关心的只是级别顺序遍历的端到端效率,而不是从队列中单独添加或删除元素的成本,因此使用ArrayDeque
可能会更快比LinkedList
。 In a sense, the LinkedList
type is generally only better if you need to have good worst-case performance on each operation performed, and that isn't the case here. 从某种意义上说,通常只有在您需要在执行的每个操作上都具有良好的最坏情况下的性能时, LinkedList
类型才更好,而在这里不是这种情况。 The ArrayDeque
is generally more performant when all you care about is end-to-end runtime. 当您只关心端到端运行ArrayDeque
通常会更ArrayDeque
。
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.