Learn all these terms:
\(N(h) \leq ??\)
\(\displaystyle\sum_{i=1}^n c = cn\) | \(\displaystyle\sum_{i=1}^n i = \displaystyle\frac{n(n+1)}{2}\) |
\(\displaystyle\sum_{i=1}^n i^2 = \displaystyle\frac{n(n+1)(2n+1)}{6}\) | \(\displaystyle\sum_{i=0}^n r^i = \displaystyle\frac{1 - r^{n+1}}{1 - r}\) |
In a linked list, we store a reference to one node, all other nodes are reachable by following \(O(n)\) references.
In a binary tree, we store a reference to one node, all other nodes are reachable by following \(O(\log_2 n)\) references.
Trace the following traversal method…
public void printMysteryOrder() {
Queue<Node> queue = new LinkedList<>();
queue.offer(root); // enqueue
while (!queue.isEmpty()) {
Node curNode = queue.poll(); // dequeue
System.out.println(curNode.getItem());
if (curNode.getLeft() != null) {
queue.offer(curNode.getLeft());
}
if (curNode.getRight() != null) {
queue.offer(curNode.getRight());
}
}
}