Slippery Rock University Dr. Deborah Whitfield Go Browns!

Chapter 6: Recursion
Data Structures and Algorithms in Java
CpSc 374

Topics
  1. Review tail recursion from 246: Factorial, Fibonacci, Hanoi
  2. Look at the code for Anagrams
  3. Look at the code for recursive Binary Search
  4. Towers of Hanoi Analysis
  5. Study the Merge sort
  6. Using stack in place of recursion
  7. Look at the Knapsack problem

Recursion Review - Tail Recursion

Top
Anagrams
  1. One Letter - A
  2. Two Letters - A and B
    1. Start with first letter followed by second
      AB
    2. Rotate the first letter A to the end of the two letters and then apply one letter case
      A to end and then one letter case with B: AB, BA
  3. Three Letters
    1. apply #2 (two-letter case to the end)
    2. Rotate the first letter to the end
    3. repeat
      ABC, ACB, BCA, BAC, CAB, CBA
  4. Four Letters
    1. apply 3 letter solution
    2. Rotate
    3. repeat
  5. N Letters
    1. Rotate base word
    2. apply n-1 letter solution
  • Author's anagram code
  • Can you analyze the code? Top

    Recursive Binary Search

    Towers of Hanoi Revisted


    Merge Sort Top


    Eliminating recursion by using stacks

    Top

    The Knapsack Problem

    Top