You are on page 1of 15

K L UNIVERSITY

FRESHMAN ENGINEERING DEPARTMENT


A Project Based Lab Report

On

<TITLE>

SUBMITTED BY:

I.D NUMBER NAME

<190030159> <B.VINAY>

<190030150> <B.POOJITHA>

<190031222> <B.UPENDRA>

<190030201> <B.SASI VARDHAN>

UNDER THE ESTEEMED GUIDANCE OF

Sri N.Sreeram

ASSOCIATE PROFESSOR

KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
Guntur Dt., AP, India.
DEPARTMENT OF BASIC ENGINEERING SCIENCES

CERTIFICATE

This is to certify that the project based laboratory report entitled


“<TITLE>” submitted by Mr B.Vinay Datta, Ms B.Poojitha Reddy, Mr
B.Upendra, Mr.B.SasiVardhan bearing Regd. No.
190030159 ,190030150 ,190030222 ,190030201 to the Department of Basic
Engineering Sciences, KL University in partial fulfillment of the requirements
for the completion of a project in “Object Oriented Programming” course in I B
Tech II Semester, is a bonafide record of the work carried out by him/her under
my supervision during the academic year 2019-20.

PROJECT SUPERVISOR HEAD OF THE DEPARTMENT

Sri N.Sreeram Dr. D.HARITHA


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 director Dr. A Jagadeesh for his
administration towards our academic growth.

I express sincere gratitude to our Coordinator and HOD-BES Dr. D.Haritha


for 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 <name> for his/her


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.

ID NO Name Of The Student

190030159 B.Vinay Datta

190030150 B.Poojitha Reddy

190030201 B.SasiVardhan

190031222 B.Upendra
ABSTRACT
This program reads standard expressions typed in by
the user.
The program constructs an expression tree to
represent the
expression. It then prints the value of the
tree. It also uses
the tree to print out a list of commands that
could be used
on a stack machine to evaluate the expression.
The expressions can use positive real numbers and
the binary operators +, -, *, and /. The unary
minus operation
is supported. The expressions are defined by the
BNF rules:

<expression> ::= [ "-" ] <term> [ [ "+"


| "-" ] <term> ]...

<term> ::= <factor> [ [ "*" | "/" ]


<factor> ]...

<factor> ::= <number> | "("


<expression> ")"

A number must begin with a digit (i.e., not a


decimal point).
A line of input must contain exactly one such
expression. If extra
data is found on a line after an expression has
been read, it is
considered an error.
INDEX
S.NO TITLE PAGE NO
1 Introduction <6>
2 Aim of the Project <7>
2.1 Advantages & Disadvantages <7>
2.2 Future Implementation <7>
3 Software & Hardware Details <8>
4 Class Diagram <9>
5 Implementation <10--12>
6 Outputs/ScreenShots <12--13>

7 Conclusion <14>

INTRODUCTION

An expression tree is a binary tree in which each


non-leaf node contains a binary operator and
each leaf node contains a number or a variable.
The tree represents an algebraic expression. At
each non-leaf node, the left and right subtrees
themselves represent algebraic expressions. The
left subtree represents the left operand of the
operator at the node and the right subtree
represents its right operand. The structure of the
tree reflects the order of operation.
AIM OF THE PROJECT
TO IMPLEMENT THE EXPRESSION TREE

Advantages:-

 1. Trees reflect structural relationships in the data.



 2. Trees are used to represent hierarchies.

 3.Trees provide an efficient insertion and searching.

 4.Trees are very flexible data, allowing to move subtrees around
with minimum effort.

Disadvantages:-

1.Tough Implementation Of Balanced BST

2.access time is O(log n)


Future enhancements:-

We can further improve it by making the program, by which we can


add some more elements in the programme and by adding pictures.

SYSTEM REQUIREMENTS

 SOFTWARE REQUIREMENTS:
The major software requirements of the project are as follows:
Language : JAVA
Operating system: Windows 10 or later.

 HARDWARE REQUIREMENTS:
The hardware requirements that map towards the software are as follows:

RAM : 1GB or more

Processor : Intel Pentium or Later

Hard disk Capacity: 40GB or more


CLASS DIAGRAM
IMPLEMENTATION
package dsproject1;

import java.util.Scanner;
public class ExpressionTree {
class TreeNode
{
char data;
TreeNode left, right;

/** constructor **/


public TreeNode(char data)
{
this.data = data;
this.left = null;
this.right = null;
}
}

/** class StackNode **/


class StackNode
{
TreeNode treeNode;
StackNode next;

/** constructor **/


public StackNode(TreeNode treeNode)
{
this.treeNode = treeNode;
next = null;
}
}

private static StackNode top;

/** constructor **/


public ExpressionTree()
{
top = null;
}

/** function to clear tree **/


public void clear()
{
top = null;
}

/** function to push a node **/


private void push(TreeNode ptr)
{
if (top == null)
top = new StackNode(ptr);
else
{
StackNode nptr = new StackNode(ptr);
nptr.next = top;
top = nptr;
}
}

/** function to pop a node **/


private TreeNode pop()
{
if (top == null)
throw new RuntimeException("Underflow");
else
{
TreeNode ptr = top.treeNode;
top = top.next;
return ptr;
}
}

/** function to get top node **/


private TreeNode peek()
{
return top.treeNode;
}

/** function to insert character **/


private void insert(char val)
{
try
{
if (isDigit(val))
{
TreeNode nptr = new TreeNode(val);
push(nptr);
}
else if (isOperator(val))
{
TreeNode nptr = new TreeNode(val);
nptr.left = pop();
nptr.right = pop();
push(nptr);
}
}
catch (Exception e)
{
System.out.println("Invalid Expression");
}
}

/** function to check if digit **/


private boolean isDigit(char ch)
{
return ch >= '0' && ch <= '9';
}

/** function to check if operator **/


private boolean isOperator(char ch)
{
return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}

/** function to convert character to digit **/


private int toDigit(char ch)
{
return ch - '0';
}

/** function to build tree from input */


public void buildTree(String eqn)
{
for (int i = eqn.length() - 1; i >= 0; i--)
insert(eqn.charAt(i));
}

/** function to evaluate tree */


public double evaluate()
{
return evaluate(peek());
}

/** function to evaluate tree */


public double evaluate(TreeNode ptr)
{
if (ptr.left == null && ptr.right == null)
return toDigit(ptr.data);
else
{
double result = 0.0;
double left = evaluate(ptr.left);
double right = evaluate(ptr.right);
char operator = ptr.data;

switch (operator)
{
case '+' : result = left + right; break;
case '-' : result = left - right; break;
case '*' : result = left * right; break;
case '/' : result = left / right; break;
default : result = left + right; break;
}
return result;
}
}

/** function to get postfix expression */


public void postfix()
{
postOrder(peek());
}

/** post order traversal */


private void postOrder(TreeNode ptr)
{
if (ptr != null)
{
postOrder(ptr.left);
postOrder(ptr.right);
System.out.print(ptr.data);
}
}

/** function to get infix expression */


public void infix()
{
inOrder(peek());
}

/** in order traversal */


private void inOrder(TreeNode ptr)
{
if (ptr != null)
{
inOrder(ptr.left);
System.out.print(ptr.data);
inOrder(ptr.right);
}
}

/** function to get prefix expression */


public void prefix()
{
preOrder(peek());
}

/** pre order traversal */


private void preOrder(TreeNode ptr)
{
if (ptr != null)
{
System.out.print(ptr.data);
preOrder(ptr.left);
preOrder(ptr.right);
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Expression Tree Test");

/** make object of ExpressionTree **/


ExpressionTree et = new ExpressionTree();

System.out.println("\nEnter equation in prefix form");


et.buildTree(scan.next());

System.out.print("\nPrefix : ");
et.prefix();
System.out.print("\n\nInfix : ");
et.infix();
System.out.print("\n\nPostfix : ");
et.postfix();
System.out.println("\n\nEvaluated Result : "+ et.evaluate());
}
}
OUTPUTS
Screen Shots:
CONCLUSION

Our project and implementation are on supermarket


Billing System. We have successfully completed it. We take
this opportunity to express us since of indebtedness and
gratitude to all those people who helped us in completing
this project and implementation
We are immensely grateful to our esteemed project guide
Sri N. Sreeram guidance without which this work would
not have been possible. This project and implementation
have contributed a lot to our knowledge that has proved to
be a value addition for us

You might also like