1 / 14

CS308 Data Structures

An application of binary trees: Binary Expression Trees. CS308 Data Structures. 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

jessie
Télécharger la présentation

CS308 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. An application of binary trees: Binary Expression Trees CS308 Data Structures

  2. 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 asingle 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.

  3. ‘/’ ‘3’ ‘+’ ‘2’ ‘4’ A Four-Level Binary Expression ‘*’ ‘-’ ‘5’ ‘8’

  4. 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.

  5. ‘*’ ‘3’ ‘+’ ‘2’ ‘4’ A Binary Expression Tree What value does it have? ( 4 + 2 ) * 3 = 18

  6. ‘*’ ‘/’ ‘-’ ‘8’ ‘5’ ‘3’ ‘+’ ‘2’ ‘4’ Easy to generate the infix, prefix, postfix expressions (how?) Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5 - 4 2 + 3 / *

  7. ‘/’ Inorder Traversal: (A + H) / (M - Y) Print second tree ‘-’ ‘+’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree last

  8. ‘/’ Preorder Traversal: / + A H - M Y Print first tree ‘-’ ‘+’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree second Print right subtree last

  9. ‘/’ Postorder Traversal: A H + M Y - / Print last tree ‘-’ ‘+’ ‘A’ ‘H’ ‘M’ ‘Y’ Print left subtree first Print right subtree second

  10. ExprTree ‘*’ ~ExprTree ‘3’ ‘+’ Build Evaluate . . . ‘2’ ‘4’ class ExprTree private: TreeNode* root;

  11. OPERAND7 .whichType .operand Each node contains two pointers struct TreeNode { InfoNode info ;// Data member TreeNode* left ;// Pointer to left child TreeNode* right ; // Pointer to right child }; NULL6000 .left .info .right

  12. OPERATOR‘+’ OPERAND7 .whichType .operation .whichType .operand InfoNode has 2 forms enum OpType { OPERATOR, OPERAND } ; struct InfoNode { OpType whichType; union // ANONYMOUS union { char operation ; int operand ; } };

  13. 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

  14. 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. Building a Binary Expression Tree from an expression in prefix notation

More Related