-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression_prefix.cpp
More file actions
194 lines (163 loc) · 5.7 KB
/
Expression_prefix.cpp
File metadata and controls
194 lines (163 loc) · 5.7 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
using namespace std;
// Structure for Node
struct Node {
char data;
Node* left;
Node* right;
// Constructor to initialize the Node
Node(char val) : data(val), left(nullptr), right(nullptr) {}
};
// ExpressionTree Class Definition
class ExpressionTree {
public:
Node* root;
// Constructor
ExpressionTree() : root(nullptr) {}
// Build the tree from the prefix expression
void buildTree(const char prefix[]) {
Node* stack[50]; // Stack for nodes
int top = -1;
int length = 0;
// Calculate length of the prefix expression
while (prefix[length] != '\0') length++;
for (int i = length - 1; i >= 0; i--) {
char current = prefix[i];
// If it's an operand, create a node and push it to stack
if ((current >= 'a' && current <= 'z') || (current >= 'A' && current <= 'Z')) {
Node* node = new Node(current);
stack[++top] = node;
}
// If it's an operator, pop two operands from the stack and create a new node
else if (current == '+' || current == '-' || current == '*' || current == '/') {
Node* node = new Node(current);
if (top < 1) {
clear(); // Clean up if invalid expression
cout << "Error: Invalid prefix expression!" << endl;
return;
}
node->left = stack[top--]; // Pop the left operand
node->right = stack[top--]; // Pop the right operand
stack[++top] = node; // Push the new node back to the stack
}
// Ignore whitespace
else if (current == ' ' || current == '\t') {
continue;
}
else {
clear(); // Clean up if invalid character found
cout << "Error: Invalid character '" << current << "' in expression!" << endl;
return;
}
}
// The last element in the stack will be the root of the tree
if (top == 0) {
root = stack[top];
} else {
clear();
cout << "Error: Invalid prefix expression!" << endl;
}
}
// Non-recursive Post-order Traversal
void postOrderTraversal() const {
if (!root) {
cout << "Tree is empty!" << endl;
return;
}
Node* stack1[50], *stack2[50]; // Two stacks
int top1 = -1, top2 = -1;
stack1[++top1] = root;
// Traverse the tree
while (top1 >= 0) {
Node* temp = stack1[top1--];
stack2[++top2] = temp;
// Push left and right children to stack1
if (temp->left) stack1[++top1] = temp->left;
if (temp->right) stack1[++top1] = temp->right;
}
// Output the post-order traversal
cout << "Post-order Traversal: ";
while (top2 >= 0) {
cout << stack2[top2--]->data << " ";
}
cout << endl;
}
// Non-recursive In-order Traversal
void inOrderTraversal() const {
if (!root) {
cout << "Tree is empty!" << endl;
return;
}
Node* stack[50];
int top = -1;
Node* current = root;
cout << "In-order Traversal: ";
while (current != nullptr || top >= 0) {
// Reach the left most Node of the current Node
while (current != nullptr) {
stack[++top] = current;
current = current->left;
}
// Current must be nullptr at this point
current = stack[top--];
cout << current->data << " ";
// Now visit the right subtree
current = current->right;
}
cout << endl;
}
// Clear the entire tree
void clear() {
clearTree(root);
root = nullptr;
}
private:
// Recursive function to delete the tree (post-order deletion)
void clearTree(Node* node) {
if (node == nullptr) return;
clearTree(node->left);
clearTree(node->right);
delete node;
}
};
int main() {
ExpressionTree tree;
char prefixExpression[50];
int choice;
while (true) {
cout << "\n----------------------------" << endl;
cout << "Expression Tree Menu" << endl;
cout << "----------------------------" << endl;
cout << "1. Build Expression Tree" << endl;
cout << "2. Post-order Traversal (Non-recursive)" << endl;
cout << "3. In-order Traversal (Non-recursive)" << endl;
cout << "4. Clear Tree" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the prefix expression: ";
cin.ignore(); // Clear input buffer
cin.getline(prefixExpression, 50);
tree.buildTree(prefixExpression);
break;
case 2:
tree.postOrderTraversal();
break;
case 3:
tree.inOrderTraversal();
break;
case 4:
tree.clear();
cout << "Tree cleared successfully!" << endl;
break;
case 5:
cout << "Exiting program..." << endl;
return 0;
default:
cout << "Invalid choice. Please try again!" << endl;
}
}
return 0;
}