recursion 4tree traversals - computing sciencemitchell/cmpt-225/2020-spring/slides/8 - recursion...
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/4.jpg)
"
÷÷÷:÷÷÷÷÷::÷← 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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022081522/5fb8863ed26f9d10e702c528/html5/thumbnails/15.jpg)
Ends