June 2019
Beginner to intermediate
770 pages
19h 24m
English
We'll start with the wrapper or Façade class, Tree. This is the core of an extension to the MutableSet class that provides the minimal method functions:
class Tree(collections.abc.MutableSet): def __init__(self, source: Iterable[Comparable] = None) -> None: self.root = TreeNode(None) self.size = 0 if source: for item in source: self.root.add(item) self.size += 1 def add(self, item: Comparable) -> None: self.root.add(item) self.size += 1 def discard(self, item: Comparable) -> None: if self.root.more: try: self.root.more.remove(item) self.size -= 1 except KeyError: pass else: pass def __contains__(self, item: Any) -> bool: if self.root.more: self.root.more.find(cast(Comparable, item)) return True else: return False
Read now
Unlock full access