-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryNode.cpp
96 lines (75 loc) · 1.44 KB
/
BinaryNode.cpp
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
/*
* BinaryNode.cpp
*
* Created on: 08/giu/2014
* Author: Matteo
*/
#include "BinaryNode.h"
/*
* BINARY NODE
* Generic structure of a BinaryTree node
*/
BinaryNode::BinaryNode() {
// Set pointers to null
left = right = NULL;
}
BinaryNode::~BinaryNode() {
if (left != nullptr) {
delete left;
left = nullptr;
}
if (right != nullptr) {
delete right;
right = nullptr;
}
}
double BinaryNode::eval(Vec2d v) {
return INFINITY;
}
/*
* CONSTANT NODE
* Leaf of the BinaryTree that returns a constant when evaluated
*/
double ConstantNode::eval(Vec2d v) {
return val;
}
/*
* TokenId NODE
* Node that recursively evaluates its children and gives back
* the result of a binary arithmetic expression
*/
double OperatorNode::eval(Vec2d v) {
// Mega switch on all the possible TokenId
switch (this->op) {
// Both operands
case SUM:
return left->eval(v) + right->eval(v);
case SUB:
return left->eval(v) - right->eval(v);
case DIV:
return left->eval(v) / right->eval(v);
case MULT:
return left->eval(v) * right->eval(v);
case POW:
return pow(left->eval(v), right->eval(v));
// Single operand
case SIN:
return sin(right->eval(v));
case COS:
return cos(right->eval(v));
default:
return 0;
}
}
/*
* VARIABLE NODE
* Leaf that returns the value of a double passed by reference
*/
double VariableNode::eval(Vec2d v){
switch (var){
case VARX:
return v.x;
case VARY:
return v.y;
}
}