Problem 3.2: Prune Small (100 pts)
Complete the function prune_small
that takes in a Tree
t
and a number n
and prunes t
mutatively. If t
or any of its branches has more than n
branches, the n
branches with the smallest labels should be kept and any other branches should be pruned, or removed, from the tree.
You can assume that a node will not have two children with the same label.
def prune_small(t, n):
"""Prune the tree mutatively, keeping only the n branches
of each node with the smallest label.
>>> t1 = Tree(6)
>>> prune_small(t1, 2)
>>> t1
Tree(6)
>>> t2 = Tree(6, [Tree(3), Tree(4)])
>>> prune_small(t2, 1)
>>> t2
Tree(6, [Tree(3)])
>>> t3 = Tree(6, [Tree(1), Tree(3, [Tree(1), Tree(2), Tree(3)]), Tree(5, [Tree(3), Tree(4)])])
>>> prune_small(t3, 2)
>>> t3
Tree(6, [Tree(1), Tree(3, [Tree(1), Tree(2)])])
"""
"*** YOUR CODE HERE ***"
Hint:
lst.pop(i)
can remove thei
th (start from 0) item inlst
.