Recall that the height of a tree is the length of the longest path from the root to a leaf.
defheight(t):"""Return the height of a tree.
>>> t = Tree(3, [Tree(5, [Tree(1)]), Tree(2)])
>>> height(t)
2
>>> t = Tree(3, [Tree(1), Tree(2, [Tree(5, [Tree(6)]), Tree(1)])])
>>> height(t)
3
""""*** YOUR CODE HERE ***"
You can use linked lists to create your own version of a sequence
They are generally useful when you want to have an infinitely-sized list, or want to dynamically change the size of the list (more useful in 61B if you do end up taking it)
In general, linked lists problems can be solved using both iteration and recursion
Like trees, they are recursive data structures, but unlike trees, you can use both recursion and iteration to solve them.
Linked Lists (Anatomy)
classLink:
empty = ()
def__init__(self, first, rest = Link.empty):assert rest is Link.empty orisinstance(rest, Link)
self.first = first
self.rest = rest
Linked lists have a first (similar to label) attribute and a rest (similar-ish to branches) attribute.
Linked Lists (Construction)
Note to Anto: Draw box-and-pointer diagram
s = Link(1, Link(2, Link(3)))
>>> s.first
1>>> s.rest
Link(2, Link(3))
>>> s.rest.first
2
s2 = Link(1, 2) # This will error because rest is not a linked list>>> s.rest.rest.first
3
Question 5: WWPD
>>> link = Link(1, Link(2, Link(3)))
>>> link.first
>>> link.rest.first
>>> link.rest.rest.rest is Link.empty
>>> link.rest = link.rest.rest
>>> link.rest.first
>>> link = Link(1)
>>> link.rest = link
>>> link.rest.rest.rest.rest.first
>>> link = Link(2, Link(3, Link(4)))
>>> link2 = Link(1, link)
>>> link2.first
>>> link2.rest.first
Mental Health Resources
CAPS:
If you need to talk to a professional, please call CAPS at 510-642-9494.
After Hours Assistance
For any assistance after hours, details on what to do can be found at this link