0% found this document useful (0 votes)
2K views

Binary Expression Trees

Binary expression trees are a type of binary tree used to represent mathematical expressions. Each leaf node contains an operand, each internal node contains an operator, and the tree shows the order of operations with subtrees evaluated before their parent node. The document provides examples of binary expression trees, discusses how they indicate operator precedence through node depth, and how they can be traversed to generate infix, prefix, and postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Binary Expression Trees

Binary expression trees are a type of binary tree used to represent mathematical expressions. Each leaf node contains an operand, each internal node contains an operator, and the tree shows the order of operations with subtrees evaluated before their parent node. The document provides examples of binary expression trees, discusses how they indicate operator precedence through node depth, and how they can be traversed to generate infix, prefix, and postfix expressions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

An application of binary trees:

Binary Expression Trees

CS308
Data Structures
1

A Binary Expression Tree is . . .


A special kind of binary tree in which:
1. Each leaf node contains a single operand
2. Each nonleaf node contains a single binary
operator
3. The left and right subtrees of an operator
node represent subexpressions that must be
evaluated before applying the operator at
the root of the subtree.
2

A Four-Level Binary Expression

-
8

+
4

2
3

Levels Indicate Precedence


The levels of the nodes in the tree indicate
their relative precedence of evaluation
(we do not need parentheses to indicate precedence).

Operations at higher levels of the tree are


evaluated later than those below them.
The operation at the root is always the last
operation performed.

A Binary Expression Tree


*

+
4

What value does it have?


( 4 + 2 ) * 3 = 18
5

Easy to generate the infix, prefix,


postfix expressions (how?)
*
/

-
8

+
4

Infix:

((8-5)*((4+2)/3))

Prefix:

*-85 /+423

Postfix:

85- 42+3/*

Inorder Traversal: (A + H) / (M - Y)
Print second

tree
/

+
A

Print left subtree first

Print right subtree last


7

Preorder Traversal: / + A H - M Y
Print first

tree
/

+
A

Print left subtree second

Print right subtree last


8

Postorder Traversal: A H + M Y - /
Print last

tree
/

+
A

Print left subtree first

Print right subtree second


9

class ExprTree

ExprTree
~ExprTree
Build
Evaluate
.
.
.

*
private:
TreeNode*
root;

+
4

10

Each node contains two pointers


struct TreeNode
{
InfoNode info ;
TreeNode* left ;
TreeNode* right ;
};

NULL

// Data member
// Pointer to left child
// Pointer to right child

OPERAND
. whichType

. left

. info

6000

. operand

. right

11

InfoNode has 2 forms


enum OpType { OPERATOR, OPERAND } ;
struct InfoNode
{
OpType
union
{
char
int
}

whichType;
// ANONYMOUS union
operation ;
operand ;

};

OPERATOR
. whichType

+
. operation

OPERAND
. whichType

. operand
12

int Eval ( TreeNode* ptr )


{

switch ( ptr->info.whichType )
{
case OPERAND : return ptr->info.operand ;
case OPERATOR :
switch ( tree->info.operation )
{
case + : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ;
case - : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ;
case * : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ;
case / : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ;
}
}

}
13

Building a Binary Expression Tree from an


expression in prefix notation

Insert new nodes, each time moving to the left


until an operand has been inserted.

Backtrack to the last operator, and put the next


node to its right.

Continue in the same pattern.

14

You might also like