-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpTree.h
More file actions
78 lines (71 loc) · 1.8 KB
/
ExpTree.h
File metadata and controls
78 lines (71 loc) · 1.8 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
74
75
76
77
78
//
// Created by Vallath Nandakumar on 11/15/17.
//
#ifndef DAY22_EXPRESSIONTREE_EXPTREE_H
#define DAY22_EXPRESSIONTREE_EXPTREE_H
#include <vector>
#include <iostream>
#include "String.h"
#include "Parse.h"
using namespace std;
class Node {
public:
bool isOperand;
bool hasSingleOperator;
int operand;
String var;
String optr;
Node* left;
Node* right;
//Primary simple constructor
Node(bool isOperand, int operand, String optr) {
left = nullptr;
right = nullptr;
this->isOperand = isOperand;
this->operand = operand;
this->optr = optr;
this->hasSingleOperator = false;
this->var = "";
}
//Constructor for variables
Node(bool isOperand, String var, String optr) {
left = nullptr;
right = nullptr;
this->isOperand = isOperand;
this->var = var;
this->operand = 0;
this->optr = optr;
this->hasSingleOperator=false;
}
//Constructor for operators
Node(bool isOperand, int operand, String optr, bool hasSingleOperator) {
left = nullptr;
right = nullptr;
this->isOperand = isOperand;
this->operand = operand;
this->optr = optr;
this->hasSingleOperator=hasSingleOperator;
this->var = "";
}
void print() {
if (isOperand) {
std::cout << operand << std::endl;
} else {
std::cout <<optr.c_str()<<std::flush;
}
}
};
class ExpTree{
private:
const int spaces_per_level = 4;
Node* overallRoot;
Node* add(Node* root, vector<Node*>&);
void printTree(Node*, int);
void printSpaces(int);
int parse(Node*);
public:
int parse();
ExpTree(vector<Node*>& expr); // constructor
void printTree();
};
#endif //DAY22_EXPRESSIONTREE_EXPTREE_H