recursion on trees
play

Recursion on Trees 7 January 2019 OSU CSE 1 Structure of Trees - PowerPoint PPT Presentation

Recursion on Trees 7 January 2019 OSU CSE 1 Structure of Trees Two views of a tree: A tree is made up of: A root node A string of zero or more child nodes of the root, each of which is the root of its own tree A tree is


  1. Recursion on Trees 7 January 2019 OSU CSE 1

  2. Structure of Trees • Two views of a tree: – A tree is made up of: • A root node • A string of zero or more child nodes of the root, each of which is the root of its own tree – A tree is made up of: • A root node • A string of zero or more subtrees of the root, each of which is another tree 7 January 2019 OSU CSE 2

  3. Structure of Trees This way of viewing a • Two views of a tree: tree treats it as a – A tree is made up of: collection of nodes. • A root node • A string of zero or more child nodes of the root, each of which is the root of its own tree – A tree is made up of: • A root node • A string of zero or more subtrees of the root, each of which is another tree 7 January 2019 OSU CSE 3

  4. Structure of Trees This way of viewing a • Two views of a tree: tree fully reveals its – A tree is made up of: recursive structure. • A root node • A string of zero or more child nodes of the root, each of which is the root of its own tree – A tree is made up of: • A root node • A string of zero or more subtrees of the root, each of which is another tree 7 January 2019 OSU CSE 4

  5. This way of viewing a R tree treats it as a collection of nodes. K B S A C L H E P T G 7 January 2019 OSU CSE 5

  6. This way of viewing a A tree... tree fully reveals its recursive structure. ... and the subtrees of its root, which are also trees. 7 January 2019 OSU CSE 6

  7. Recursive Algorithms • The “in-your-face” recursive structure of trees (in the second way to view them) allows you to implement some methods that operate on trees using recursion – Indeed, this is sometimes the only sensible way to implement those methods 7 January 2019 OSU CSE 7

  8. XMLTree • The methods for XMLTree are named using the collection-of-nodes view of a tree, because most uses of XMLTree (e.g., the XML/RSS projects) do not need to leverage the recursive structure of trees • But some uses of XMLTree demand that you use the recursive view... 7 January 2019 OSU CSE 8

  9. Example /** * Reports the size of an XMLTree. * ... * @ensures * size = [number of nodes in t] */ private static int size(XMLTree t) {...} 7 January 2019 OSU CSE 9

  10. The number of nodes in this tree, t ... 7 January 2019 OSU CSE 10

  11. ... is 1 (the root) plus the total number of nodes in all the subtrees of the root of t . 7 January 2019 OSU CSE 11

  12. Example private static int size(XMLTree t) { int totalNodes = 1; if (t.isTag()) { for ( int i = 0; i < t.numberOfChildren(); i++) { totalNodes += size(t.child(i)); } } return totalNodes; } 7 January 2019 OSU CSE 12

  13. Example private static int size(XMLTree t) { int totalNodes = 1; if (t.isTag()) { for ( int i = 0; i < t.numberOfChildren(); i++) { totalNodes += size(t.child(i)); } This recursive call } reports the size of a return totalNodes; subtree of the root. } 7 January 2019 OSU CSE 13

  14. Example /** * Reports the height of an XMLTree. * ... * @ensures * height = [height of t] */ private static int height(XMLTree t) {...} 7 January 2019 OSU CSE 14

  15. Example private static int height(XMLTree t) { int maxSubtreeHeight = 0; if (t.isTag()) { for ( int i = 0; i < t.numberOfChildren(); i++) { int subtreeHeight = height(t.child(i)); if (subtreeHeight > maxSubtreeHeight) { maxSubtreeHeight = subtreeHeight; } } } return maxSubtreeHeight + 1; } 7 January 2019 OSU CSE 15

  16. Example This recursive call private static int height(XMLTree t) { reports the height of a int maxSubtreeHeight = 0; subtree of the root. if (t.isTag()) { for ( int i = 0; i < t.numberOfChildren(); i++) { int subtreeHeight = height(t.child(i)); if (subtreeHeight > maxSubtreeHeight) { maxSubtreeHeight = subtreeHeight; } } } return maxSubtreeHeight + 1; } 7 January 2019 OSU CSE 16

  17. Example Why is it a good idea private static int height(XMLTree t) { to store the result of int maxSubtreeHeight = 0; the recursive call in a if (t.isTag()) { variable here? for ( int i = 0; i < t.numberOfChildren(); i++) { int subtreeHeight = height(t.child(i)); if (subtreeHeight > maxSubtreeHeight) { maxSubtreeHeight = subtreeHeight; } } } return maxSubtreeHeight + 1; } 7 January 2019 OSU CSE 17

  18. Expression Trees • There are many other uses for XMLTree • Consider an expression tree , which is a representation of a formula you might type into a Java program or into a calculator, such as: (1 + 3) * 5 – (4 / 2) 7 January 2019 OSU CSE 18

  19. Expression Trees What is the value of this • There are many other uses for XMLTree expression? Computing this value is what we mean by • Consider an expression tree , which is a evaluating the expression. representation of a formula you might type into a Java program or into a calculator, such as: (1 + 3) * 5 – (4 / 2) 7 January 2019 OSU CSE 19

  20. Order of Evaluation • What is the order of evaluation of subexpressions in this expression? (1 + 3) * 5 – (4 / 2) 7 January 2019 OSU CSE 20

  21. Order of Evaluation • What is the order of evaluation of subexpressions in this expression? (1 + 3) * 5 – (4 / 2) • Let’s fully parenthesize it to help: ((1 + 3) * 5) – (4 / 2) 7 January 2019 OSU CSE 21

  22. Order of Evaluation • What is the order of evaluation of subexpressions in this expression? (1 + 3) * 5 – (4 / 2) • Let’s fully parenthesize it to help: ((1 + 3) * 5) – (4 / 2) The fully parenthesized version is based on a convention regarding the precedence of operators (e.g., “ * before – ” in ordinary math). 7 January 2019 OSU CSE 22

  23. Order of Evaluation • What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2) First (= 4 ) 7 January 2019 OSU CSE 23

  24. Order of Evaluation • What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2) First Second (= 4 ) (= 20 ) 7 January 2019 OSU CSE 24

  25. Order of Evaluation • What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2) First Second Third (= 4 ) (= 20 ) (= 2 ) 7 January 2019 OSU CSE 25

  26. Order of Evaluation • What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2) First Second Fourth Third (= 4 ) (= 20 ) (= 18 ) (= 2 ) 7 January 2019 OSU CSE 26

  27. Order of Evaluation “Inner-most” parentheses first, but there may be some flexibility in the order of evaluation (e.g., / before + • What is the order in which the would work just as well, but not * subexpressions in this expression are before +, in this expression). evaluated? ((1 + 3) * 5) – (4 / 2) First Second Fourth Third (= 4 ) (= 20 ) (= 18 ) (= 2 ) 7 January 2019 OSU CSE 27

  28. Tree Representation of Expression • Key: Each operand of an operator must be evaluated before that operator can be evaluated 7 January 2019 OSU CSE 28

  29. Tree Representation of Expression • Key: Each operand of an operator must be evaluated before that operator can be evaluated 1 + 2 – 3 * 4 / 5 7 January 2019 OSU CSE 29

  30. Tree Representation of Expression • So, this approach works: – Last operator evaluated is in root node – Each operator’s left and right operands are its two subtrees (i.e., each operator has two subtrees, each of which is a subexpression in the larger expression) 7 January 2019 OSU CSE 30

  31. ((1 + 3) * 5) – (4 / 2) – * / + 5 4 2 1 3 7 January 2019 OSU CSE 31

  32. Evaluation of Expression Trees • To evaluate any expression tree : – If the root is an operator, then first evaluate the expression trees that are its left (first) and right (second) subtrees; then apply that operator to these two values, and the result is the value of the expression represented by the tree – If the root has no subtrees, then it must be an operand, and that operand is the value of the expression represented by the tree 7 January 2019 OSU CSE 32

  33. To evaluate the expression tree rooted here ... – * / + 5 4 2 1 3 7 January 2019 OSU CSE 33

  34. ... first evaluate this expression tree – (= 20 ) ... * / + 5 4 2 1 3 7 January 2019 OSU CSE 34

  35. ... then evaluate this expression tree – (= 2 ) ... * / + 5 4 2 1 3 7 January 2019 OSU CSE 35

  36. ... then apply the operator in the root – (= 18 ). * / + 5 4 2 1 3 7 January 2019 OSU CSE 36

  37. XML Encoding of Expressions • The difference between an operator and an operand can be encoded in XML tags (e.g., "<operator>" and "<operand>") – The specific operator (e.g., "+", "–", "*", "/") can be either an attribute of an operator tag, or its content – Similarly, the value of an operand (e.g., "1", "34723576", etc.) ... • Given such details for a specific XML encoding of expressions, you should be able to evaluate an expression given an XMLTree for its encoding 7 January 2019 OSU CSE 37

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend