0% found this document useful (0 votes)
6 views4 pages

Dsa SK Ass4

The document is an assignment submission by Shah Nawaz Khan for a C++ program that evaluates a binary expression tree. It includes a code implementation that defines classes for nodes and the expression tree, and demonstrates how to evaluate the expression represented by the tree. The output of the program is the result of the expression, which is 11 for the given example.

Uploaded by

shahzaib6374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Dsa SK Ass4

The document is an assignment submission by Shah Nawaz Khan for a C++ program that evaluates a binary expression tree. It includes a code implementation that defines classes for nodes and the expression tree, and demonstrates how to evaluate the expression represented by the tree. The output of the program is the result of the expression, which is 11 for the given example.

Uploaded by

shahzaib6374
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DSA

ASSIGNMENT: 04

Name: Shah Nawaz Khan


Enrolment: 01-132222-038
Class: BCE-05(A)
Submitted To: Engr. Muhammad Nauman

Department Of Computer Engineering


Bahria University Islamabad Campus
Question/Task: 01
You are given a binary expression tree where internal nodes are operators (+, -,
*, /) and leaf nodes are operands (integers). Write a C++ program to evaluate the
expression represented by the tree.
Input: A binary tree representing an expression.
+
/ \
* 5
/ \
3 2
Output: 11 (Expression: (3 * 2) + 5 = 11)
Implementation of must utilize classes and functions.

Program:
#include <iostream>
using namespace std;

class Node
{
public:
char data;
Node* left;
Node* right;
Node(char value) : data(value), left(nullptr), right(nullptr) {}
};

class ExpressionTree
{
public:
int evaluate(Node* root)
{
if (!root->left && !root->right)
return root->data - '0';
int leftResult = evaluate(root->left);
int rightResult = evaluate(root->right);

switch (root->data)
{
case '+': return leftResult + rightResult;
case '-': return leftResult - rightResult;
case '*': return leftResult * rightResult;
case '/': return leftResult / rightResult;
default: throw runtime_error("Invalid Operator");
}
}
};

int main()
{
Node* root = new Node('+');
root->left = new Node('*');
root->right = new Node('5');
root->left->left = new Node('3');
root->left->right = new Node('2');
ExpressionTree tree;
cout << "Result: " << tree.evaluate(root) << endl;
delete root->left->left;
delete root->left->right;
delete root->left;
delete root->right;
delete root;
return 0;
}
Output:

Explanation:
Left Subtree: 3 * 2 → 6
Right Subtree: 5
Root Operator (+): 6 + 5 → 11

Reference:
Evaluation of Expression tree:
https://www.geeksforgeeks.org/evaluation-of-expression-tree/

You might also like