0% found this document useful (0 votes)
33 views8 pages

Assignment 3

The document contains C++ code to: 1) Define classes for nodes and stacks to represent trees and evaluate expressions. 2) Convert infix expressions to postfix and evaluate postfix expressions. 3) Construct expression trees from postfix expressions and find the prefix expression. 4) Main function tests converting an infix expression to postfix, evaluating postfix, constructing the expression tree, and finding the prefix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views8 pages

Assignment 3

The document contains C++ code to: 1) Define classes for nodes and stacks to represent trees and evaluate expressions. 2) Convert infix expressions to postfix and evaluate postfix expressions. 3) Construct expression trees from postfix expressions and find the prefix expression. 4) Main function tests converting an infix expression to postfix, evaluating postfix, constructing the expression tree, and finding the prefix expression.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT 3 :

CODE:

#include <iostream>

#include <stack>

#include <string>

using namespace std;

template <typename T>

class Node {

public:

T data;

Node* next;

Node(T value)

this->data=value;

this->next=NULL;

};

template <typename T>

class Stack {

private:

Node<T>* top;

public:

Stack();

~Stack();

void push(T x);

T pop();
T peek();

bool isEmpty();

};

template <typename T>

//Stack<T>::Stack() : top(NULL) {}

Stack<T>::Stack()

top=NULL;

template <typename T>

Stack<T>::~Stack() {

Node<T>* p = top;

while (top) {

top = top->next;

delete p;

p = top;

template <typename T>

void Stack<T>::push(T x) {

Node<T>* t = new Node<T>(x);

t->next = top;

top = t;

template <typename T>

T Stack<T>::pop() {
if (isEmpty()) {

cerr << "Stack Underflow!" << endl;

return T(); // Return default value for simplicity; you may handle underflow differently

Node<T>* p = top;

T x = p->data;

top = top->next;

delete p;

return x;

template <typename T>

T Stack<T>::peek() {

if (isEmpty()) {

cerr << "Stack is empty!" << endl;

return T(); // Return default value for simplicity; you may handle empty stack differently

return top->data;

template <typename T>

bool Stack<T>::isEmpty() {

return top == NULL;

int precedence(char op) {

if (op == '+' || op == '-') {

return 1;

} else if (op == '*' || op == '/') {

return 2;

}
return 0;

bool isOperand(char c) {

return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');

string infixToPostfix(const string& infix) {

string postfix = "";

Stack<char> operatorStack;

for (int i = 0; i < infix.size(); ++i) {

char c = infix[i];

if (isOperand(c)) {

postfix += c;

} else if (c == '(') {

operatorStack.push(c);

} else if (c == ')') {

while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {

postfix += operatorStack.pop();

operatorStack.pop(); // Pop '('

} else {

while (!operatorStack.isEmpty() && precedence(c) <= precedence(operatorStack.peek())) {

postfix += operatorStack.pop();

operatorStack.push(c);

while (!operatorStack.isEmpty()) {
postfix += operatorStack.pop();

return postfix;

int evaluatePostfix(const string& postfix) {

Stack<int> operandStack;

for (int i = 0; i < postfix.size(); ++i) {

char c = postfix[i];

if (isOperand(c)) {

operandStack.push(c - '0');

} else {

int operand2 = operandStack.pop();

int operand1 = operandStack.pop();

switch (c) {

case '+': operandStack.push(operand1 + operand2); break;

case '-': operandStack.push(operand1 - operand2); break;

case '*': operandStack.push(operand1 * operand2); break;

case '/': operandStack.push(operand1 / operand2); break;

default: break;

return operandStack.pop();

class TreeNode {
public:

char data;

TreeNode* left;

TreeNode* right;

//TreeNode(char value) : data(value), left(NULL), right(NULL) {}

TreeNode(char value)

data=value;

left=NULL;

right=NULL;

};

TreeNode* constructExpressionTree(const string& postfix) {

Stack<TreeNode*> treeStack;

for (int i = 0; i < postfix.size(); ++i) {

char c = postfix[i];

TreeNode* node = new TreeNode(c);

if (isOperand(c)) {

treeStack.push(node);

} else {

TreeNode* operand2 = treeStack.pop();

TreeNode* operand1 = treeStack.pop();

node->right = operand2;

node->left = operand1;

treeStack.push(node);

}
return treeStack.pop();

void prefixFromTree(TreeNode* root, string& result) {

if (root) {

result += root->data;

prefixFromTree(root->left, result);

prefixFromTree(root->right, result);

string findPrefixFromTree(TreeNode* root) {

string result = "";

prefixFromTree(root, result);

return result;

int main() {

string infixExpression = "5*9+6/2";

string postfixExpression = infixToPostfix(infixExpression);

cout << "Postfix Expression: " << postfixExpression << endl;

int resultPostfix = evaluatePostfix(postfixExpression);

cout << "Result of Postfix Expression: " << resultPostfix << endl;

TreeNode* expressionTree = constructExpressionTree(postfixExpression);

string prefixFromTreeResult = findPrefixFromTree(expressionTree);

cout << "Prefix Expression from Tree: " << prefixFromTreeResult << endl;
return 0;

OUTPUT :

You might also like