import run_tests from r3_itree import T class TreeIter( Generic[ T ] ): def __init__( self, tree: Tree[ T ] ) -> None: self.n : Optional[ Tree[ T ] ] = tree def descend( self ) -> None: assert self.n is not None while self.n.left is not None: self.n = self.n.left def ascend( self ) -> None: assert self.n is not None while ( self.n.parent is not None and self.n == self.n.parent.right ): self.n = self.n.parent self.n = self.n.parent # coming from left def __iter__( self ) -> TreeIter[ T ]: assert self.n is not None i = TreeIter( self.n ) i.descend() return i def __next__( self ) -> T: if self.n is None: raise StopIteration() assert self.n is not None # srsly v = self.n.value if self.n.right is not None: self.n = self.n.right self.descend() else: self.ascend() return v