Skip to content

Commit b9a17c2

Browse files
authored
Added (#233)
1 parent b053c06 commit b9a17c2

File tree

6 files changed

+350
-0
lines changed

6 files changed

+350
-0
lines changed

Trees/Expression_tree/BST.c

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
#include<stdlib.h>
4+
#include"BST.h"
5+
#include"Stack.h"
6+
7+
Node* createEXTNode(char data, Node *left, Node *right) {
8+
Node *newNode = malloc(sizeof(Node));
9+
newNode->data = data;
10+
newNode->left = left;
11+
newNode->right = right;
12+
return newNode;
13+
}
14+
15+
int is_operator(char data) {
16+
switch(data) {
17+
case '+':
18+
return 1;
19+
case '-':
20+
return 1;
21+
case '*':
22+
return 1;
23+
case '/':
24+
return 1;
25+
}
26+
return 0;
27+
}
28+
29+
int precedance(char ch) {
30+
if(ch == '+' || ch == '-') {
31+
return 1;
32+
} else if(ch == '*' || ch == '/') {
33+
return 2;
34+
} else if(ch == '^') {
35+
return 3;
36+
} else {
37+
return 0;
38+
}
39+
}
40+
41+
int isAlNum(char ch) {
42+
//48-57 0-9
43+
//65-90 A-Z
44+
//97-122 a -z
45+
46+
if(((int)ch) >= 48 && ((int)ch) <= 57) {
47+
return 1;
48+
}
49+
if(((int)ch) >= 65 && ((int)ch) <= 90) {
50+
return 1;
51+
}
52+
if(((int)ch) >= 97 && ((int)ch) <= 122) {
53+
return 1;
54+
}
55+
return 0;
56+
}
57+
58+
char* convertToPostFix(char* inputString, int size) {
59+
int index = 0;
60+
CharStackList stack;
61+
initCharStack(&stack);
62+
63+
char* postfix = malloc(sizeof(char) * size);
64+
char *temp = postfix;
65+
while(*inputString != '\0') {
66+
if(isAlNum(*inputString)) {
67+
*postfix = *inputString;
68+
postfix++;
69+
} else if(*inputString == '(') {
70+
pushChar(&stack, *inputString);
71+
} else if(*inputString == '^') {
72+
pushChar(&stack, *inputString);
73+
} else if(*inputString == ')') {
74+
while(peekChar(stack) != '(') {
75+
*postfix = peekChar(stack);
76+
postfix++;
77+
popChar(&stack);
78+
}
79+
popChar(&stack);
80+
}
81+
else {
82+
if(precedance(*inputString) > precedance(peekChar(stack))) {
83+
pushChar(&stack, *inputString);
84+
} else {
85+
while(precedance(*inputString) <= precedance(peekChar(stack))) {
86+
*postfix = peekChar(stack);
87+
postfix++;
88+
popChar(&stack);
89+
}
90+
pushChar(&stack, *inputString);
91+
}
92+
}
93+
94+
inputString++;
95+
}
96+
97+
while(peekChar(stack) != '\0') {
98+
*postfix = peekChar(stack);
99+
postfix++;
100+
popChar(&stack);
101+
}
102+
*postfix = '\0';
103+
postfix = temp;
104+
105+
return postfix;
106+
}
107+
108+
void disp(char* str) {
109+
while(*str != '\0') {
110+
printf("%c", *str);
111+
str++;
112+
}
113+
}
114+
115+
void initTree(EXT *tree, char* inputString, int size) {
116+
inputString = convertToPostFix(inputString, size);
117+
118+
int index = 0;
119+
StackList stack;
120+
initStack(&stack);
121+
while(inputString[index] != '\0') {
122+
if(!is_operator(inputString[index])) {
123+
push(&stack, (void *) createEXTNode(inputString[index], NULL, NULL));
124+
} else {
125+
Node* element2 = (Node *)peek(stack);
126+
pop(&stack);
127+
128+
Node* element1 = (Node *)peek(stack);
129+
pop(&stack);
130+
131+
push(&stack, (Node *)createEXTNode(inputString[index], element1, element2));
132+
}
133+
index++;
134+
}
135+
136+
Node* finalTree = (Node *) peek(stack);
137+
pop(&stack);
138+
139+
*tree = finalTree;
140+
141+
}
142+
143+
void traverse(EXT tree) {
144+
if(tree != NULL) {
145+
traverse(tree->left);
146+
printf("%c \n", tree->data);
147+
traverse(tree->right);
148+
}
149+
}
150+
151+
int compute(EXT tree) {
152+
if(tree == NULL)
153+
return 0;
154+
155+
if(tree->left == NULL && tree->right == NULL)
156+
return ((int)tree->data) - 48;
157+
158+
int left = compute(tree->left);
159+
int right = compute(tree->right);
160+
161+
if(tree->data == '+')
162+
return left + right;
163+
if(tree->data == '-')
164+
return left - right;
165+
if(tree->data == '*')
166+
return left * right;
167+
if(tree->data == '/')
168+
return left / right;
169+
170+
return -1;
171+
}
172+
173+

Trees/Expression_tree/BST.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
typedef struct Node {
5+
struct Node* left;
6+
struct Node* right;
7+
char data;
8+
}Node;
9+
typedef Node* EXT;
10+
11+
char* convertToPostFix(char* inputString, int size);
12+
void initTree(EXT *tree, char *inputString, int size);
13+
void traverse(EXT tree);
14+
int compute(EXT tree);
15+

Trees/Expression_tree/Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CC=gcc
2+
TARGET=main
3+
4+
all: try run
5+
try: main.o BST.o Stack.o
6+
$(CC) main.o BST.o Stack.o -o $(TARGET)
7+
main.o: main.c BST.h Stack.h
8+
$(CC) -c main.c
9+
BST.o: BST.c Stack.c
10+
$(CC) -c BST.c
11+
Stack.o: Stack.c
12+
$(CC) -c Stack.c
13+
run: $(TARGET)
14+
./main
15+
clean:
16+
rm -rvf *.o $(TARGET)
17+

Trees/Expression_tree/Stack.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include"Stack.h"
4+
5+
void initStack(StackList *list) {
6+
*list = NULL;
7+
}
8+
9+
int pop(StackList *list) { // Remove From First
10+
if(*list == NULL) {
11+
return 1; // queue underflow
12+
} else {
13+
StackNode* secondNode = NULL;
14+
15+
if((*list)->next != NULL)
16+
secondNode = (*list)->next;
17+
18+
free(*list);
19+
*list = secondNode;
20+
}
21+
return 0;
22+
}
23+
24+
void push(StackList *list, void *data) { // Insert At First
25+
StackNode* newNode = malloc(sizeof(StackNode));
26+
newNode->next = NULL;
27+
newNode->data = data;
28+
29+
if(newNode == NULL) {
30+
return;
31+
}
32+
33+
if(*list == NULL) {
34+
*list = newNode;
35+
} else {
36+
StackNode *firstNode = *list;
37+
*list = newNode;
38+
(*list)->next = firstNode;
39+
}
40+
}
41+
42+
void* peek(StackList list) {
43+
if(list == NULL)
44+
return NULL;
45+
return list->data;
46+
}
47+
48+
49+
50+
51+
void initCharStack(CharStackList *list) {
52+
*list = NULL;
53+
}
54+
55+
int popChar(CharStackList *list) { // Remove From First
56+
if(*list == NULL) {
57+
return 1; // queue underflow
58+
} else {
59+
CharStackNode* secondNode = NULL;
60+
61+
if((*list)->next != NULL)
62+
secondNode = (*list)->next;
63+
64+
free(*list);
65+
*list = secondNode;
66+
}
67+
return 0;
68+
}
69+
70+
void pushChar(CharStackList *list, char data) { // Insert At First
71+
CharStackNode* newNode = malloc(sizeof(StackNode));
72+
newNode->next = NULL;
73+
newNode->data = data;
74+
75+
if(newNode == NULL) {
76+
return;
77+
}
78+
79+
if(*list == NULL) {
80+
*list = newNode;
81+
} else {
82+
CharStackNode *firstNode = *list;
83+
*list = newNode;
84+
(*list)->next = firstNode;
85+
}
86+
}
87+
88+
char peekChar(CharStackList list) {
89+
if(list == NULL)
90+
return '\0';
91+
return list->data;
92+
}

Trees/Expression_tree/Stack.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
typedef struct StackNode {
5+
struct StackNode* next;
6+
void* data;
7+
}StackNode;
8+
typedef StackNode* StackList;
9+
10+
void initStack(StackList *list);
11+
12+
// To work as a stack
13+
void push(StackList *list, void *data);
14+
int pop(StackList *list);
15+
16+
void* peek(StackList list);
17+
18+
19+
typedef struct CharStackNode {
20+
struct CharStackNode* next;
21+
char data;
22+
}CharStackNode;
23+
typedef CharStackNode* CharStackList;
24+
25+
void initCharStack(CharStackList *list);
26+
27+
// To work as a stack
28+
void pushChar(CharStackList *list, char data);
29+
int popChar(CharStackList *list);
30+
31+
char peekChar(CharStackList list);
32+

Trees/Expression_tree/main.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include<stdio.h>
2+
#include <string.h>
3+
#include"BST.h"
4+
5+
int main() {
6+
EXT tree;
7+
char data[100];
8+
9+
printf("\n Enter the input Expression : ");
10+
scanf("%s", data);
11+
12+
initTree(&tree, data, strlen(data));
13+
14+
printf("\n Expression Tree Traversal is : ");
15+
traverse(tree);
16+
17+
printf("\n Computed Result is : %d", compute(tree));
18+
19+
return 0;
20+
}
21+

0 commit comments

Comments
 (0)