-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
116 lines (96 loc) · 2.41 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
116
#ifndef TREE_H_
#define TREE_H_
#include <iostream>
#include <stack>
using namespace std;
// Class for syntax tree
class tree
{
private:
string val; // Value of the node
string type; // Type of the node
public:
tree *left; // Pointer to the left child
tree *right; // Pointer to the right child
void setType(string typ); // Set the type of the node
void setVal(string value); // Set the value of the node
string getType(); // Get the type of the node
string getVal(); // Get the value of the node
tree *createNode(string value, string typ); // Create a new node with the given value and type
tree *createNode(tree *x); // Create a new node as a copy of the given node
void print_tree(int no_of_dots); // Print the syntax tree
};
// Set the type of the node
void tree::setType(string typ)
{
type = typ;
}
// Set the value of the node
void tree::setVal(string value)
{
val = value;
}
// Get the type of the node
string tree::getType()
{
return type;
}
// Get the value of the node
string tree::getVal()
{
return val;
}
// Create a new node with the given 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 a new node as a copy of the given node
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 the syntax tree
void tree::print_tree(int no_of_dots)
{
int n = 0;
while (n < no_of_dots)
{
cout << ".";
n++;
}
// If the node type is ID, STR, or INT, print "<type:val>"
if (type == "ID" || type == "STR" || type == "INT")
{
cout << "<";
cout << type;
cout << ":";
}
// If the node type is BOOL, NIL, or DUMMY, print "<val>"
if (type == "BOOL" || type == "NIL" || type == "DUMMY")
cout << "<";
cout << val;
// If the node type is ID, STR, or INT, print ">"
if (type == "ID" || type == "STR" || type == "INT")
cout << ">";
// If the node type is BOOL, NIL, or DUMMY, print ">"
if (type == "BOOL" || type == "NIL" || type == "DUMMY")
cout << ">";
cout << endl;
// Print the left and right subtrees
if (left != NULL)
left->print_tree(no_of_dots + 1);
if (right != NULL)
right->print_tree(no_of_dots);
}
#endif