class Tree
class Tree: def __init__(self, entry, branches=()): self.entry = entry for branch in branches:
assert isinstance(branch, Tree)
self.branches = list(branches)
def __repr__(self):
if self.branches:
branches_str = ‘, ‘ + repr(self.branches)
else:
branches_str = ”
return ‘Tree({0}{1})’.format(self.entry, branches_str)
def __str__(self):
def print_tree(t, indent=0):
tree_str = ‘ ‘ * indent + str(t.entry) + “n”
for b in t.branches:
tree_str += print_tree(b, indent + 1)
return tree_str
return print_tree(self).rstrip()
def is_leaf(self):
return not self.branches
def search(t, value):
“””Searches for and returns the Tree whose entry is equal to value if
it exists and None if it does not. Assume unique entries.
>>> t = Tree(1, [Tree(3, [Tree(5)]), Tree(7)])
>>> search(t, 10)
>>> search(t, 5)
Tree(5)
>>> search(t, 1)
Tree(1, [Tree(3, [Tree(5)]), Tree(7)])
“””
I am trying to finish the search function and am having no luck. Any help would be greatly appreciated! Listed above is the implementation of the tree, hope that helps for references.
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"
