Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

The expression tree is a binary tree in which each internal node corresponds to the operator and each leaf node corresponds to the operand so for example expression tree for 3 + ((5+9)*2) would be:
Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)Inorder traversal of expression tree produces infix version of given postfix expression (same with postorder traversal it gives postfix expression)

Evaluating the expression represented by an expression tree:
Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Construction of Expression Tree:

Now For constructing an expression tree we use a stack. We loop through input expression and do the following for every character.

  1. If a character is an operand push that into the stack
  2. If a character is an operator pop two values from the stack make them its child and push the current node again.

In the end, the only element of the stack will be the root of an expression tree.

Below is the implementation of the above approach:

  • C++
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Java
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C#
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • JavaScript
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Output
infix expression is
a + b - e * f * g

Evaluation of Expression Tree

Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree.

Examples:

  • Input:
    Root node of the below tree
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Output:
    100
  • Input:
    Root node of the below tree
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)Output:
    110

As all the operators in the tree are binary hence each node will have either 0 or 2 children. As it can be inferred from the examples above , the integer values would appear at the leaf nodes , while the interior nodes represent the operators.
To evaluate the syntax tree , a recursive approach can be followed.

Algorithm

Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

The time complexity would be O(n), as each node is visited once.
Below is a C++ program for the same:

  • C/C++
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Output:

  • 60
  • 110
The document Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

shortcuts and tricks

,

ppt

,

Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

Viva Questions

,

Previous Year Questions with Solutions

,

Objective type Questions

,

MCQs

,

Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

Exam

,

Expression Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

Important questions

,

Semester Notes

,

Summary

,

mock tests for examination

,

Extra Questions

,

practice quizzes

,

past year papers

,

study material

,

Free

,

video lectures

,

pdf

,

Sample Paper

;