Recursion on Trees - Computer Science and...
-
Upload
phungxuyen -
Category
Documents
-
view
223 -
download
8
Transcript of Recursion on Trees - Computer Science and...
![Page 1: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/1.jpg)
Recursion on Trees
26 April 2013 OSU CSE 1
![Page 2: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/2.jpg)
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
26 April 2013 OSU CSE 2
![Page 3: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/3.jpg)
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
26 April 2013 OSU CSE 3
This way of viewing a tree treats it as a
collection of nodes.
![Page 4: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/4.jpg)
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
26 April 2013 OSU CSE 4
This way of viewing a tree fully reveals its recursive structure.
![Page 5: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/5.jpg)
26 April 2013 OSU CSE 5
R
B K
A C
T
L
G
S
H E P
This way of viewing a tree treats it as a
collection of nodes.
![Page 6: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/6.jpg)
26 April 2013 OSU CSE 6
This way of viewing a tree fully reveals its recursive structure.
A tree...
... and the subtrees of its root, which are
also trees.
![Page 7: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/7.jpg)
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
26 April 2013 OSU CSE 7
![Page 8: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/8.jpg)
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...
26 April 2013 OSU CSE 8
![Page 9: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/9.jpg)
Example /** * Reports the size of an XMLTree.
* ... * @ensures * size = [number of nodes in t]
*/ private static int size(XMLTree t) {...}
26 April 2013 OSU CSE 9
![Page 10: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/10.jpg)
26 April 2013 OSU CSE 10
The number of nodes in this
tree, t ...
![Page 11: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/11.jpg)
26 April 2013 OSU CSE 11
... is 1 (the root) plus the total
number of nodes in all the subtrees of the
root of t.
![Page 12: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/12.jpg)
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; }
26 April 2013 OSU CSE 12
![Page 13: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/13.jpg)
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; }
26 April 2013 OSU CSE 13
This recursive call reports the size of a subtree of the root.
![Page 14: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/14.jpg)
Example /** * Reports the height of an XMLTree.
* ... * @ensures * height = [height of t]
*/ private static int height(XMLTree t) {...}
26 April 2013 OSU CSE 14
![Page 15: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/15.jpg)
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; }
26 April 2013 OSU CSE 15
![Page 16: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/16.jpg)
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; }
26 April 2013 OSU CSE 16
This recursive call reports the height of a
subtree of the root.
![Page 17: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/17.jpg)
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; }
26 April 2013 OSU CSE 17
Why is it a good idea to store the result of the recursive call in a
variable here?
![Page 18: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/18.jpg)
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)
26 April 2013 OSU CSE 18
![Page 19: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/19.jpg)
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)
26 April 2013 OSU CSE 19
What is the value of this expression? Computing this
value is what we mean by evaluating the expression.
![Page 20: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/20.jpg)
Order of Evaluation
• What is the order of evaluation of subexpressions in this expression? (1 + 3) * 5 – (4 / 2)
26 April 2013 OSU CSE 20
![Page 21: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/21.jpg)
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)
26 April 2013 OSU CSE 21
![Page 22: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/22.jpg)
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)
26 April 2013 OSU CSE 22
The fully parenthesized version is based on a convention regarding
the precedence of operators (e.g., “ * before – ” in ordinary math).
![Page 23: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/23.jpg)
Order of Evaluation
• What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)
26 April 2013 OSU CSE 23
First (= 4)
![Page 24: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/24.jpg)
Order of Evaluation
• What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)
26 April 2013 OSU CSE 24
Second (= 20)
First (= 4)
![Page 25: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/25.jpg)
Order of Evaluation
• What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)
26 April 2013 OSU CSE 25
Second (= 20)
First (= 4)
Third (= 2)
![Page 26: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/26.jpg)
Order of Evaluation
• What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)
26 April 2013 OSU CSE 26
Second (= 20)
First (= 4)
Third (= 2)
Fourth (= 18)
![Page 27: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/27.jpg)
Order of Evaluation
• What is the order in which the subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)
26 April 2013 OSU CSE 27
Second (= 20)
First (= 4)
Third (= 2)
Fourth (= 18)
“Inner-most” parentheses first, but there may be some flexibility in the order of evaluation (e.g., / before + would work just as well, but not *
before +, in this expression).
![Page 28: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/28.jpg)
Tree Representation of Expression
• Key: Each operand of an operator must be evaluated before that operator can be evaluated
26 April 2013 OSU CSE 28
![Page 29: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/29.jpg)
Tree Representation of Expression
• Key: Each operand of an operator must be evaluated before that operator can be evaluated
26 April 2013 OSU CSE 29
+ – * /
1 2 3 4 5
![Page 30: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/30.jpg)
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)
26 April 2013 OSU CSE 30
![Page 31: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/31.jpg)
26 April 2013 OSU CSE 31
–
*
+
1
5
3
/
4 2
((1 + 3) * 5) – (4 / 2)
![Page 32: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/32.jpg)
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
26 April 2013 OSU CSE 32
![Page 33: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/33.jpg)
26 April 2013 OSU CSE 33
To evaluate the expression tree rooted here ... –
*
+
1
5
3
/
4 2
![Page 34: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/34.jpg)
26 April 2013 OSU CSE 34
... first evaluate this expression
tree (= 20) ... –
*
+
1
5
3
/
4 2
![Page 35: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/35.jpg)
26 April 2013 OSU CSE 35
–
*
+
1
5
3
/
4 2
... then evaluate this expression
tree (= 2) ...
![Page 36: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/36.jpg)
26 April 2013 OSU CSE 36
... then apply the operator in
the root (= 18). –
*
+
1
5
3
/
4 2
![Page 37: Recursion on Trees - Computer Science and Engineeringweb.cse.ohio-state.edu/.../extras/slides/24.Recursion-Trees.pdf · Structure of Trees • Two views of a tree: – A tree is made](https://reader034.fdocuments.in/reader034/viewer/2022052608/5ae0623c7f8b9a5a668d7ed3/html5/thumbnails/37.jpg)
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
26 April 2013 OSU CSE 37