Tree recursion
Count and list with tree recursion: binary tree shapes, binary strings without adjacent ones, and partitions into powers of two.
- Lessons
- Tree recursion
This discussion goes with Lesson 8. The question to ask every time: what is the first choice, and what problem is left after each option?
Q1: What would Swift print?
What would Swift print?
var calls = 0
func count(_ n: Int) -> Int {
calls += 1
if n <= 1 {
return 1
}
return count(n - 1) + count(n - 2)
}
print(count(4), calls)Show answer and explanation
Answer5 9
count is the stairs function: count(4) is 5. Drawing the call tree for
count(4) shows 9 nodes, since count(2) and smaller are recomputed.
Q2: Shapes of binary trees
A binary tree with n nodes has a root, a left subtree with k nodes, and a
right subtree with n − 1 − k nodes, for any k from 0 to n − 1. Write
numTrees(_:), the number of different shapes of binary trees with n
nodes. There is one shape with 0 nodes (the empty tree) and one with 1.
func numTrees(_ n: Int) -> Int {
// your code here
}
Show solution
func numTrees(_ n: Int) -> Int {
if n <= 1 {
return 1
}
var total = 0
for left in 0..<n {
total += numTrees(left) * numTrees(n - 1 - left)
}
return total
}
assert(numTrees(2) == 2)
assert(numTrees(3) == 5)
assert(numTrees(8) == 1430)For each split, every left shape can pair with every right shape, so the counts multiply; different splits are different trees, so they add. These are the Catalan numbers.
Q3: Binary strings without adjacent ones
Count the strings of 0s and 1s of length n in which no two 1s are next to
each other, then print them in order.
func countNoAdjacentOnes(_ n: Int) -> Int {
// your code here
}
func printNoAdjacentOnes(_ n: Int, _ prefix: String = "") {
// your code here
}
Show solution
func countNoAdjacentOnes(_ n: Int, afterOne: Bool = false) -> Int {
if n == 0 {
return 1
}
let withZero = countNoAdjacentOnes(n - 1, afterOne: false)
let withOne = afterOne ? 0 : countNoAdjacentOnes(n - 1, afterOne: true)
return withZero + withOne
}
func printNoAdjacentOnes(_ n: Int, _ prefix: String = "") {
if n == 0 {
print(prefix)
return
}
printNoAdjacentOnes(n - 1, prefix + "0")
if !prefix.hasSuffix("1") {
printNoAdjacentOnes(n - 1, prefix + "1")
}
}
assert(countNoAdjacentOnes(1) == 2)
assert(countNoAdjacentOnes(3) == 5)
assert(countNoAdjacentOnes(6) == 21)
printNoAdjacentOnes(3) // 000 001 010 100 101, one per lineA 0 can always come next; a 1 only if the last character was not a 1. The counting version tracks that with a flag, and the printing version by looking at the prefix. The counts are Fibonacci numbers.
Q4: Partitions into powers of two
In how many ways can n be written as a sum of powers of two (1, 2, 4, 8, …),
ignoring order? For example, 4 can be 4, 2 + 2, 2 + 1 + 1, or 1 + 1 + 1 + 1:
four ways. Write binaryPartitions(_:).
func binaryPartitions(_ n: Int) -> Int {
// your code here
}
Show solution
func binaryPartitions(_ n: Int) -> Int {
func count(_ n: Int, _ largest: Int) -> Int {
if n == 0 {
return 1
}
if largest == 0 {
return 0
}
let usingLargest = largest <= n ? count(n - largest, largest) : 0
return usingLargest + count(n, largest / 2)
}
var largest = 1
while largest * 2 <= n {
largest *= 2
}
return count(n, largest)
}
assert(binaryPartitions(0) == 1)
assert(binaryPartitions(4) == 4)
assert(binaryPartitions(7) == 6)
assert(binaryPartitions(10) == 14)This is countPartitions from Lesson 8 with powers of two as the parts. The
next smaller part after largest is largest / 2, and 1 / 2 is 0, which
ends the parts.