-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreOrderIterative.js
61 lines (56 loc) · 1.38 KB
/
PreOrderIterative.js
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
var Stack = function() {
this.count = 0;
this.storage = {};
}
// Adds a value onto the end of the stack
Stack.prototype.push = function(value) {
this.storage[this.count] = value;
this.count++;
}
// Removes and returns the value at the end of the stack
Stack.prototype.pop = function() {
// Check to see if the stack is empty
if (this.count === 0) {
return undefined;
}
this.count--;
var result = this.storage[this.count];
delete this.storage[this.count];
return result;
}
// Returns the length of the stack
Stack.prototype.size = function() {
return this.count;
}
Stack.prototype.peek = function() {
return this.storage[this.count-1];
}
function PreOrderIterative(root){
var container = [],result=[];
var stack = new Stack()
if(root==null)
return ;
stack.push(root);
while(stack.count>0){
var node = stack.peek();
result.push(node.data);
stack.pop();
// Push right and left children of the popped node to stack
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
console.log(result);
}
PreOrderIterative({ data: 129,
left:
{ data: 97,
left: { data: 93 },
right: { data: 106} },
right:
{ data: 98,
left: { data: 27 },
right: { data: 61} } })