{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# l05-add-del-sch - CS112 Data Structures Lecture 05...

This preview shows pages 1–14. Sign up to view the full content.

CS112: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 5 CS112: Data Structures CS112: Data Structures Lecture 05 Structures for search/add/delete

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 2 Lecture 5 Review: Recursion Review: Recursion Recursion is a way of looking at a problem EG problem: print pattern like * * * * * * * * * *
CS112: Slides for Prof. Steinberg ʼ s lecture 3 Lecture 5 Recursion Recursion Non-recursive view * * * * * * * * * * A size 4 triangle is four lines, lengths 1, 2, 3, 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 4 Lecture 5 Recursion Recursion Recursive view * * * * * * * * * * A size 4 triangle is A size 3 triangle, followed by A line of length 4
CS112: Slides for Prof. Steinberg ʼ s lecture 5 Lecture 5 Recursive Definitions in Math Recursive Definitions in Math Factorial n! = n * (n-1)! 1! = 1 e.g., 3! = 3*2! = 3 * (2 * 1!) = 3 * (2 * 1) = 6 Definition looks circular, but is not Two parts: recursive case base case

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 6 Lecture 5 Recursive Methods Recursive Methods Example: palindrome same letters backwards as forwards (assume no space or punctuation) e.g, radar madam im adam a man a plan a canal panama How can we write a method to test if a string is a palindrome?
CS112: Slides for Prof. Steinberg ʼ s lecture 7 Lecture 5 Recursive Definition Recursive Definition A string is a palindrome if first and last characters are the same, and r ada r rest of string without first and last is a palindrome ada A string of length 0 or 1 is a palindrome d See RecString.java

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 8 Lecture 5 Integer Power Integer Power 3 * 3 = 9 3 2 9 * 3 = 27 3 3 27 * 3 = 81 3 4 81 * 3 = 243 3 5 243 * 3 = 729 3 6 729 * 3 = 2187 3 7 2187 * 3 = 6561 3 8 7 *’s How many multiplies does it take to calculate 3 8 ? 3 * 3 = 9 3 2 9 * 9 = 81 3 4 81 * 81 = 6561 3 8 3 *’s 3 y/2 * 3 y/2 = 3 (y/2+y/2) = 3 y
CS112: Slides for Prof. Steinberg ʼ s lecture 9 Lecture 5 Recursive Definition Recursive Definition y even: x y = x y/2 * x y/2 = (x y/2 ) 2 y odd: x y = x * x y/2 * x y/2 = x * (x y/2 ) 2 y = 1: x y = x y = 0: x y = 1 See Power.java

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 10 Lecture 5 Seeing recursion look at a problem as if it were “pregnant”: inside it is a small version of the same problem Is “radar” a palindrome? is “ada” a palindrone?
CS112: Slides for Prof. Steinberg ʼ s lecture 11 Lecture 5 Designing Recursive Methods Designing Recursive Methods Print triangle of *s * ** *** **** Print a triangle size 4, Can you see how solving a similar but smaller problem would help solve this one?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 12 Lecture 5 Designing Recursive Methods Designing Recursive Methods Print triangle of *s * ** *** **** To print triangle size 4, print a triangle of size 3 print 4 stars
CS112: Slides for Prof. Steinberg ʼ s lecture 13 Lecture 5 Ruler Pattern Ruler Pattern * ** * *** * ** * **** * ** * *** * ** *

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 55

l05-add-del-sch - CS112 Data Structures Lecture 05...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online