forked from OperationCode/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInOrderIterative.js
More file actions
45 lines (26 loc) · 805 Bytes
/
InOrderIterative.js
File metadata and controls
45 lines (26 loc) · 805 Bytes
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
import BinaryTreeNode from './BinaryTreeNode';
import Stack from './../stack/Stack';
export function inOrderIterative(tree,callback){
var node = tree.store;
var st = new Stack();
while (node || !st.isEmpty() )
{
/* Reach the leftmost node of the
curr */
while (node !== null)
{
/* place tree node on
the stack before traversing
the node's left subtree */
st.push(node);
node = node.left;
}
/* Node must be NULL at this point */
node = st.pop();
callback(node);
/* we have visited the node and its
left subtree. Now, it is the right
subtree's turn */
node = node.right;
}
}