Trees
Work with trees as recursive data: find the largest label, the path to a label, the labels at a depth, and grow new leaves.
- Lessons
- Linked lists and trees
This discussion goes with Lesson 13. Every problem uses this tree type:
struct Tree {
var label: Int
var branches: [Tree] = []
var isLeaf: Bool { branches.isEmpty }
}
and this example tree, drawn with the root at the top:
1
/ \
2 3
|
4
Q1: What would Swift print?
What would Swift print?
struct Tree {
var label: Int
var branches: [Tree] = []
}
let t = Tree(label: 1, branches: [Tree(label: 2, branches: [Tree(label: 4)]), Tree(label: 3)])
print(t.branches.count, t.branches[0].branches[0].label, t.branches.map(\.label))Show answer and explanation
Answer2 4 [2, 3]
The root has two branches. Following the first branch and then its first branch reaches 4. The branches’ own labels are 2 and 3.
Q2: The largest label
Write treeMax(_:), the largest label in a tree.
func treeMax(_ t: Tree) -> Int {
// your code here
}
Show solution
struct Tree {
var label: Int
var branches: [Tree] = []
}
func treeMax(_ t: Tree) -> Int {
t.branches.map(treeMax).reduce(t.label, max)
}
let t = Tree(label: 1, branches: [Tree(label: 2, branches: [Tree(label: 4)]), Tree(label: 3)])
assert(treeMax(t) == 4)
assert(treeMax(Tree(label: -5)) == -5)reduce(t.label, max) starts from this node’s label and keeps the larger of
it and each branch’s maximum. max is passed as a function, like +.
Q3: The path to a label
Write findPath(_:_:), which returns the labels from the root down to the
first node labeled target (searching branches left to right), or nil if
there is none.
func findPath(_ t: Tree, _ target: Int) -> [Int]? {
// your code here
}
Show solution
struct Tree {
var label: Int
var branches: [Tree] = []
}
func findPath(_ t: Tree, _ target: Int) -> [Int]? {
if t.label == target {
return [t.label]
}
for branch in t.branches {
if let path = findPath(branch, target) {
return [t.label] + path
}
}
return nil
}
let t = Tree(label: 1, branches: [Tree(label: 2, branches: [Tree(label: 4)]), Tree(label: 3)])
assert(findPath(t, 4) == [1, 2, 4])
assert(findPath(t, 1) == [1])
assert(findPath(t, 9) == nil)The optional carries the answer to “was it found in this branch?”. The first branch that returns a path wins, and the root’s label goes in front.
Q4: Labels at a depth
The root is at depth 0, its branches at depth 1, and so on. Write
labelsAtDepth(_:_:), returning the labels at a given depth, left to right.
func labelsAtDepth(_ t: Tree, _ depth: Int) -> [Int] {
// your code here
}
Show solution
struct Tree {
var label: Int
var branches: [Tree] = []
}
func labelsAtDepth(_ t: Tree, _ depth: Int) -> [Int] {
if depth == 0 {
return [t.label]
}
return t.branches.flatMap { labelsAtDepth($0, depth - 1) }
}
let t = Tree(label: 1, branches: [Tree(label: 2, branches: [Tree(label: 4)]), Tree(label: 3)])
assert(labelsAtDepth(t, 1) == [2, 3])
assert(labelsAtDepth(t, 2) == [4])
assert(labelsAtDepth(t, 5) == [])Depth d in this tree is depth d − 1 in each branch. A leaf has no
branches, so asking for more depth than exists gives an empty array rather
than an error.
Q5: Sprout leaves
Write sproutLeaves(_:_:), which returns a new tree where every leaf has new
branches, one leaf for each value in leaves.
func sproutLeaves(_ t: Tree, _ leaves: [Int]) -> Tree {
// your code here
}
Show solution
struct Tree {
var label: Int
var branches: [Tree] = []
var isLeaf: Bool { branches.isEmpty }
}
func labels(_ t: Tree) -> [Int] {
[t.label] + t.branches.flatMap(labels)
}
func sproutLeaves(_ t: Tree, _ leaves: [Int]) -> Tree {
if t.isLeaf {
return Tree(label: t.label, branches: leaves.map { Tree(label: $0) })
}
return Tree(label: t.label, branches: t.branches.map { sproutLeaves($0, leaves) })
}
let t = Tree(label: 1, branches: [Tree(label: 2, branches: [Tree(label: 4)]), Tree(label: 3)])
let grown = sproutLeaves(t, [7, 8])
assert(labels(grown) == [1, 2, 4, 7, 8, 3, 7, 8])
assert(labels(sproutLeaves(Tree(label: 0), [1])) == [0, 1])Only leaves change; every other node is rebuilt with its branches sprouted.
The original t is untouched, because Tree is a structure.