-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressiontree.c
More file actions
126 lines (109 loc) · 2.96 KB
/
expressiontree.c
File metadata and controls
126 lines (109 loc) · 2.96 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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
struct Node {
char data;
struct Node *left, *right;
};
struct Node* newNode(char data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->left = node->right = NULL;
return node;
}
int precedence(char op) {
if (op == '^') return 3;
if (op == '*' || op == '/') return 2;
if (op == '+' || op == '-') return 1;
return 0;
}
int isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^';
}
// Stack for operators (char)
char opStack[MAX];
int opTop = -1;
void pushOp(char c) { opStack[++opTop] = c; }
char popOp() { return opStack[opTop--]; }
char peekOp() { return opStack[opTop]; }
int isEmptyOp() { return opTop == -1; }
// Stack for tree nodes
struct Node* nodeStack[MAX];
int nodeTop = -1;
void pushNode(struct Node* n) { nodeStack[++nodeTop] = n; }
struct Node* popNode() { return nodeStack[nodeTop--]; }
// Convert infix to postfix
void infixToPostfix(char* infix, char* postfix) {
int j = 0;
for (int i = 0; infix[i]; i++) {
char c = infix[i];
if (isalnum(c)) {
postfix[j++] = c;
} else if (c == '(') {
pushOp(c);
} else if (c == ')') {
while (!isEmptyOp() && peekOp() != '(') {
postfix[j++] = popOp();
}
popOp(); // remove '('
} else if (isOperator(c)) {
while (!isEmptyOp() && precedence(peekOp()) >= precedence(c)) {
postfix[j++] = popOp();
}
pushOp(c);
}
}
while (!isEmptyOp()) {
postfix[j++] = popOp();
}
postfix[j] = '\0';
}
// Build expression tree from postfix
struct Node* buildTreeFromPostfix(char* postfix) {
for (int i = 0; postfix[i]; i++) {
char c = postfix[i];
if (isalnum(c)) {
pushNode(newNode(c));
} else if (isOperator(c)) {
struct Node* right = popNode();
struct Node* left = popNode();
struct Node* opNode = newNode(c);
opNode->left = left;
opNode->right = right;
pushNode(opNode);
}
}
return popNode();
}
// Preorder traversal (Prefix)
void preorder(struct Node* root) {
if (root) {
printf("%c", root->data);
preorder(root->left);
preorder(root->right);
}
}
// Postorder traversal (Postfix)
void postorder(struct Node* root) {
if (root) {
postorder(root->left);
postorder(root->right);
printf("%c", root->data);
}
}
int main() {
char infix[MAX], postfix[MAX];
printf("Enter infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
struct Node* root = buildTreeFromPostfix(postfix);
printf("Prefix expression: ");
preorder(root);
printf("\n");
printf("Postfix expression: ");
postorder(root);
printf("\n");
return 0;
}