-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
115 lines (96 loc) · 2.22 KB
/
tree.h
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// tree for rpal intepreter
#ifndef TREE_H
#define TREE_H
#include <iostream>
#include <stack>
using namespace std;
// class for syntax tree
class tree {
private:
string val;
string type;
public:
tree *left; // Left child
tree *right; // Right child
void setType(string typ); // Set type of node
void setVal(string value); // Set value of node
string getType(); // Get type of node
string getVal(); // Get value of node
tree *createNode(string value, string typ); // Create node
tree *createNode(tree *x); // Create node
void print_tree(int no_of_dots); // Print tree
};
// Set type of node
void tree::setType(string typ)
{
type = typ;
}
// Set value of node
void tree::setVal(string value)
{
val = value;
}
// Get type of node
string tree::getType()
{
return type;
}
// Get value of node
string tree::getVal()
{
return val;
}
// Create node with value and type
tree *createNode(string value, string typ)
{
tree *t = new tree();
t->setVal(value);
t->setType(typ);
t->left = NULL;
t->right = NULL;
return t;
}
// Create node with tree object
tree *createNode(tree *x)
{
tree *t = new tree();
t->setVal(x->getVal());
t->setType(x->getType());
t->left = x->left;
t->right = NULL;
return t;
}
// Print syntax tree
void tree::print_tree(int no_of_dots)
{
int n = 0;
while (n < no_of_dots)
{
cout << ".";
n++;
}
// If node type is ID, STR or INT, print <type:val>
if (type == "ID" || type == "STR" || type == "INT")
{
cout << "<";
cout << type;
cout << ":";
}
// If node type is BOOL, NIL or DUMMY, print <val>
if (type == "BOOL" || type == "NIL" || type == "DUMMY")
cout << "<";
cout << val;
// If node type is ID, STR or INT, print >
if (type == "ID" || type == "STR" || type == "INT")
cout << ">";
// If node type is BOOL, NIL or DUMMY, print >
if (type == "BOOL" || type == "NIL" || type == "DUMMY")
cout << ">";
cout << endl;
// Print left and right subtrees
if (left != NULL)
left->print_tree(no_of_dots + 1);
if (right != NULL)
right->print_tree(no_of_dots);
}
#endif