from typing import TypeVar, Generic, Optional import run_tests T = TypeVar( 'T' ) class Tree( Generic[ T ] ): def __init__( self, value: T ) -> None: self.left : Optional[ Tree[ T ] ] = None self.right : Optional[ Tree[ T ] ] = None self.value = value def rotate_left( self ) -> Any: assert self.left is not None r = self.left detach = r.right r.right = self self.left = detach return r def rotate_right( self ) -> Any: assert self.right is not None r = self.right detach = r.left r.left = self self.right = detach return r