You are on page 1of 22

Koneru Lakshmaiah Education Foundation

(Deemed to be University)

FRESHMAN ENGINEERING DEPARTMENT


A Project Based Lab Report

On

EXPRESSION TREE USING BINARY SEARCH TREE

SUBMITTED BY:

I.D NUMBER NAME

2200032152 N SIDDARTH

2200032153 P NARESH

2200031776 P JASWANTH

UNDER THE GUIDANCE OF

DR . ARUMLLA NAGARAJU

Associate Professor

KL UNIVERSITY
Green fields, Vaddeswaram – 522 302
Guntur Dt., AP, India.

DEPARTMENT OF BASIC ENGINEERING SCIENCES-1

1
CERTIFICATE
This is to certify that the project based laboratory report entitled
“EXPRESSION TREE USING BINARY SEARCH TREE ” submitted by
Ms.N SIDDARTH bearing redg no. 2200032152 , Mr.P NARESH bearing
Regd. no. 2200032153, Mr. P JASWANTH bearing Regd no. 22000321776
Department of Basic Engineering Sciences-1, KL University in partial
fulfillment of the requirements for the completion of a project based
Laboratory in “DATA STRUCTURES ”course in I year B Tech 2 Semester, is
a bonafide record of the work carried out by him/her under my
supervision during the academic year 2022 – 2023.

PROJECT SUPERVISOR HEAD OF THE DEPARTMENT

DR. ARUMLLA NAGARAJU Dr.D.Haritha

2
ACKNOWLEDGEMENTS

It is great pleasure for me to express my gratitude to our honorable President


Sri. Koneru Satyanarayana, for giving the opportunity and platform with
facilities in accomplishing the project based laboratory report.

I express the sincere gratitude to our principal Dr. A. Jagadeesh for his
administration towards our academic growth.

I express sincere gratitude to HOD-BES-1Dr. D. Harithafor her leadership and


constant motivation provided in successful completion of our academic semester.
I record it as my privilege to deeply thank for providing us the efficient faculty
and facilities to make our ideas into reality.

I express my sincere thanks to our project supervisor Dr.A NAGARAJU sir for his
novel association of ideas, encouragement, appreciation and intellectual zeal
which motivated us to venture this project successfully.

Finally, it is pleased to acknowledge the indebtedness to all those who devoted


themselves directly or indirectly to make this project report success.

Reg.Id NAME

2200032152 N SIDDARTH

2200032153 P NARESH

2200031776 P JASWANTH

3
ABSTRACT
An expression tree is a binary tree used to represent arithmetic

expressions in a way that allows for efficient evaluation. One common way

to implement an expression tree is to use a Binary Search Tree (BST),

where each node in the tree represents an operand or operator in the

expression. The left and right children of each node represent the left and

right operands of the operator. Using a BST to represent an expression tree

allows for easy evaluation of the expression, as the tree can be traversed in

a depth-first manner, evaluating the left and right subtrees recursively and

combining the results with the current operator at each node. In summary,

an expression tree using a BST is a data structure that provides an efficient

way to represent and evaluate arithmetic expressions. By organizing the

tree based on the values of the nodes, it allows for quick access and

manipulation of the expression, making it a useful tool for mathematical

computations.

4
INDEX
S.NO TITLE PAGE NO

1 Introduction 6

2 Aim of the Project 7

2.1 Advantages Disadvantages 8

2.2 Future Implementation 9

3 Software & Hardwar Details 10

4 Algorithm 11

5 Flowchart 12

6 Implementation 13-18

7 Results and Screenshots 19-20

8 Conclusion 21

5
1. INTRODUCTION
An expression tree is a type of binary tree used to represent arithmetic

expressions, where the operands and operators are the nodes of the tree.

The tree structure is constructed in a way that reflects the order of

operations in the expression, with the operators at the internal nodes at the

operands at the leaf nodes. One common way to implement an expression

tree is by using a Binary Search Tree (BST), where the left child of a node

contains a smaller value and the right child contains a larger value. In this

case, the nodes of the BST represent the operators and operands of the

expression, and the tree is constructed by inserting nodes based on their

values. Using a BST to represent an expression tree has several advantages.

Firstly, the tree can be easily traversed in a depth-first manner, allowing for

efficient evaluation of the expression. Additionally, the use of a BST allows

for easy insertion and removal of nodes, making it easy to modify the

expression Overall, an expression tree using a BST provides a powerful

data structure for representing and manipulating arithmetic expressions

,making it a useful tool for mathematical computations.

6
2 .AIM OF THE PROJECT

The aim of using an expression tree using BST is to provide an efficient way

to represent arithmetic expressions and allow for easy evaluation of the

expression. By using a binary search tree to organize the nodes of the

expression tree, we can easily traverse the tree and perform the necessary

calculations, while maintaining the order of operations of the expression.

This makes it easier to evaluate complex expressions, and allows for easy

modification of the expression if needed. The use of an expression tree

using BST can also provide a more organized and structured approach to

representing arithmetic expressions, making it a useful tool for

mathematical computations. Overall, the aim is to provide an efficient and

flexible data structure for representing and manipulating arithmetic

expressions.

7
2.1 ADVANTAGES

Efficient evaluation: By using an expression tree, we can easily traverse

the tree in a depth-first manner and evaluate the expression in a way

that reflects the order of operations. This allows for efficient evaluation

of complex expressions .Flexibility the use of a BST allows for easy

insertion and removal of nodes, making it easy to modify the expression

as needed. This provides flexibility in working with arithmetic

expressions. Organized representation: The expression tree provides a

structured and organized way to represent arithmetic expressions,

making it easier to understand and work with. Easy to implement: The

implementation of an expression tree using BST is relatively simple and

straightforward, making it easy to create and work with.

2.1 DISADVANTAGES

Limited applicability: The use of an expression tree using BST is

primarily limited to arithmetic expressions, and may not be suitable for

other types of data.Memory consumption: Depending on the size of the

expression, the use of an expression tree may require a significant

amount of memory.Complexity of implementation: While the

implementation of an expression tree using BST is generally

straightforward, more complex operations such as variable assignments

or function calls may require additional work.

8
2.2.FUTURE IMPLEMENTS

Support for more complex operations: While expression trees are

commonly used for arithmetic expressions, they can also be used for

other types of expressions, such as logical or 9oolean expressions. Future

implementations could expand the functionality of expression trees

using BST to support these types of expressions. Optimization for

memory usage: As mentioned before, the use of expression trees can

require a significant amount of memory. Future implementations could

focus on optimizing the memory usage of the data structure while still

maintaining its functionality. Integration with other data structures:

Expression trees using BST can be used in combination with other data

structures to perform more complex computations. Future

implementations could focus on integrating expression trees with other

data structures, such as stacks or queues, to create more powerful

computational tools. Error handling: Current implementations of

expression trees using BST may not handle errors or exceptions that

occur during evaluation of an expression. Future implementations could

include more robust error handling capabilities to improve the reliability

of the data structure. Visualization: Expression trees can be difficult to

visualize, especially for large or complex expressions. Future

9
3 .SOFTWARE REQUIRMENTS

The major software requirements of the project are as follows:


Language : C
Operating system: Windows 11

HARDWARE REQUIRMENTS

The hardware requirements that map towards the software are as

follows: RAM 1GB or more

Processor : Intel i5

Hard disk Capacity: 1 TB

10
4 .ALGORITHM

STEP 1: Convert the infix expression to postfix notation.

STEP 2: Create an empty BST.

STEP 3: Traverse the postfix expression from left to right.

STEP 4: If the current symbol is an operand, create a new node with

the value of the operand and insert it into the BST.

STEP 5: If the current symbol is an operator, create a new node with

the value of the operator and insert it into the BST.

STEP 6: Set the right child of the new node to the node at the top of

the stack.

STEP 7: Pop the top node from the stack and set its left child to the

new node.

STEP 8: Push the new node onto the stack.

STEP 9: When the entire postfix expression has been traversed, the

top node on the stack will be the root of the expression tree.

11
5. FLOWCHART

12
6. IMPLEMENTATION OF CODE
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

struct Node {

char data;

struct Node* left;

struct Node* right;

};

// Function to create a new node with the given data

struct Node* newNode(char data) {

struct Node* node = (struct Node*) malloc(sizeof(struct Node));

node->data = data;

node->left = NULL;

node->right = NULL;

return node;

13
// Function to check if a given character is an operator

int isOperator(char c) {

if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^') {

return 1;

} else {

return 0;

// Function to construct an expression tree from a postfix expression

struct Node* constructTree(char postfix[]) {

struct Node* stack[100];

int top = -1;

for (int i = 0; postfix[i]; ++i) {

if (isalpha(postfix[i])) {

stack[++top] = newNode(postfix[i]);

} else {

struct Node* right = stack[top--];

struct Node* left = stack[top--];

14
struct Node* node = newNode(postfix[i]);

node->left = left;

node->right = right;

stack[++top] = node;

return stack[top];

// Function to derive the infix expression from an expression tree

void inorder(struct Node* node) {

if (node != NULL) {

if (isOperator(node->data)) {

printf("(");

inorder(node->left);

printf("%c", node->data);

inorder(node->right);

if (isOperator(node->data)) {

printf(")");

15
}

// Function to derive the prefix expression from an expression tree

void preorder(struct Node* node) {

if (node != NULL) {

printf("%c", node->data);

preorder(node->left);

preorder(node->right);

// Function to derive the postfix expression from an expression tree

void postorder(struct Node* node) {

if (node != NULL) {

postorder(node->left);

postorder(node->right);

printf("%c", node->data);

16
}

int main() {

char postfix[100];

printf("Enter a postfix expression: ");

scanf("%s", postfix);

struct Node* root = constructTree(postfix);

printf("Enter your choice:\n");

printf("1. Inorder expression\n");

printf("2. Preorder expression\n");

printf("3. Postorder expression\n");

int choice;

printf("Choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Inorder expression: ");

inorder(root);

break;

case 2:

17
printf("Preorder expression: ");

preorder(root);

break;

case 3:

printf("Postorder expression: ");

postorder(root);

break;

default:

printf("Invalid choice\n");

break;

printf("\n");

return 0;

18
7 . RESULT AND SCREENSHOTS

19
20
8. CONCLUSION

A binary search tree (BST) can be used to represent an expression tree.

In an expression tree, the nodes represent operands or operators, and

the leaves represent operands. The root node represents the operator at

the top of the tree, and its children represent the operands.To build an

expression tree using a BST, we start by inserting the operators into the

BST in order of their precedence, with the highest precedence operator

at the root of the tree. Then we insert the operands as leaves, in the

order they appear in the expression.To evaluate the expression

represented by the expression tree, we traverse the tree in postfix order

(also known as reverse polish notation) and perform the operations as

we encounter them. This involves visiting each node in the tree exactly

once, so the time complexity of evaluating the expression is O(n), where

n is the number of nodes in the tree.Overall, using a BST to represent an

expression tree can be an efficient way to evaluate expressions,

especially if we need to evaluate the same expression multiple times.

However, building the tree initially can be time-consuming, and the

space complexity of the tree can be relatively high, depending on the

complexity of the expression.

21
22

You might also like