-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix.js
61 lines (52 loc) · 980 Bytes
/
postfix.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
class Tree {
constructor(d, l, r){
this.data = d;
this.left = l;
this.right = r;
}
isLeaf(){
return this.left == undefined && this.right == undefined;
}
performOperation(v1,v2,op){
let value;
switch(op){
case "+":
value = v1 + v2;
break;
case "-":
value = v1 - v2;
break;
case "*":
value = v1 * v2;
break;
case "/":
value = v1 / v2;
break;
}
return value;
}
postorderTraverse(){
if(this.isLeaf())
return this.data;
else{
let v1,v2;
v1 = this.left.postorderTraverse();
v2 = this.right.postorderTraverse();
return performOperation(v1,v2,this.data);
}
}
preorderTraverse(){
if(this.isLeaf()
return this.data;
else {
}
}
}
var t0 = new Tree(1);
var t1 = new Tree(2);
var r = new Tree("+",t0,t1);
console.log(r.isLeaf());
console.log(t0.isLeaf());
console.log(r.postorderTraverse());
//TODO: implement the other fix expressions
// make the printing stuff happen