Exam Prep 4: Trees
Students from past semesters wanted more content and structured time to prepare for exams. Exam Prep sections are a way to solidify your understanding of the week's materials. The problems are typically designed to be a bridge between discussion/lab/homework difficulty and exam difficulty.
Reminder: There is nothing to turn in and there is no credit given for attending Exam Prep Sections.
We try to make these problems exam level , so you are not expected to be able to solve them coming straight from lecture without additional practice. To get the most out of Exam Prep, we recommend you try these problems first on your own before coming to the Exam Prep section, where we will explain how to solve these problems while giving tips and advice for the exam. Do not worry if you struggle with these problems, it is okay to struggle while learning.
You can work with anyone you want, including sharing solutions. We just ask you don't spoil the problems for anyone else in the class. Thanks!
You may only put code where there are underscores for the codewriting questions.
You can test your functions on their doctests by clicking the red test tube in the top right corner after clicking Run in 61A Code. Passing the doctests is not necessarily enough to get the problem fully correct. You must fully solve the stated problem.
Q1: Perfectly Balanced and Pruned
Difficulty: ⭐
Part A: Implement sum_tree
, which takes adds together all the labels in a tree.
Part B: Implement balanced
, a function which takes in a tree and returns whether each of the branches have the same total sum, and each branch is balanced.
Part C: Implement prune_tree
, a function which takes in a tree t
as well as a function predicate
that returns True or False for each label of each tree node. prune_tree
returns a new tree where any node for which predicate
of the label of that node returns True, then all the branches for that tree are pruned (not included in the new tree).
IMPORTANT: You may use as many lines as you want for these two parts.
Challenge: Solve both of these parts with just 1 line of code each.
Run in 61A CodeQ2: Closest - Spring 2015 Midterm 2 Q3(c)
IMPORTANT: For this problem, you will be given time during the Exam Prep section to solve on your own before we go over it.
Difficulty: ⭐⭐
Implement closest
, which takes a tree of numbers t
and returns the smallest absolute difference anywhere in the tree between an entry and the sum of the entries of its branches. The sum only compares the any node value to the sum of its branches, or nodes one level below that node.
The built-in min
function takes a sequence and returns its minimum value.
Reminder: A branch of a branch of a tree t is not considered to be a branch of t.
Run in 61A CodeQ3: Recursion on Tree ADT - Summer 2014 Midterm 1 Q7
Difficulty: ⭐⭐
Define a function dejavu
, which takes in a tree of numbers t
and a number n
. It returns True
if there is a path from the root to a leaf such that the sum of the numbers along that path is n
and False
otherwise.
IMPORTANT: For this problem, the starter code template is just a suggestion. You are welcome to add/delete/modify the starter code template, or even write your own solution that doesn’t use the starter code at all.
Run in 61A CodeQ4: Forest Path - Fall 2015 Final Q3 (a)(b)(d)
Difficulty: ⭐⭐⭐
Definition: A path through a tree
is a list of adjacent node values that starts with the root value and ends with a leaf value. For example, the paths of tree(1, [tree(2), tree(3, [tree(4), tree(5)])])
are
[1, 2]
[1, 3, 4]
[1, 3, 5]
Part A: Implement bigpath
, which takes a tree t
and an integer n
. It returns the number of paths
in t
whose sum is at least n
. Assume that all node values of t
are integers.
Part B: Implement allpath
which takes a tree t
, a one-argument predicate f
, a two-argument reducing function g
, and a starting value s
. It returns the number of paths p
in t
for which f(reduce(g, p, s))
returns a truthy value. The reduce
function is in the code. Pay close attention to the order of arguments to the f
function in reduce
. You do not need to call it, though.
Part C: Re-implement bigpath
(Part A) using allpath
(Part B). Assume allpath
is implemented correctly.
Q5: Extra Practice
Difficulty: >⭐⭐⭐