.py
files containing your work. (If you upload a screenshot or other file format, you won't get credit.)This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 8 March 2022.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
The course materials you may refer to for this homework are:
trees
subdirectory are of particular note.This homework assignment has two problems, numbered 2 and 3. The grading breakdown is:
Points | Item |
---|---|
2 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
10 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
In Gradescope, the score assigned to your homework submission by the autograder (checking for syntax and docstrings) will be recorded as "Problem 1". Therefore, the numbering of the actual problems begins with 2.
A node of a tree is called a leaf if it has no children.
Add a method leaves
to class Node
in hwk8.py
that, when called on the root of a tree, will find all leaf nodes in that tree and return a list of them. (So the return value should be a list of Node
objects.)
Inside class Node
:
def leaves(self):
'''Returns list of all leaf nodes'''
if self.left == None and self.right == None:
return [self] # If current node has no children, then it must be a leaf
elif self.left == None:
return self.right.leaves()
elif self.right == None:
return self.left.leaves()
else:
return self.left.leaves() + self.right.leaves() # `+` for list addition
Example of usage (in some other Python file):
import treevis
import treeutil
T = treeutil.random_bst(nodes=5,seed=21)
treevis.treeprint(T)
print("Leaves of tree T:")
print(T.leaves())
[]
since they are Node
objectsAdd a method positive_keys
to class BST
in hwk8.py
that, when called on the root of a binary search tree containing floats or integers, will return a list of all the positive keys that appear in the binary search tree.
Inside class BST
:
def positive_keys(self):
'''Returns list of keys strictly greater than 0'''
# First case: If current key is negative (or 0), no need to look to the left
if self.key <= 0:
if self.right != None: # Even if key is negative, we might need to look to the right
return self.right.positive_keys()
else:
return [] # Case where no positive keys were found
# Second case: If key is positive, then keep current key and check both left and right sides
else:
L = [self.key]
if self.left != None:
L.extend(self.left.positive_keys())
if self.right != None:
L.extend(self.right.positive_keys())
return L
Testing the method:
import trees
import treevis
# Build a tree with some arbitrary positive and negative values
T = trees.BST(10)
for k in [15, 4, -6, 13, 1, -3, 100, -50, 2, 11,12, 500]:
T.insert(k)
treevis.treeprint(T)
print("Positive keys of tree T:")
print(T.positive_keys())