-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpTreeFunctionalSyntax.js
More file actions
73 lines (62 loc) · 2.06 KB
/
ExpTreeFunctionalSyntax.js
File metadata and controls
73 lines (62 loc) · 2.06 KB
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
const expressionTree = {
// need 2 determine tree ht if possible
operations: {
'^': (a, b) => a ** b,
'*': (a, b) => a * b,
'/': (a, b) => a / b,
'+': (a, b) => a + b,
'-': (a, b) => a - b
},
createNode(data) {
const node = {
data: data,
left: null,
right: null
}
return node
},
parse(expression) {
const treeStack = []
expression.forEach((char) => {
const newNode = this.createNode(char)
if (!Object.keys(this.operations).includes(char)) {
newNode.data = parseInt(newNode.data, 10)
treeStack.push(newNode)
} else {
newNode.right = treeStack.pop()
newNode.left = treeStack.pop()
treeStack.push(newNode)
}
})
console.log('parsed input tree pre-evaluation: ')
console.log(JSON.stringify(treeStack[0], null, 4))
return treeStack[0]
},
operate(n1, o, n2) {
return this.operations[o](n1, n2)
},
evaluate(root) {
if (!root.data) return 0
if (!root.left && !root.right) return root.data
const leftResult = this.evaluate(root.left)
const rightResult = this.evaluate(root.right)
return this.operate(leftResult, root.data, rightResult)
},
returnAnswer(input) { // postfix
return this.evaluate(this.parse(input))
}
}
const input = ['8', '2', '^', '6', '+', '2', '10', '*', '2', '/', '-']
const answer = expressionTree.returnAnswer(input)
console.log('Postfix input: ', input.join(' '))
console.log('answer: ', answer)
// 12 2 6-*
const input2 = ['12', '2', '6', '-', '*']
const answer2 = expressionTree.returnAnswer(input2)
console.log('Postfix input2: ', input2.join(' '))
console.log('answer2: ', answer2)
// 12 2 * 6-
const input3 = ['12', '2', '*', '6', '-']
const answer3 = expressionTree.returnAnswer(input3)
console.log('Postfix input3: ', input3.join(' '))
console.log('answer3: ', answer3)