-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTIterator.java
119 lines (97 loc) · 3.08 KB
/
BSTIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.yk.training.bst.iterators;
import com.yk.training.bst.BSTTraversal;
import com.yk.training.bst.Node;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;
/**
* Iterates a BST.
* Check implementations: {@link BSTArrayIterator} and {@link BSTStackIterator}.
*/
public interface BSTIterator {
boolean hasNext();
int next();
}
/**
* BST iterator which uses a list under the hood.
* <p>
* {@code hasNext()} - O(1),
* {@code next()} - O(1).
* However, it allocates O(n) internal memory before starting the iteration.
* It stores all nodes in the array.
*/
class BSTArrayIterator implements BSTIterator {
final List<Integer> values = new ArrayList<>();
int currentIndex = 0;
public BSTArrayIterator(final Node root) {
if (root == null) {
return;
}
BSTTraversal.inorder(root, node -> values.add(node.value));
}
@Override
public boolean hasNext() {
return currentIndex < values.size();
}
@Override
public int next() {
return values.get(currentIndex++);
}
}
/**
* BST iterator which uses a stack under the hood.
* <p>
* https://leetcode.com/problems/binary-search-tree-iterator/
* <p>
* {@code hasNext()} - O(1), It checks if a stack is empty, which is O(1),
* and left and right children have been visited: left subtree is always visited first,
* see {@link BSTTraversal#inorder(Node, Consumer)}; therefore we do not check anything here.
* If {@code currentNode} has a right child, it means that is has next.
* <p>
* {@code next()} - O(1), <b>on average</b>. In the worst case, it should remove all elements from the stack,
* and then find next element of a node that has not been visited.
* However, when stack has <b>k</b> elements,
* it means that there were other {@code next()} calls at least <b>k</b> times.
* And these calls would take O(1) time complexity: check left, right children, add an element into the stack.
* Therefore, average complexity of <b>k</b> calls is O(1).
*
*
*/
class BSTStackIterator implements BSTIterator {
final LinkedList<Node> stack = new LinkedList<>();
Node currentNode;
public BSTStackIterator(final Node root) {
this.currentNode = root;
if (root == null) {
return;
}
navigateLeftSubtree();
}
@Override
public boolean hasNext() {
return !stack.isEmpty();
}
@Override
public int next() {
currentNode = stack.pop();
final int currentValue = currentNode.value;
if (currentNode.right != null) {
// Push root of the right subtree into stack.
currentNode = currentNode.right;
navigateLeftSubtree();
}
return currentValue;
}
/**
* Find next node to be returned in {@link #next()}.
* Push it to stack.
*/
public void navigateLeftSubtree() {
stack.push(currentNode);
while (currentNode.left != null) {
currentNode = currentNode.left;
stack.push(currentNode);
}
}
}