Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Unit 10 Overview

4 min readoctober 19, 2021

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway Of This Unit

Unit Overview

Exam Weighting

  • 5-7.5% of the test
  • Roughly 2 to 3 multiple-choice questions

Enduring Understanding

Sometimes, you can break down a large problem or task by doing a subproblem repeatedly. This is called . If this sounds like loops and iteration, it's because all recursive (the adjective form of ) methods can be written as a loop! We will learn how to use effectively and see how this will simplify our code!

Building Computational Thinking

When writing , notice how the code is much more simplified than if we were using loops. But, they will run slower, so we will sacrifice speed for conciseness in code. Recursive code has two main parts, the repeating/recursive part and the base case which makes sure we don't create an infinite loop in which our programs never stop.

Main Ideas for This Unit

  • Intro to
  • How to code recursive methods
  • Recursive Algorithms

10.1: Recursion

Intro to Recursion

 is a way to simplify problems by having a subproblem that calls itself repeatedly. A recursive method has two parts: a base case and the recursive call. In the recursive call, the method basically calls itself, telling it to start over, either with the same parameters or different ones. After multiple calls, we approach the base case, where the is stopped.

Here is its anatomy:

public static void recursiveMethod()
  if (baseCaseCondition) { // base case
    base case steps
  } else {
    do something
    recursiveMethod(); // recursive call
  }
}

The Base Case

The base case is the last recursive call. When the base case is reached, the is stopped and a value is returned. The base case is the easiest part of the recursive call to write and should be written first to make sure that the program doesn't run forever.

Recursive Calls and the Call Stack

The recursive calls are the different calls to the method. Each of the different recursive calls has different parameter values and leads up to the base case being reached. To write efficient , recognize what the subtask is and what is different between each time the subtask is done. The subtask is the recursive call and what is different and stays the same are the parameters.

Whenever we have a recursive method, we have a  that keeps track of all the times that the recursive function is called, as well as their individual parameters. This is useful for when we have a function that has a recursive call in its return statement where the results of later recursive calls are used in past calls. Here is a picture demonstrating the for .

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-OjrLI5sOdBWJ.png?alt=media&token=ed5983f8-f93c-4b58-ba4e-391cf4750757

Courtesy of Cornell University.

In this, x and e are the parameters of the recursive calls, and h is the result of the that is passed back up. The recursive calls are made from top to bottom, while the recursive returns are passed from bottom to top. This is where tracing comes in handy, as shown in the picture above!

Recursion vs. Loops

All can be written as a loop, that is, all recursive code can be written iteratively. If we can just use iteration, why use ? The main answer? Simplicity. Recursive code is usually easier to read than . Is there a trade-off? Yes. This comes in the form of speed and memory. This is because the takes up memory in your computer when running the program, and it takes time to pass up the recursive return values.

Here is an example of an iterative and recursive method to do multiplication using repeated addition:

:

public static int multiply(int a, int b) {
  int sum = 0;
  for (int i = 0; i < b; i++) {
    sum += a;
  }
  return sum;
}

Recursive Code

public static int multiply(int a, int b) {
  if (b == 0) {
    return 0;
  } else {
    return multiply(a, b - 1) + a;
  }
}

On the AP test FRQs, you can choose whether to write code iteratively or recursively, but you still have to know how works for the AP exam!

Recursive Traversals

We can write using . As a reminder, here is the :

for (int i = 0; i < 

Meanwhile, here is the recursive code:

public static void traverseArray(

What This Method Is Doing

The is actually getting the sublist of the big with the first item in the sublist acting as the next item to be printed, and the last item in the sublist acts as the last item in the . The base case is when there is only one item left in the sublist, at which point the last item is printed and the method stopped.

Key Terms to Review (5)

ArrayList

: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Call Stack

: The call stack is a data structure that keeps track of function calls in a program. It stores information about the order and location of function calls, allowing the program to keep track of where to return after each function call is completed.

Iterative Code

: Iterative code refers to programming constructs or algorithms that involve repetition using loops. It allows for executing a block of code multiple times until certain conditions are met.

Recursion

: Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller, similar subproblems.

Traversal

: Traversal refers to the process of visiting and accessing each element in a data structure, such as an array or a tree, in a specific order.

Unit 10 Overview

4 min readoctober 19, 2021

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway Of This Unit

Unit Overview

Exam Weighting

  • 5-7.5% of the test
  • Roughly 2 to 3 multiple-choice questions

Enduring Understanding

Sometimes, you can break down a large problem or task by doing a subproblem repeatedly. This is called . If this sounds like loops and iteration, it's because all recursive (the adjective form of ) methods can be written as a loop! We will learn how to use effectively and see how this will simplify our code!

Building Computational Thinking

When writing , notice how the code is much more simplified than if we were using loops. But, they will run slower, so we will sacrifice speed for conciseness in code. Recursive code has two main parts, the repeating/recursive part and the base case which makes sure we don't create an infinite loop in which our programs never stop.

Main Ideas for This Unit

  • Intro to
  • How to code recursive methods
  • Recursive Algorithms

10.1: Recursion

Intro to Recursion

 is a way to simplify problems by having a subproblem that calls itself repeatedly. A recursive method has two parts: a base case and the recursive call. In the recursive call, the method basically calls itself, telling it to start over, either with the same parameters or different ones. After multiple calls, we approach the base case, where the is stopped.

Here is its anatomy:

public static void recursiveMethod()
  if (baseCaseCondition) { // base case
    base case steps
  } else {
    do something
    recursiveMethod(); // recursive call
  }
}

The Base Case

The base case is the last recursive call. When the base case is reached, the is stopped and a value is returned. The base case is the easiest part of the recursive call to write and should be written first to make sure that the program doesn't run forever.

Recursive Calls and the Call Stack

The recursive calls are the different calls to the method. Each of the different recursive calls has different parameter values and leads up to the base case being reached. To write efficient , recognize what the subtask is and what is different between each time the subtask is done. The subtask is the recursive call and what is different and stays the same are the parameters.

Whenever we have a recursive method, we have a  that keeps track of all the times that the recursive function is called, as well as their individual parameters. This is useful for when we have a function that has a recursive call in its return statement where the results of later recursive calls are used in past calls. Here is a picture demonstrating the for .

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-OjrLI5sOdBWJ.png?alt=media&token=ed5983f8-f93c-4b58-ba4e-391cf4750757

Courtesy of Cornell University.

In this, x and e are the parameters of the recursive calls, and h is the result of the that is passed back up. The recursive calls are made from top to bottom, while the recursive returns are passed from bottom to top. This is where tracing comes in handy, as shown in the picture above!

Recursion vs. Loops

All can be written as a loop, that is, all recursive code can be written iteratively. If we can just use iteration, why use ? The main answer? Simplicity. Recursive code is usually easier to read than . Is there a trade-off? Yes. This comes in the form of speed and memory. This is because the takes up memory in your computer when running the program, and it takes time to pass up the recursive return values.

Here is an example of an iterative and recursive method to do multiplication using repeated addition:

:

public static int multiply(int a, int b) {
  int sum = 0;
  for (int i = 0; i < b; i++) {
    sum += a;
  }
  return sum;
}

Recursive Code

public static int multiply(int a, int b) {
  if (b == 0) {
    return 0;
  } else {
    return multiply(a, b - 1) + a;
  }
}

On the AP test FRQs, you can choose whether to write code iteratively or recursively, but you still have to know how works for the AP exam!

Recursive Traversals

We can write using . As a reminder, here is the :

for (int i = 0; i < 

Meanwhile, here is the recursive code:

public static void traverseArray(

What This Method Is Doing

The is actually getting the sublist of the big with the first item in the sublist acting as the next item to be printed, and the last item in the sublist acts as the last item in the . The base case is when there is only one item left in the sublist, at which point the last item is printed and the method stopped.

Key Terms to Review (5)

ArrayList

: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Call Stack

: The call stack is a data structure that keeps track of function calls in a program. It stores information about the order and location of function calls, allowing the program to keep track of where to return after each function call is completed.

Iterative Code

: Iterative code refers to programming constructs or algorithms that involve repetition using loops. It allows for executing a block of code multiple times until certain conditions are met.

Recursion

: Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller, similar subproblems.

Traversal

: Traversal refers to the process of visiting and accessing each element in a data structure, such as an array or a tree, in a specific order.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.