Extended Binary Trees Recurrence relations Today: Extended Binary - - PowerPoint PPT Presentation

extended binary trees recurrence relations today
SMART_READER_LITE
LIVE PREVIEW

Extended Binary Trees Recurrence relations Today: Extended Binary - - PowerPoint PPT Presentation

Extended Binary Trees Recurrence relations Today: Extended Binary Trees (basis for much of WA8) Recurrence relations, part 1 EditorTrees worktime Bringing new life to Null nodes! 1-2 An Extended Binary tree is either an


slide-1
SLIDE 1

Extended Binary Trees Recurrence relations

slide-2
SLIDE 2

 Today:

  • Extended Binary Trees (basis for much of WA8)
  • Recurrence relations, part 1
  • EditorTrees worktime
slide-3
SLIDE 3

Bringing new life to Null nodes!

slide-4
SLIDE 4

 An Extended Binary tree is either

  • an external

nal (nu null ll) no node, or

  • an (intern

rnal al) root node and two

EBTs TL and TR.

 We draw internal nodes as circles and external nodes as

squares.

  • Generic picture and detailed picture.

 This is simply an alternative way of viewing binary trees,

in which we view the external nodes as “places” where a search can end or an element can be inserted. 1-2

slide-5
SLIDE 5

 Prope

perty rty P(N): For any N>=0, any EBT with N internal nodes has _______ external nodes.

 Proo

roof by by stro trong in indu duction, based on the recursive definition.

  • A notation for this problem: IN(T), EN(T)
  • Note that, like a lot of other simple examples, this one

can be done without induction.

  • But one purpose of this exercise is practice with strong

induction, especially on binary trees.

 What is the crux of any induction proof?

  • Finding a way to relate the properties for larger values

(in this case larger trees) to the property for smaller values (smaller trees). Do

  • th

the e proof

  • of n

now

  • w.

3-10 10

slide-6
SLIDE 6

A technique for analyzing recursive algorithms

slide-7
SLIDE 7
slide-8
SLIDE 8

 Split the sequence in half  Where can the maximum subsequence appear?  Three possibilities :

  • entirely in the first half,
  • entirely in the second half, or
  • begins

ns in the first half and end nds in the second half

11 11

slide-9
SLIDE 9

1.

Using recursion, find the maximum sum of firs irst half of sequence

2.

Using recursion, find the maximum sum of seco econd half of sequence

3.

Compute the max of all sums that begin in the first half and end in the second half

  • (Use a couple of loops for this)

4.

Choose the largest of these three numbers

slide-10
SLIDE 10

So, what’s the run-time? 12 12-13 13

slide-11
SLIDE 11

 Use a Rec

ecurrence R Rel elation

  • A function of N, typically

written T(N)

  • Gives the run-time as a

function of N

  • Two (or more) part definition:

 Base case, like T(1) = c  Recursive case, like T(N) = T(N/2)

So, what’s the recurrence relation for the recursive MCSS algorithm? 14 14

slide-12
SLIDE 12

What’s N in the base case? 15 15-16 16

slide-13
SLIDE 13

 An equation (or inequality) that relates the

nth element of a sequence to certain of its predecessors (recursive case)

 Includes an initial condition (base case)  Solu

  • lutio

ion: A function of n.

 Similar to differential equation, but discrete

instead of continuous

 Some solution techniques are similar to

  • diff. eq. solution techniques
slide-14
SLIDE 14

 One strategy: gu

gues ess a and ch chec eck

 Examples:

  • T(0) = 0, T(N) = 2 + T(N-1)
  • T(0) = 1, T(N) = 2 T(N-1)
  • T(0) = T(1) = 1, T(N) = T(N-2) + T(N-1)
  • T(0) = 1, T(N) = N T(N-1)
  • T(0) = 0, T(N) = T(N -1) + N
  • T(1) = 1, T(N) = 2 T(N/2) + N

(just consider the cases where N=2k)

18 18-20: Skip Skip 1 17 for n

  • r now
  • w
slide-15
SLIDE 15

 Substi

titu tuti tion

  • n

 T(1) = 1, T(N) = 2 T(N/2) + N

(just consider N=2k)

 Suppose we substitute N/2 for N in the

recursive equation?

  • We can plug the result into the original equation!

20 20

slide-16
SLIDE 16

 Guess and check  Substitution  Telescoping and iteration  The “master” method

slide-17
SLIDE 17

What’s N?

slide-18
SLIDE 18

 Consider:

  • T(1) = 1
  • T(N) = N + T(N/2), where N = 2k for some k

 Substitution:

  • Use recurrence relation repeatedly to expand T() on

right-hand side of relation

slide-19
SLIDE 19