Skip to content

Added Expression Tree Implementation. #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions Trees/Expression_tree/BST.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"BST.h"
#include"Stack.h"

Node* createEXTNode(char data, Node *left, Node *right) {
Node *newNode = malloc(sizeof(Node));
newNode->data = data;
newNode->left = left;
newNode->right = right;
return newNode;
}

int is_operator(char data) {
switch(data) {
case '+':
return 1;
case '-':
return 1;
case '*':
return 1;
case '/':
return 1;
}
return 0;
}

int precedance(char ch) {
if(ch == '+' || ch == '-') {
return 1;
} else if(ch == '*' || ch == '/') {
return 2;
} else if(ch == '^') {
return 3;
} else {
return 0;
}
}

int isAlNum(char ch) {
//48-57 0-9
//65-90 A-Z
//97-122 a -z

if(((int)ch) >= 48 && ((int)ch) <= 57) {
return 1;
}
if(((int)ch) >= 65 && ((int)ch) <= 90) {
return 1;
}
if(((int)ch) >= 97 && ((int)ch) <= 122) {
return 1;
}
return 0;
}

char* convertToPostFix(char* inputString, int size) {
int index = 0;
CharStackList stack;
initCharStack(&stack);

char* postfix = malloc(sizeof(char) * size);
char *temp = postfix;
while(*inputString != '\0') {
if(isAlNum(*inputString)) {
*postfix = *inputString;
postfix++;
} else if(*inputString == '(') {
pushChar(&stack, *inputString);
} else if(*inputString == '^') {
pushChar(&stack, *inputString);
} else if(*inputString == ')') {
while(peekChar(stack) != '(') {
*postfix = peekChar(stack);
postfix++;
popChar(&stack);
}
popChar(&stack);
}
else {
if(precedance(*inputString) > precedance(peekChar(stack))) {
pushChar(&stack, *inputString);
} else {
while(precedance(*inputString) <= precedance(peekChar(stack))) {
*postfix = peekChar(stack);
postfix++;
popChar(&stack);
}
pushChar(&stack, *inputString);
}
}

inputString++;
}

while(peekChar(stack) != '\0') {
*postfix = peekChar(stack);
postfix++;
popChar(&stack);
}
*postfix = '\0';
postfix = temp;

return postfix;
}

void disp(char* str) {
while(*str != '\0') {
printf("%c", *str);
str++;
}
}

void initTree(EXT *tree, char* inputString, int size) {
inputString = convertToPostFix(inputString, size);

int index = 0;
StackList stack;
initStack(&stack);
while(inputString[index] != '\0') {
if(!is_operator(inputString[index])) {
push(&stack, (void *) createEXTNode(inputString[index], NULL, NULL));
} else {
Node* element2 = (Node *)peek(stack);
pop(&stack);

Node* element1 = (Node *)peek(stack);
pop(&stack);

push(&stack, (Node *)createEXTNode(inputString[index], element1, element2));
}
index++;
}

Node* finalTree = (Node *) peek(stack);
pop(&stack);

*tree = finalTree;

}

void traverse(EXT tree) {
if(tree != NULL) {
traverse(tree->left);
printf("%c \n", tree->data);
traverse(tree->right);
}
}

int compute(EXT tree) {
if(tree == NULL)
return 0;

if(tree->left == NULL && tree->right == NULL)
return ((int)tree->data) - 48;

int left = compute(tree->left);
int right = compute(tree->right);

if(tree->data == '+')
return left + right;
if(tree->data == '-')
return left - right;
if(tree->data == '*')
return left * right;
if(tree->data == '/')
return left / right;

return -1;
}


15 changes: 15 additions & 0 deletions Trees/Expression_tree/BST.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
struct Node* left;
struct Node* right;
char data;
}Node;
typedef Node* EXT;

char* convertToPostFix(char* inputString, int size);
void initTree(EXT *tree, char *inputString, int size);
void traverse(EXT tree);
int compute(EXT tree);

17 changes: 17 additions & 0 deletions Trees/Expression_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
CC=gcc
TARGET=main

all: try run
try: main.o BST.o Stack.o
$(CC) main.o BST.o Stack.o -o $(TARGET)
main.o: main.c BST.h Stack.h
$(CC) -c main.c
BST.o: BST.c Stack.c
$(CC) -c BST.c
Stack.o: Stack.c
$(CC) -c Stack.c
run: $(TARGET)
./main
clean:
rm -rvf *.o $(TARGET)

92 changes: 92 additions & 0 deletions Trees/Expression_tree/Stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include<stdio.h>
#include<stdlib.h>
#include"Stack.h"

void initStack(StackList *list) {
*list = NULL;
}

int pop(StackList *list) { // Remove From First
if(*list == NULL) {
return 1; // queue underflow
} else {
StackNode* secondNode = NULL;

if((*list)->next != NULL)
secondNode = (*list)->next;

free(*list);
*list = secondNode;
}
return 0;
}

void push(StackList *list, void *data) { // Insert At First
StackNode* newNode = malloc(sizeof(StackNode));
newNode->next = NULL;
newNode->data = data;

if(newNode == NULL) {
return;
}

if(*list == NULL) {
*list = newNode;
} else {
StackNode *firstNode = *list;
*list = newNode;
(*list)->next = firstNode;
}
}

void* peek(StackList list) {
if(list == NULL)
return NULL;
return list->data;
}




void initCharStack(CharStackList *list) {
*list = NULL;
}

int popChar(CharStackList *list) { // Remove From First
if(*list == NULL) {
return 1; // queue underflow
} else {
CharStackNode* secondNode = NULL;

if((*list)->next != NULL)
secondNode = (*list)->next;

free(*list);
*list = secondNode;
}
return 0;
}

void pushChar(CharStackList *list, char data) { // Insert At First
CharStackNode* newNode = malloc(sizeof(StackNode));
newNode->next = NULL;
newNode->data = data;

if(newNode == NULL) {
return;
}

if(*list == NULL) {
*list = newNode;
} else {
CharStackNode *firstNode = *list;
*list = newNode;
(*list)->next = firstNode;
}
}

char peekChar(CharStackList list) {
if(list == NULL)
return '\0';
return list->data;
}
32 changes: 32 additions & 0 deletions Trees/Expression_tree/Stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<stdio.h>
#include<stdlib.h>

typedef struct StackNode {
struct StackNode* next;
void* data;
}StackNode;
typedef StackNode* StackList;

void initStack(StackList *list);

// To work as a stack
void push(StackList *list, void *data);
int pop(StackList *list);

void* peek(StackList list);


typedef struct CharStackNode {
struct CharStackNode* next;
char data;
}CharStackNode;
typedef CharStackNode* CharStackList;

void initCharStack(CharStackList *list);

// To work as a stack
void pushChar(CharStackList *list, char data);
int popChar(CharStackList *list);

char peekChar(CharStackList list);

21 changes: 21 additions & 0 deletions Trees/Expression_tree/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<stdio.h>
#include <string.h>
#include"BST.h"

int main() {
EXT tree;
char data[100];

printf("\n Enter the input Expression : ");
scanf("%s", data);

initTree(&tree, data, strlen(data));

printf("\n Expression Tree Traversal is : ");
traverse(tree);

printf("\n Computed Result is : %d", compute(tree));

return 0;
}