recursion 4tree traversals - computing sciencemitchell/cmpt-225/2020-spring/slides/8 - recursion...

15
Recursion 4 Tree Traversals

Upload: others

Post on 15-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Recursion 4 Tree Traversals

Page 2: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Recursion: A definite of a function is recursive ifthe body contains an application

of itself .

Consider : Scn) = ?jior : Scn)={ 0 if n=o-

n+ Slm) if n> 0

These two descriptions of Scn) suggest two

implementations :

E⇒ SHE SHE5=0 if n=0for i=l . - A return 0St St i else

returns return ntsln- D7 3① ②

Page 3: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Thesedescribethesamecomputatiai.IOSC4) :5=0

5=0t I = I

S = It 2 =3

5=3t 3=68--6+4=10

Start with 0 , then add 1 , then 2 , - . . then n .

Page 4: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

"

÷÷÷:÷÷÷÷÷::÷← return 4t6%

Start with 0 ,then add 1 , then 2 , . . .then n.

The computation is the same , but the

controlmechjis.im is different.

Page 5: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

The call stack-

→p

¥¥¥×⇒: TEEN

④ =glD : →ads)} *IT4

{ III.nite ←

Page 6: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

RecursionkTheCakStsailcoI%,s%-iffnlIII.→ Call SCD

÷:""

Theakston:

.¥¥#¥¥÷¥?¥¥i¥functions

.-

Page 7: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Example with 2 recursive calls-

0 if n= 0Fn =/ ¥ on it n "

fibcn) {if n a 2return n

elsereturn fib (n- i ) t fib(n - 2)

}

(Aside: fibC) is a terrible way to compute Fn :The running time is a double exponentialin the input site . )

Page 8: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

TreeofRecursireCaF4

--Fz F3

¥4 AF,

Fz

AF. I

Page 9: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Recursion onTrees - Height of a node ¥-

Themhayeisbeht ftp.anenddeby ?in a binary tree |¥§

hat =/ 0if v is a leaf-

max(h(letters) , hlrightlu'D ) , otherwise-

Pseudo-codeversi left(D=height G){ left child

if r is a leaf of v

if I'Tas% exam

return It height (a)else

g.return It maxfheig.lt/leftlrD,heightCrishtCuD)

Page 10: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

ReeursionatreesExauple-Heightofanode.tn4) = ?↳↳ :

⇐i %

o

⇐✓ ↳ i.

Notice: The tree of recursive calls is the sameas the tree rooted at r .

Page 11: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

TraversalsofR-otedbinargfrees.tttraversal of a graph is a processthat "visits" each

node in the graph once .

- We consider 4 standard tree traversals :1 level order2. pre - order

3. in - order

4. post - order .

23,4 begin at the roof ! recursively visitthe nodes in each subtree € theroot

. They vary in the relative organ . .

Page 12: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

pre-Order Traversal-

Perf - order-The){Visit v

if leftG) exists , pre -order-T ( left

}it right4) exists , pre

- order -T ( rightful)

- v is visited before anyof its descendants

-

every node inthe left subtree is visited

before anynode in the right subtree .

-

* A.BP1E 4FK→T ---

if#asB- - p

¥*⇒. ⇐*a- ÷G

Page 13: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

IPorTrauersalsIn - order -THE post -order-THE

if leftG) exists l post - order -thefts)

÷:÷÷÷÷:*.li:::÷÷÷:*:*.}

in - order -T( rightCN) visit u

} 3

±:*:I ¥ ¥ "a 5¥ ¥ ↳< A

Page 14: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

T.me/ntreetraversalsThesepre/in/post- order traversals take time ON

,

.tn a tree with n vertices,

• assuming that :

. given a vertex v , we can accessthe children of v in 04 ) time .

-

" visiting"a vertex takes old time .

Page 15: Recursion 4Tree Traversals - Computing Sciencemitchell/cmpt-225/2020-Spring/slides/8 - Recursion and... · Recursion: A definite of a function is recursive if the body contains an

Ends