98
Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in front of us is not bar Our passage to our journey's end for good, But just to ask us who we think we are Insisting always on our own way so. She likes to halt us in our runner tracks, And make us get down in a foot of snow Debating what to do without an ax. And yet she knows obstruction is in vain: We will not be put off the final goal We have it hidden in us to attain, Not though we have to seize earth by the pole And, tired of aimless circling in one place, Steer straight off after something into space. Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Content derived from: Data Structures Using C++ (D.S. Malik) and Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)c

Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Embed Size (px)

Citation preview

Page 1: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Merge Sort andTreesCS 244

On a Tree Fallen Across the Road by Robert Frost(To hear us talk)

The tree the tempest with a crash of woodThrows down in front of us is not barOur passage to our journeys end for goodBut just to ask us who we think we are

Insisting always on our own way soShe likes to halt us in our runner tracksAnd make us get down in a foot of snowDebating what to do without an ax

And yet she knows obstruction is in vainWe will not be put off the final goalWe have it hidden in us to attainNot though we have to seize earth by the pole

And tired of aimless circling in one placeSteer straight off after something into space

Brent M Dingle PhD

Game Design and Development Program

Department of Mathematics Statistics and Computer Science

University of Wisconsin ndash Stout

Content derived from Data Structures Using C++ (DS Malik)and Data Structures and Algorithms in C++ (Goodrich Tamassia Mount)c

Previously

bull Sortingbull Selection Sort (chap 10 page 534-ish)bull Insertion Sort (chap 10 page 540-ish)bull Quicksort (chap 10 page 552-ish)

Today

bull MergeSortrsquos Tree Representation

bull Introduction to Treesbull General and Binary

Merge Sort

bull Merge sort is based on the divide-and-conquer paradigm It consists of three stepsndash Divide partition input

sequence S into two sequences S1 and S2 of about n2 elements each

ndash Recur recursively sort S1 and S2

ndash Conquer merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S C)Input sequence S comparator C Output sequence S sorted

according to Cif Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

The Mystery

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Somehow this

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 2: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Previously

bull Sortingbull Selection Sort (chap 10 page 534-ish)bull Insertion Sort (chap 10 page 540-ish)bull Quicksort (chap 10 page 552-ish)

Today

bull MergeSortrsquos Tree Representation

bull Introduction to Treesbull General and Binary

Merge Sort

bull Merge sort is based on the divide-and-conquer paradigm It consists of three stepsndash Divide partition input

sequence S into two sequences S1 and S2 of about n2 elements each

ndash Recur recursively sort S1 and S2

ndash Conquer merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S C)Input sequence S comparator C Output sequence S sorted

according to Cif Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

The Mystery

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Somehow this

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 3: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Today

bull MergeSortrsquos Tree Representation

bull Introduction to Treesbull General and Binary

Merge Sort

bull Merge sort is based on the divide-and-conquer paradigm It consists of three stepsndash Divide partition input

sequence S into two sequences S1 and S2 of about n2 elements each

ndash Recur recursively sort S1 and S2

ndash Conquer merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S C)Input sequence S comparator C Output sequence S sorted

according to Cif Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

The Mystery

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Somehow this

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 4: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Merge Sort

bull Merge sort is based on the divide-and-conquer paradigm It consists of three stepsndash Divide partition input

sequence S into two sequences S1 and S2 of about n2 elements each

ndash Recur recursively sort S1 and S2

ndash Conquer merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S C)Input sequence S comparator C Output sequence S sorted

according to Cif Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

The Mystery

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Somehow this

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 5: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

The Mystery

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Somehow this

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 6: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Becomes this

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 7: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

The MysteryAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

But how

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 8: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Class Exercise

C R P G B H Q A

Algorithm mergeSort(S C)if Ssize() gt 1

(S1 S2) = partition(S Ssize()2)

S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Begin at the beginning

Recursion Depth = RD = 0

RD = 0

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 9: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 10: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R P G B H Q A

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

RD = 0

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 11: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 12: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R P G B H Q A

C R P G

C R P G B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

RD = 1

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 13: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 14: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R

C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 15: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ C ]

3

Enhanced Version

What happens next

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 16: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R

C C R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

RD = 2

Enhanced Version

What happens next

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 17: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C R

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

return [ R ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 18: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C RRD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 19: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

return [ C R ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 20: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 21: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 22: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 23: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G

return [ P ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 24: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R P G

P P G

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 25: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

P G

C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R P G

P P G Greturn [ G ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 26: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 27: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A C R P G

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

return [ G P ]

RD = 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 28: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

RD = 1

Enhanced Version

What happens next

S1 S2

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 29: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 30: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 31: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 32: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 33: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 34: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 35: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

A B

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 36: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Details of Merge

Already did 2 trivial merges

But this is the first

that requires

some ldquoreal thoughtrdquo

Algorithm merge(A B)Input sequences A and B

with n2 elements each

Output sorted sequence of A B

S empty sequencewhile AisEmpty() BisEmpty()

if Afirst() lt Bfirst()

SinsertLast(AremoveFirst())else

SinsertLast(BremoveFirst())while AisEmpty()

SinsertLast(AremoveFirst())while BisEmpty()

SinsertLast(BremoveFirst())return S

RD = 1

Enhanced Version

What happens next

S

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 37: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

return [ C G P R ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 38: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

S1 = [ C G P R ] now call S2 = mergeSort

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 39: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 40: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 41: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 42: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 43: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

return [ B ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 44: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

HB B

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 45: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

return [ H ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 46: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

B H Q A

B B H H

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 47: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

return [ B H ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 48: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H

B H B H

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 49: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 50: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 51: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H

return [ Q ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 52: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q A

B H B H 2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 53: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 3

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

return [ A ]

3

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 54: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A

B B H H Q Q

B H B H

A A

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 55: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 2

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A Q ]

2

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 56: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 57: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 1

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B H Q ]

RD = 1

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 58: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

merge [ C G P R ] with [ A B H Q ]

RD = 0

Enhanced Version

What happens next

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 59: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

C C R R

C R P G B H Q A A B C G H P Q R

B H Q A A B H Q

Class ExerciseAlgorithm mergeSort(S C)

if Ssize() gt 1 (S1 S2) = partition(S

Ssize()2) S1 = mergeSort(S1 C)S2 = mergeSort(S2 C)S = merge(S1 S2)

return(S)

Recursion Depth = RD = 0

C R C R

P P G G

P G G P

C R P G C G P R

Q A A Q

B B H H Q Q

B H B H

A A

return [ A B C G H P Q R ]

RD = 0

Enhanced Version

And we return out

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 60: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is lg N where N is the number of items being sortedbull How do we ldquoknowrdquo that

bull [ pause for students answer here ] =)

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 61: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

MergeSort Take-Aways

bull The execution results of a MergeSort can be represented as a binary treebull And you now know how to fill that tree out

bull MergeSort uses a Divide and Conquer method

bull The height of the tree is O(lg N) where N is the number of items being sortedbull How do we ldquoknow thatrdquobull lg N is the number of times N can be divided by 2 (integer-

wise)

Enhanced Version

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 62: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull [ more pausing ]

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 63: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Runtime of Merge Sort

bull Knowing the height of the representative tree is O(lg N) is important whybull or rather knowing lg N is the number of times N

can be divided by 2 is important to understanding Merge Sort why

bull It is the ldquokey conceptrdquo in showingproving the runtime of Merge Sort is O(N lg N)

Enhanced Version

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 64: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow in-placeFor small data sets

Insertion Sort O(n2) Slow in-placeFor small data sets

Bubble Sort O(n2) Slow in-placeFor small data sets

Heap Sort O(nlog n) Fast in-placeFor large data sets

Merge Sort O(nlogn) Fast sequential data accessFor huge data sets

Quick Sort O(nlogn) Assumes key values are random and uniformly distributed

Place holder for others we have not yet seen

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 65: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Continuing with Trees

bull Chapter 11 Stuffbull Tree Definitions and ADT

bull Tree Traversal Algorithms for General Trees bull preorder and postorder

bull Binary Treesbull Data structures for treesbull Traversals of Binary Trees

bull (preorder inorder postorder)

bull Euler Tours

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 66: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

What is a Tree

bull In Computer Science a tree is an abstract model of a hierarchical structure

bull A tree consists of nodes with a parent-child relation

bull Applicationsndash Organization chartsndash File systemsndash Many others

ComputersrdquoRrdquoUs

Sales

RampD

Manufacturing

Laptops

Desktops

US

International

Europe

Asia

Canada

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 67: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

General Tree Terminologybull Root node without parent (A)

bull Internal node node with at least one child (A B C F)

bull Leaf (aka External node) node without children (E I J K G H D)

bull Ancestors of a node parent grandparent great-grandparent etc

bull Depth of a node number of ancestors(root has depth 0)

bull Height of a tree maximum depth of any node (3)

bull Descendant of a node child grandchild great-grandchild etc

A

B DC

G HE F

I J K

bull Size number of nodes in a tree

bull Subtree tree consisting of a node and its descendants

Subtree

More detail on

depth and height

follow on next slide

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 68: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Definitions Related to Treesbull For clarity (from previous class)

bull Depth of a node p number of ancestors of pSo the root has depth 0

bull Height of a node p the length of the longest downward path to a leaf from pSo if p is a leaf height is 0

bull Examplesbull Height of A is 3 Depth of A is 0bull Height if E is 0 Depth of E is 2bull Height of F is 1 Depth of F is 2bull Height of K is 0 Depth of K is 3

bull Most often we speak of the depth of nodes and the height of the tree

A

B DC

G HE F

I J K

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 69: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Exercise Trees

bull Answer the following questions about the tree shown on the rightndash What is the size of the tree to the rightndash What nodes are leavesndash What node is the rootndash What nodes are internal nodesndash List the ancestors of node Fndash List the descendants of node Bndash What is the depth of Andash What is the height of the treendash How many leaves does a tree with

exactly 1 node have

A

B DC

G HE F

I J K

Take 5 minutes (or less) to answer the followingYou will be randomly selected to share your answers shortlyBe prepared

Recall Definitions Leaf node without children Internal node node with at least one child

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 70: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Tree ADTbull We use positions to abstract

nodesbull Generic methods

bull integer size()bull boolean isEmpty()bull objectIterator elements()bull positionIterator positions()

bull Accessor methodsbull position root()bull position parent(p)bull positionIterator children(p)

bull Query methodsbull boolean isInternal(p)bull boolean isLeaf (p)bull boolean isRoot(p)

bull Update methodsbull swapElements(p q)bull object replaceElement(p o)

bull Additional update methods may be defined by data structures implementing the Tree ADT

Context and relation

to other ADTs

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 71: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 72: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 73: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

A D F

C

E

B

DA

C E

F

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 74: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

A Linked Structure for General Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Sequence of children nodes

bull Node objects implement the Position ADT

B

DA

C E

F

B

A D F

C

E

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 75: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Binary Treebull A binary tree is a tree with the

following propertiesbull Each internal node has two childrenbull The children of a node are an ordered

pair

bull We call the children of an internal node left child and right child

A

B C

F GD E

H I

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 76: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

BinaryTree ADTbull The BinaryTree ADT

extends the Tree ADT ie it inherits all the methods of the Tree ADT

bull Additional methodsbull position leftChild(p)bull position rightChild(p)bull position sibling(p)

bull Update methods may be defined by data structures implementing the BinaryTree ADT

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 77: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

A Linked Structure for Binary Trees

bull A node is represented by an object storing

bull Elementbull Parent nodebull Left child nodebull Right child node

bull Node objects implement the Position ADT

B

DA

C E

B

A D

C E

We want to represent this tree

This is whatit would looklike usinga linked structure

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 78: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Alternate Picture of a binary tree

a

b c

d e

g h i

l

f

j k

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 79: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

[ Pause for Student answer ]

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 80: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

This is the lowest level

Examine the ones above it

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 81: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 2 has 4 nodes 4 = 22

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 82: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 1 has 2 nodes 2 = 21

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 83: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

Level 0 has 1 node 1 = 20

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 84: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b c

d e f g

h i j

YES ndash tree is Balanced

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 85: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 86: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

a

b

c

d

e

f

g h

i j

NO ndash tree is not Balanced2 = 24

Enhanced Version

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 87: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

>

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 88: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull Balanced Treebull A binary tree is balanced if

every level above the lowest is ldquofullrdquo (contains 2level nodes)

bull Is the tree to the right balanced

Insufficient information to tell

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 89: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 90: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

YES ndash tree is Complete

Enhanced Version

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 91: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 92: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

YES ndash tree is Complete

Enhanced Version

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 93: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

Is this tree complete

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 94: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Some More Definitions

bull A Complete Binary Tree with height h is a tree such thatbull level i has 2i nodes

for 0 le i le h-1bull ie balanced

bull Nodes at level h fill up left to right

NO ndash tree is NOT complete

Enhanced Version

Is this tree complete

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 95: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Exercise Binary Tree

bull Are the two trees below the same

bull Why or why not

A

B

A

B

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 96: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Sorted binary treesbull A binary tree is sorted if every node in the tree is larger than (or equal to)

its left descendants and smaller than (or equal to) its right descendants

bull Equal nodes can go either on the left or the right (but it has to be consistent)

10

8 15

4 12 20

17

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 97: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

Binary Search in a sorted arraybull Look at array location (lo + hi)2

2 3 5 7 11 13 17 0 1 2 3 4 5 6

Searching for 5(0+6)2 = 3

hi = 2(0 + 2)2 = 1

lo = 2(2+2)2=2

7

3 13

2 5 11 17

Using a binary search tree

Review and

Re-Apply

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End
Page 98: Merge Sort and Trees CS 244 On a Tree Fallen Across the Road by Robert Frost (To hear us talk) The tree the tempest with a crash of wood Throws down in

The End

bull of this part

  • Merge Sort and Trees
  • Previously
  • Today
  • Merge Sort
  • The Mystery
  • The Mystery (2)
  • The Mystery (3)
  • Class Exercise
  • Class Exercise (2)
  • Slide 10
  • Slide 11
  • Slide 12
  • Class Exercise (3)
  • Class Exercise (4)
  • Class Exercise (5)
  • Class Exercise (6)
  • Class Exercise (7)
  • Class Exercise (8)
  • Class Exercise (9)
  • Class Exercise (10)
  • Class Exercise (11)
  • Class Exercise (12)
  • Class Exercise (13)
  • Class Exercise (14)
  • Class Exercise (15)
  • Class Exercise (16)
  • Class Exercise (17)
  • Class Exercise (18)
  • Class Exercise (19)
  • Class Exercise (20)
  • Class Exercise (21)
  • Class Exercise (22)
  • Class Exercise (23)
  • Class Exercise (24)
  • Class Exercise (25)
  • Class Exercise (26)
  • Class Exercise (27)
  • Class Exercise (28)
  • Class Exercise (29)
  • Class Exercise (30)
  • Class Exercise (31)
  • Class Exercise (32)
  • Class Exercise (33)
  • Class Exercise (34)
  • Class Exercise (35)
  • Class Exercise (36)
  • Class Exercise (37)
  • Class Exercise (38)
  • Class Exercise (39)
  • Class Exercise (40)
  • Class Exercise (41)
  • Class Exercise (42)
  • Class Exercise (43)
  • Class Exercise (44)
  • Class Exercise (45)
  • Class Exercise (46)
  • Class Exercise (47)
  • Class Exercise (48)
  • Class Exercise (49)
  • MergeSort Take-Aways
  • MergeSort Take-Aways (2)
  • Runtime of Merge Sort
  • Runtime of Merge Sort (2)
  • Summary of Sorting Algorithms (so far)
  • Continuing with Trees
  • What is a Tree
  • General Tree Terminology
  • Definitions Related to Trees
  • Exercise Trees
  • Tree ADT
  • A Linked Structure for General Trees
  • A Linked Structure for General Trees (2)
  • A Linked Structure for General Trees (3)
  • A Linked Structure for General Trees (4)
  • Binary Tree
  • BinaryTree ADT
  • A Linked Structure for Binary Trees
  • Alternate Picture of a binary tree
  • Some More Definitions
  • Some More Definitions (2)
  • Some More Definitions (3)
  • Some More Definitions (4)
  • Some More Definitions (5)
  • Some More Definitions (6)
  • Some More Definitions (7)
  • Some More Definitions (8)
  • Some More Definitions (9)
  • Some More Definitions (10)
  • Some More Definitions (11)
  • Some More Definitions (12)
  • Some More Definitions (13)
  • Some More Definitions (14)
  • Some More Definitions (15)
  • Some More Definitions (16)
  • Exercise Binary Tree
  • Sorted binary trees
  • Binary Search in a sorted array
  • The End