Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
RashidFaridChishti
17 views
26 slides
May 18, 2024
Slide 1 of 26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
About This Presentation
Data Structures and Agorithm: DS 14 Binary Expression Tree.pptx
Size: 173.31 KB
Language: en
Added: May 18, 2024
Slides: 26 pages
Slide Content
Data Structure Lecture No. 14 Binary Expression Tree Engr . Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan Video Lecture
Expression trees are binary trees for mathematical expression Leaves are operands (a, b, c, …) Other nodes are operators. (+, -, *, /) This is Expression Tree for ( a+b *c )+((d* e+f )*g ) Binary Expression Tree + + * b a c * + g f * d e
Preorder traversal : (node, left, right) gives Prefix Expression . + + a * b c * + * d e f g Expression Tree Traversal + + * b a c * + g f * d e
Inorder traversal : (left, node, right ) gives Infix Expression . a + b * c + d * e + f * g Expression Tree Traversal + + * b a c * + g f * d e
Postorder traversal : (left, right, node) gives Postfix Expression . a b c * + d e * f + g * + Expression Tree Traversal + + * b a c * + g f * d e
Read expression one symbol at a time If the symbol is an operand Create a one-node tree Push its pointer to a stack If the symbol is an operator Pop two pointers Form a new tree whose root is the operator In the end, the only element of the stack will be the root of an expression tree. Constructing a Binary Expression Tree from Postfix
a b + c d e + * * Constructing Expression Tree from Postfix Stack
a b + c d e + * * Constructing Expression Tree from Postfix Stack a
a b + c d e + * * Constructing Expression Tree from Postfix Stack a b
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a c
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a c d
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a c d e
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a c e + d
a b + c d e + * * Constructing Expression Tree from Postfix Stack b + a * c e + d
a b + c d e + * * Constructing Expression Tree from Postfix Stack * * c e + d b * a
a b + c d e + * * Constructing Expression Tree from Postfix Stack * * c e + d b * a root
(1+2) * ((3*(4+5)) Evaluation of a Binary Expression Tree * * 3 5 + 4 2 + 1 root 3 9 27 81
#include < iostream > #include <string> #include < math.h > using namespace std ; struct Data{ char opr ; double num ; }; struct Tree_Node { Tree_Node * left ; Data data ; Tree_Node * right ; }; typedef Tree_Node * SType ; 19 Implementation of Binary Expression Tree struct Node{ SType info ; Node *next ; }; class Stack{ private : Node * head; public : Stack ( ); bool Is_Empty (); SType Top(); void Push ( SType Data ); SType Pop ( ); ~ Stack( ) ; }; Stack ::Stack( ){ head = NULL; } 1 2
bool Stack:: Is_Empty () { return head == NULL; } SType Stack::Top() { if ( ! Is_Empty () ) return head->info; return NULL; } void Stack :: Push ( SType Data ) { Node * newNode ; newNode = new Node ; if ( newNode == NULL ){ cout << endl << "Stack is full" ; return ; } newNode -> info = Data ; newNode -> next = head ; head = newNode ; } 20 Implementation of Binary Expression Tree SType Stack :: Pop( ) { if ( Is_Empty () ){ cout << "Stack is empty " ; return NULL ; } Node *current ; SType Data ; current = head ; Data = current -> info ; head = head -> next ; delete current ; return Data ; } Stack :: ~Stack( ){ Node *current ; while ( head != NULL ){ current = head ; head = head -> next ; delete head ; } } 3 4
Binary_Expression_Tree :: Binary_Expression_Tree ( char Postfix[]){ // Traverse through every character of // input expression for ( int i=0; Postfix[i]; i ++) { // ignore space and tab if ( Postfix[ i ] == ' ' || Postfix[ i ] == '\t' ) continue ; // If operand, simply push into stack if (! Is_Operator (Postfix[ i ])) { Data d; d.num = Postfix[ i ] - 0x30; d.opr = 0; t = New_Tree_Node (d); St.Push (t ); } 22 Implementation of Binary Expression Tree else { // operator Data d; d.num = 0; d.opr = Postfix[ i ]; t = New_Tree_Node (d); // Pop two top nodes t1 = St.Pop (); // Remove top t2 = St.Pop (); // make them children t- >right = t1; t- >left = t2; // Add this subexpression to stack St.Push (t ); } } // connect it with root pointer root = St.Pop (); } 7 8
double Binary_Expression_Tree :: Evaluate ( ) { return Evaluate(root); } double Binary_Expression_Tree :: Evaluate ( Tree_Node * node) { // empty tree if (node == NULL) return 0; // leaf node i.e, an integer if (node-> left == NULL && node- > right== NULL ) { return node-> data.num ; } 23 Implementation of Binary Expression Tree // Evaluate left subtree double l_val = Evaluate(node->left); // Evaluate right subtree double r_val = Evaluate(node->right); // Check which operator to apply if (node-> data.opr == '+' ) return l_val + r_val ; if (node-> data.opr == '-' ) return l_val-r_val ; if (node-> data.opr == '*' ) return l_val * r_val ; if (node-> data.opr == '/' ) return l_val / r_val ; if (node-> data.opr == '^' ) return pow( l_val,r_val ); return 0; } 9 10