-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdsa 10.txt
242 lines (208 loc) · 4.17 KB
/
dsa 10.txt
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
const int MAX = 50;
class node_cls
{
public:
char data;
node_cls *left, *right;
node_cls()
{
left = right = NULL;
}
node_cls(char ch)
{
data = ch;
left = right = NULL;
}
friend class tree_cls;
};
class Data_stek
{
private:
int top;
node_cls *info[MAX];
public:
Data_stek()
{
top = -1;
}
void push(node_cls *cnode_cls)
{
top++;
info[top] = cnode_cls;
}
node_cls *Top()
{
return info[top];
}
node_cls *pop()
{
if (!empty())
return info[top--];
return NULL;
}
bool empty()
{
return (top == -1);
}
bool isFull()
{
return (top == MAX - 1);
}
};
class tree_cls
{
public:
node_cls *root;
tree_cls()
{
root = NULL;
}
void create(string str);
void inorder_rec(node_cls *rnode_cls);
void postorder_rec(node_cls *rnode_cls);
void inorderNonRec();
void postorder();
void inorder();
void deleteTree(node_cls *node);
int priority(char ch);
};
void tree_cls::create(string str)
{
Data_stek sl, s2;
int i = 0;
char ch;
while (str[i] != '\0')
{
ch = str[i];
if (isalpha(ch))
{ // is operand
node_cls *temp = new node_cls(ch);
sl.push(temp);
}
else
{ // operator
if (s2.empty())
{
node_cls *temp = new node_cls(ch);
s2.push(temp);
}
else if (priority(ch) > priority(s2.Top()->data))
{
node_cls *temp = new node_cls(ch);
s2.push(temp);
}
else
{
while (!s2.empty() && priority(ch) <= priority(s2.Top()->data))
{
node_cls *op = s2.pop();
node_cls *rchild = sl.pop();
node_cls *lchild = sl.pop();
op->right = rchild;
op->left = lchild;
sl.push(op);
}
s2.push(new node_cls(ch));
}
}
i++;
}
while (!s2.empty())
{
node_cls *op = s2.pop();
node_cls *rchild = sl.pop();
node_cls *lchild = sl.pop();
op->right = rchild;
op->left = lchild;
sl.push(op);
}
root = sl.pop();
}
int tree_cls::priority(char ch)
{
switch (ch)
{
case '+':
case '-':
return 0;
case '*':
case '/':
return 1;
case '^':
return 2;
default:
return -1;
}
}
void tree_cls::inorderNonRec()
{
node_cls *ptr = root;
Data_stek sl;
while (ptr != NULL || !sl.empty())
{
while (ptr != NULL)
{
sl.push(ptr);
ptr = ptr->left;
}
ptr = sl.pop();
cout << ptr->data << " ";
ptr = ptr->right;
}
}
void tree_cls::inorder_rec(node_cls *rnode_cls)
{
if (rnode_cls)
{
inorder_rec(rnode_cls->left);
cout << rnode_cls->data << " ";
inorder_rec(rnode_cls->right);
}
}
void tree_cls::postorder_rec(node_cls *rnode_cls)
{
if (rnode_cls)
{
postorder_rec(rnode_cls->left);
postorder_rec(rnode_cls->right);
cout << rnode_cls->data << " ";
}
}
void tree_cls::postorder()
{
postorder_rec(root);
}
void tree_cls::inorder()
{
inorder_rec(root);
}
void tree_cls::deleteTree(node_cls *node)
{
if (node == NULL)
return;
deleteTree(node->left);
deleteTree(node->right);
cout << "\n Deleting node: " << node->data;
delete node;
}
int main()
{
tree_cls tl;
string exp = "+--a*bc/def";
cout << "Original Expression: " << exp << endl;
tl.create(exp);
cout << "\nInorder Traversal Recursive: ";
tl.inorder();
cout << "\nInorder Non-Recursive: ";
tl.inorderNonRec();
cout << "\nPostorder Traversal recursive: ";
tl.postorder();
cout << "\nDeleting Tree: ";
tl.deleteTree(tl.root);
cout << "\nEntire tree is deleted..";
return 0;
}