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

recursion on trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Recursion on Trees

7 January 2019 OSU CSE 1

slide-2
SLIDE 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
  • f which is another tree

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

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
  • f which is another tree

7 January 2019 OSU CSE 3

This way of viewing a tree treats it as a collection of nodes.

slide-4
SLIDE 4

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
  • f which is another tree

7 January 2019 OSU CSE 4

This way of viewing a tree fully reveals its recursive structure.

slide-5
SLIDE 5

7 January 2019 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.

slide-6
SLIDE 6

7 January 2019 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.

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 10

7 January 2019 OSU CSE 10

The number of nodes in this tree, t ...

slide-11
SLIDE 11

7 January 2019 OSU CSE 11

... is 1 (the root) plus the total number of nodes in all the subtrees of the root of t.

slide-12
SLIDE 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

slide-13
SLIDE 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)); } } return totalNodes; }

7 January 2019 OSU CSE 13

This recursive call reports the size of a subtree of the root.

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 16

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 16

This recursive call reports the height of a subtree of the root.

slide-17
SLIDE 17

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 17

Why is it a good idea to store the result of the recursive call in a variable here?

slide-18
SLIDE 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

slide-19
SLIDE 19

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 19

What is the value of this expression? Computing this value is what we mean by evaluating the expression.

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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)

7 January 2019 OSU CSE 22

The fully parenthesized version is based on a convention regarding the precedence of operators (e.g., “ * before – ” in ordinary math).

slide-23
SLIDE 23

Order of Evaluation

  • What is the order in which the

subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)

7 January 2019 OSU CSE 23

First (= 4)

slide-24
SLIDE 24

Order of Evaluation

  • What is the order in which the

subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)

7 January 2019 OSU CSE 24

Second (= 20) First (= 4)

slide-25
SLIDE 25

Order of Evaluation

  • What is the order in which the

subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)

7 January 2019 OSU CSE 25

Second (= 20) First (= 4) Third (= 2)

slide-26
SLIDE 26

Order of Evaluation

  • What is the order in which the

subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)

7 January 2019 OSU CSE 26

Second (= 20) First (= 4) Third (= 2) Fourth (= 18)

slide-27
SLIDE 27

Order of Evaluation

  • What is the order in which the

subexpressions in this expression are evaluated? ((1 + 3) * 5) – (4 / 2)

7 January 2019 OSU CSE 27

Second (= 20) First (= 4) Third (= 2) Fourth (= 18) “Inner-most” parentheses first, but there may be some flexibility in the

  • rder of evaluation (e.g., / before +

would work just as well, but not * before +, in this expression).

slide-28
SLIDE 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

slide-29
SLIDE 29

Tree Representation of Expression

  • Key: Each operand of an operator must

be evaluated before that operator can be evaluated

7 January 2019 OSU CSE 29

+ – * / 1 2 3 4 5

slide-30
SLIDE 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

slide-31
SLIDE 31

7 January 2019 OSU CSE 31

– * + 1 5 3 / 4 2 ((1 + 3) * 5) – (4 / 2)

slide-32
SLIDE 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

  • perator 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

  • perand, and that operand is the value of the

expression represented by the tree

7 January 2019 OSU CSE 32

slide-33
SLIDE 33

7 January 2019 OSU CSE 33

To evaluate the expression tree rooted here ...

– * + 1 5 3 / 4 2

slide-34
SLIDE 34

7 January 2019 OSU CSE 34

... first evaluate this expression tree (= 20) ...

– * + 1 5 3 / 4 2

slide-35
SLIDE 35

7 January 2019 OSU CSE 35

– * + 1 5 3 / 4 2

... then evaluate this expression tree (= 2) ...

slide-36
SLIDE 36

7 January 2019 OSU CSE 36

... then apply the operator in the root (= 18).

– * + 1 5 3 / 4 2

slide-37
SLIDE 37

XML Encoding of Expressions

  • The difference between an operator and an
  • perand 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