More Practice with Recursion
> Click or hit Control-Enter to run Example.main above
Welcome Back!
I hope you all had a restful weekend.
Recursive Tree Right Greater Than Left
Let’s find the number of nodes in our tree where the value of the right child is greater than or equal to value of the left child.
What’s Our (Recursive) Algorithm?
Recursive Tree Right Greater Than Left
-
Base case: We’ve reached a tree with one node. It does not have a right child or left child, so we can return 0.
-
Recursive step: Consider our right tree and left tree separately.
-
Combine results: Determine whether our right child is greater than our left child. If so, add 1 to the sum of results from our left and right child.
> Click or hit Control-Enter to run Example.main above
Recursive Tree Print Leaves
Let’s print only leaf nodes: those that have neither a left nor a right child.
What’s Our (Recursive) Algorithm?
Recursive Tree Print Leaves
-
Base case: We’ve reached a tree with no left or right node. It’s also a a leaf node, so print our value.
-
Recursive step: Consider our right tree and left tree separately.
-
Combine results: Nothing to do here, just continue down the tree.
> Click or hit Control-Enter to run Example.main above
Recursive Tree Search
Let’s determine whether a tree contains a certain value.
What’s Our (Recursive) Algorithm?
Recursive Tree Search
-
Base case: We’ve reached a node with no descendants. Return true if its value matches, zero otherwise.
-
Recursive step: Consider our right tree and left tree separately.
-
Combine results: Return true if either we or our right or left subtree contain the search value.
> Click or hit Control-Enter to run Example.main above
How Could We Make This Search More Efficient?
> Click or hit Control-Enter to run Example.main above
Other Recursive Data Structures
Every sub(blank) of a (blank) is, itself, a (blank).
-
Tree
-
(Contiguous) List
-
(Contiguous) Array
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
public class Item {
public int value;
public Item next;
Item(int setValue, Item setNext) {
value = setValue;
next = setNext;
}
}
List Recursion
Just like with trees, we need a way to both make the problem smaller and identify the smallest subproblem.
-
How do we make the problem smaller? Break the list into the current item and the rest of the list.
-
What’s the smallest subproblem? A list with a single element.
Linked List Reversal
Given a linked list, reverse the elements in the list.
What’s Our (Recursive) Algorithm?
Linked List Reversal
-
Base case: We’ve reached a list with one element. No need to do anything, since it’s the same reversed.
-
Recursive step: Consider the current element and the rest of the list separately.
-
Combine results: Add the current element as the last element of the reversal of the rest of the list.
> Click or hit Control-Enter to run Example.main above
This Is Tricky
Recursive List Reversal
// Set ourselves as the end of the list
current.next.next = current;
// Mark that the item after us is null
current.next = null;
-
At this point we’ve reversed the end of the list.
-
But
Item6
still has a reference to what was its next element, but is now the end of the rest of the list. -
So we can use that to set it as the last item.
Recursive List Reversal
// Set ourselves as the end of the list
current.next.next = this;
// Mark that the item after us is null
current.next = null;
-
At this point we’ve reversed the end of the list.
-
But
Item6
still has a reference to what was its next element, but is now the end of the rest of the list. -
So we can use that to set it as the last item.
Recursive List Reversal
// Set ourselves as the end of the list
current.next.next = this;
// Mark that the item after us is null
current.next = null;
-
At this point we’ve reversed the end of the list.
-
But
Item6
still has a reference to what was its next element, but is now the end of the rest of the list. -
So we can use that to set it as the last item.
MP5
MP5 is out. It’s our first (and only) completely new MP this semester.
-
A huge shout out to Ben Nordick and Bailey Tincher: the two course developers who led the development of this fantastic MP.
-
MP5 is new, meaning the course assistants have not yet completed it. As always, the course staff will be working as hard as possible to support MP5.
-
MP5 is not intended to be easy, but will be extremely worthwhile to complete. It introduces you to a new and extremely powerful data structure: graphs.
-
MP5 is not about chemistry. It’s about computer science. The core programming tasks are common graph algorithms.
Announcements
-
MP5 is out! Get started.
-
I have office hours today from 10AM–12PM. Please come by!
-
Note that we will have class on the Friday before Thanksgiving but not on the Wednesday before Thanksgiving. As usual, the calendar is up-to-date.
-
We’ve added an anonymous feedback form to the course website. Use it to give us feedback!