Professional Documents
Culture Documents
DS Sem 2 Project
DS Sem 2 Project
(Deemed to be University)
On
SUBMITTED BY:
2200032152 N SIDDARTH
2200032153 P NARESH
2200031776 P JASWANTH
DR . ARUMLLA NAGARAJU
Associate Professor
KL UNIVERSITY
Green fields, Vaddeswaram – 522 302
Guntur Dt., AP, India.
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.
2
ACKNOWLEDGEMENTS
I express the sincere gratitude to our principal Dr. A. Jagadeesh for his
administration towards our academic growth.
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.
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
expression. The left and right children of each node represent the left and
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,
tree based on the values of the nodes, it allows for quick access and
computations.
4
INDEX
S.NO TITLE PAGE NO
1 Introduction 6
4 Algorithm 11
5 Flowchart 12
6 Implementation 13-18
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.
operations in the expression, with the operators at the internal nodes at the
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
Firstly, the tree can be easily traversed in a depth-first manner, allowing for
for easy insertion and removal of nodes, making it easy to modify the
6
2 .AIM OF THE PROJECT
The aim of using an expression tree using BST is to provide an efficient way
expression tree, we can easily traverse the tree and perform the necessary
This makes it easier to evaluate complex expressions, and allows for easy
using BST can also provide a more organized and structured approach to
expressions.
7
2.1 ADVANTAGES
that reflects the order of operations. This allows for efficient evaluation
2.1 DISADVANTAGES
8
2.2.FUTURE IMPLEMENTS
commonly used for arithmetic expressions, they can also be used for
focus on optimizing the memory usage of the data structure while still
Expression trees using BST can be used in combination with other data
expression trees using BST may not handle errors or exceptions that
9
3 .SOFTWARE REQUIRMENTS
HARDWARE REQUIRMENTS
Processor : Intel i5
10
4 .ALGORITHM
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 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;
};
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) {
return 1;
} else {
return 0;
if (isalpha(postfix[i])) {
stack[++top] = newNode(postfix[i]);
} else {
14
struct Node* node = newNode(postfix[i]);
node->left = left;
node->right = right;
stack[++top] = node;
return stack[top];
if (node != NULL) {
if (isOperator(node->data)) {
printf("(");
inorder(node->left);
printf("%c", node->data);
inorder(node->right);
if (isOperator(node->data)) {
printf(")");
15
}
if (node != NULL) {
printf("%c", node->data);
preorder(node->left);
preorder(node->right);
if (node != NULL) {
postorder(node->left);
postorder(node->right);
printf("%c", node->data);
16
}
int main() {
char postfix[100];
scanf("%s", postfix);
int choice;
printf("Choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
inorder(root);
break;
case 2:
17
printf("Preorder expression: ");
preorder(root);
break;
case 3:
postorder(root);
break;
default:
printf("Invalid choice\n");
break;
printf("\n");
return 0;
18
7 . RESULT AND SCREENSHOTS
19
20
8. CONCLUSION
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
at the root of the tree. Then we insert the operands as leaves, in the
we encounter them. This involves visiting each node in the tree exactly
21
22