trees.py
def balance(self) -> None:
        """ Balance <self> so that there is at most a difference of 1 between
        the size of the _lt subtree and the size of the _gt subtree for all
        trees in <self>.

        === Precondition ===
        It is possible to balance this tree
        >>> tree = TwoDTree((0, 0), (500, 500))
        >>> tree.insert('a', (250, 250))
        >>> tree.insert('b', (100, 150))
        >>> tree.insert('c', (120, 200))
        >>> tree.insert('d', (150, 100))
        >>> tree.insert('e', (300, 300))
        >>> tree.balance()
        >>> tree.__getattribute__('_point')
        (150, 100)
        >>> tree.__getattribute__('_name')
        'd'
        >>> tree_l1 = tree.__getattribute__('_lt')
        >>> tree_l1.__getattribute__('_point')
        (120, 200)
        >>> tree_l2 = tree_l1.__getattribute__('_lt')
        >>> tree_l2.__getattribute__('_point')
        (100, 150)
        >>> tree_g1 = tree.__getattribute__('_gt')
        >>> tree_g1.__getattribute__('_point')
        (300, 300)
        >>> tree_g2 = tree_g1.__getattribute__('_lt')
        >>> tree_g2.__getattribute__('_point')
        (250, 250)
        >>> tree_g1.height() <= tree_l1.height() <= tree_g1.height() + 1
        True
        """
        if not self.is_leaf() and not (
                self._lt and self._gt and self._lt.is_leaf() and
                self._gt.is_leaf()):
            if (not self._lt or self._lt.is_leaf() or self._gt) and \
                    (not self._gt or not self._gt.is_leaf() or self._lt):
                lst_x, lst_y0 = self._build_list()
                lst_x.sort()
                lst_y0.sort()
                lst_y = []
                for item in lst_y0:
                    lst_y.append(((item[0][1], item[0][0]), item[1]))
                self._lt, self._gt = None, None
                self._point, self._name = None, None
                self._help_balance(lst_x, lst_y)
 Balance <self> so that there is at most a difference of 1 between
        the size of the _lt subtree and the size of the _gt subtree for all
        trees in <self>.

        === Precondition ===
        It is possible to balance this tree
        >>> tree = TwoDTree((0, 0), (500, 500))
        >>> tree.insert('a', (250, 250))
        >>> tree.insert('b', (100, 150))
        >>> tree.insert('c', (120, 200))
        >>> tree.insert('d', (150, 100))
        >>> tree.insert('e', (300, 300))
        >>> tree.balance()
        >>> tree.__getattribute__('_point')
        (150, 100)
        >>> tree.__getattribute__('_name')
        'd'
        >>> tree_l1 = tree.__getattribute__('_lt')
        >>> tree_l1.__getattribute__('_point')
        (120, 200)
        >>> tree_l2 = tree_l1.__getattribute__('_lt')
        >>> tree_l2.__getattribute__('_point')
        (100, 150)
        >>> tree_g1 = tree.__getattribute__('_gt')
        >>> tree_g1.__getattribute__('_point')
        (300, 300)
        >>> tree_g2 = tree_g1.__getattribute__('_lt')
        >>> tree_g2.__getattribute__('_point')
        (250, 250)
        >>> tree_g1.height() <= tree_l1.height() <= tree_g1.height() + 1
        True
        
Balance  so that there is at most a difference of 1 between
the size of the _lt subtree and the size of the _gt subtree for all
trees in .


Balance so that there is at most a difference of 1 between the size of the _lt subtree and the size of the _gt subtree for all trees in .
def balance(self) -> None:
        if not self.is_leaf() and not (
                self._lt and self._gt and self._lt.is_leaf() and
                self._gt.is_leaf()):
            if (not self._lt or self._lt.is_leaf() or self._gt) and \
                    (not self._gt or not self._gt.is_leaf() or self._lt):
                lst_x, lst_y0 = self._build_list()
                lst_x.sort()
                lst_y0.sort()
                lst_y = []
                for item in lst_y0:
                    lst_y.append(((item[0][1], item[0][0]), item[1]))
                self._lt, self._gt = None, None
                self._point, self._name = None, None
                self._help_balance(lst_x, lst_y)
def balance ( self ) -> None : if not self . is_leaf ( ) and not ( self . _lt and self . _gt and self . _lt . is_leaf ( ) and self . _gt . is_leaf ( ) ) : if ( not self . _lt or self . _lt . is_leaf ( ) or self . _gt ) and ( not self . _gt or not self . _gt . is_leaf ( ) or self . _lt ) : lst_x , lst_y0 = self . _build_list ( ) lst_x . sort ( ) lst_y0 . sort ( ) lst_y = [ ] for item in lst_y0 : lst_y . append ( ( ( item [ 0 ] [ 1 ] , item [ 0 ] [ 0 ] ) , item [ 1 ] ) ) self . _lt , self . _gt = None , None self . _point , self . _name = None , None self . _help_balance ( lst_x , lst_y )
Balance <self> so that there is at most a difference of 1 between
the size of the _lt subtree and the size of the _gt subtree for all
trees in <self>.
Balance <self > so that there is at most a difference of 1 between the size of the _lt subtree and the size of the _gt subtree for all trees in <self > .
""" Balance <self> so that there is at most a difference of 1 between
        the size of the _lt subtree and the size of the _gt subtree for all
        trees in <self>.

        === Precondition ===
        It is possible to balance this tree
        >>> tree = TwoDTree((0, 0), (500, 500))
        >>> tree.insert('a', (250, 250))
        >>> tree.insert('b', (100, 150))
        >>> tree.insert('c', (120, 200))
        >>> tree.insert('d', (150, 100))
        >>> tree.insert('e', (300, 300))
        >>> tree.balance()
        >>> tree.__getattribute__('_point')
        (150, 100)
        >>> tree.__getattribute__('_name')
        'd'
        >>> tree_l1 = tree.__getattribute__('_lt')
        >>> tree_l1.__getattribute__('_point')
        (120, 200)
        >>> tree_l2 = tree_l1.__getattribute__('_lt')
        >>> tree_l2.__getattribute__('_point')
        (100, 150)
        >>> tree_g1 = tree.__getattribute__('_gt')
        >>> tree_g1.__getattribute__('_point')
        (300, 300)
        >>> tree_g2 = tree_g1.__getattribute__('_lt')
        >>> tree_g2.__getattribute__('_point')
        (250, 250)
        >>> tree_g1.height() <= tree_l1.height() <= tree_g1.height() + 1
        True
        """
self