'''simple docstring'''




from __future__ import annotations

def _UpperCamelCase (							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				):
     assert (
         c % greatest_common_divisor(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				) == 0
     )  # greatest_common_divisor(a,b) function implemented below
     ((lowercase_)					,(lowercase_)					,(lowercase_))       :							List[str]  					=       extended_gcd(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				)  # extended_gcd(a,b) function implemented below
     lowercase_       :							Dict  					=       c / d
     return (r * x, r * y)

def _UpperCamelCase (							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_ = 2				):
     ((lowercase_)					,(lowercase_))       :							List[Any]  					=       diophantine(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				)  # Initial value
     lowercase_       :							List[str]  					=       greatest_common_divisor(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				)
     lowercase_       :							Union[str, Any]  					=       a // d
     lowercase_       :							Tuple  					=       b // d

     for i in range(SCREAMING_SNAKE_CASE_				):
          lowercase_       :							Any  					=       xa + i * q
          lowercase_       :							Any  					=       ya - i * p
          print(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				)

def _UpperCamelCase (							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				):
     if a < b:
          lowercase_					,lowercase_       :							str  					=       b, a

     while a % b != 0:
          lowercase_					,lowercase_       :							Optional[int]  					=       b, a % b

     return b

def _UpperCamelCase (							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				):
     assert a >= 0 and b >= 0

     if b == 0:
          lowercase_					,lowercase_					,lowercase_       :							Optional[Any]  					=       a, 1, 0
     else:
          ((lowercase_)					,(lowercase_)					,(lowercase_))       :							Any  					=       extended_gcd(SCREAMING_SNAKE_CASE_	,							a % b				)
          lowercase_       :							str  					=       q
          lowercase_       :							str  					=       p - q * (a // b)

     assert a % d == 0 and b % d == 0
     assert d == a * x + b * y

     return (d, x, y)


if __name__ == "__main__":
 from doctest import testmod

 testmod(name='diophantine', verbose=True)
 testmod(name='diophantine_all_soln', verbose=True)
 testmod(name='extended_gcd', verbose=True)
 testmod(name='greatest_common_divisor', verbose=True)





'''simple docstring'''




from __future__ import annotations

import unittest
from collections.abc import Iterator


class    UpperCAmelCase__	:






   """simple docstring"""
   def __init__(self							,					_a							,					_a					)     ->  None:
        lowercase_       :							Union[str, Any]  					=       label
        lowercase_       :							List[str]  					=       parent
        lowercase_       :							Node | None  					=       None
        lowercase_       :							Node | None  					=       None



class    UpperCAmelCase__	:






   """simple docstring"""
   def __init__(self					)     ->  None:
        lowercase_       :							Node | None  					=       None
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							int  					=       None
   def 		_lowerCamelCase			(self					)     ->  bool:
        return self.root is None
   def 		_lowerCamelCase			(self							,					_a					)     ->  None:
        lowercase_       :							Any  					=       self._put(self.root							,					_a					)
   def 		_lowerCamelCase			(self							,					_a							,					_a							,					_a = None					)     ->  Node:
        if node is None:
             lowercase_       :							Union[str, Any]  					=       Node(_a							,					_a					)
        else:
             if label < node.label:
                  lowercase_       :							Optional[int]  					=       self._put(node.left							,					_a							,					_a					)
             elif label > node.label:
                  lowercase_       :							List[str]  					=       self._put(node.right							,					_a							,					_a					)
             else:
                  lowercase_       :							Optional[int]  					=       f'''Node with label {label} already exists'''
                  raise Exception(_a					)

        return node
   def 		_lowerCamelCase			(self							,					_a					)     ->  Node:
        return self._search(self.root							,					_a					)
   def 		_lowerCamelCase			(self							,					_a							,					_a					)     ->  Node:
        if node is None:
             lowercase_       :							Optional[int]  					=       f'''Node with label {label} does not exist'''
             raise Exception(_a					)
        else:
             if label < node.label:
                  lowercase_       :							int  					=       self._search(node.left							,					_a					)
             elif label > node.label:
                  lowercase_       :							str  					=       self._search(node.right							,					_a					)

        return node
   def 		_lowerCamelCase			(self							,					_a					)     ->  None:
        lowercase_       :							List[str]  					=       self.search(_a					)
        if node.right and node.left:
             lowercase_       :							int  					=       self._get_lowest_node(node.right					)
             lowercase_       :							Any  					=       node.left
             lowercase_       :							str  					=       node.right
             lowercase_       :							Any  					=       lowest_node
             if node.right:
                  lowercase_       :							Any  					=       lowest_node
             self._reassign_nodes(_a							,					_a					)
        elif not node.right and node.left:
             self._reassign_nodes(_a							,					node.left					)
        elif node.right and not node.left:
             self._reassign_nodes(_a							,					node.right					)
        else:
             self._reassign_nodes(_a							,					_a					)
   def 		_lowerCamelCase			(self							,					_a							,					_a					)     ->  None:
        if new_children:
             lowercase_       :							List[str]  					=       node.parent

        if node.parent:
             if node.parent.right == node:
                  lowercase_       :							str  					=       new_children
             else:
                  lowercase_       :							Tuple  					=       new_children
        else:
             lowercase_       :							Union[str, Any]  					=       new_children
   def 		_lowerCamelCase			(self							,					_a					)     ->  Node:
        if node.left:
             lowercase_       :							Optional[int]  					=       self._get_lowest_node(node.left					)
        else:
             lowercase_       :							str  					=       node
             self._reassign_nodes(_a							,					node.right					)

        return lowest_node
   def 		_lowerCamelCase			(self							,					_a					)     ->  bool:
        try:
             self.search(_a					)
             return True
        except Exception:
             return False
   def 		_lowerCamelCase			(self					)     ->  int:
        if self.root is None:
             raise Exception('Binary search tree is empty'					)

        lowercase_       :							Tuple  					=       self.root
        while node.right is not None:
             lowercase_       :							Union[str, Any]  					=       node.right

        return node.label
   def 		_lowerCamelCase			(self					)     ->  int:
        if self.root is None:
             raise Exception('Binary search tree is empty'					)

        lowercase_       :							Optional[Any]  					=       self.root
        while node.left is not None:
             lowercase_       :							Optional[int]  					=       node.left

        return node.label
   def 		_lowerCamelCase			(self					)     ->  Iterator[Node]:
        return self._inorder_traversal(self.root					)
   def 		_lowerCamelCase			(self							,					_a					)     ->  Iterator[Node]:
        if node is not None:
             yield from self._inorder_traversal(node.left					)
             yield node
             yield from self._inorder_traversal(node.right					)
   def 		_lowerCamelCase			(self					)     ->  Iterator[Node]:
        return self._preorder_traversal(self.root					)





   def 		_lowerCamelCase			(self							,					_a					)     ->  Iterator[Node]:
        if node is not None:
             yield node
             yield from self._preorder_traversal(node.left					)
             yield from self._preorder_traversal(node.right					)



class    UpperCAmelCase__	(							unittest.TestCase      ):






   """simple docstring"""
   @staticmethod
   def 		_lowerCamelCase			()     ->  BinarySearchTree:
        lowercase_       :							int  					=       BinarySearchTree()
        t.put(8					)
        t.put(3					)
        t.put(6					)
        t.put(1					)
        t.put(10					)
        t.put(14					)
        t.put(13					)
        t.put(4					)
        t.put(7					)
        t.put(5					)

        return t
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							List[Any]  					=       BinarySearchTree()
        assert t.is_empty()

        t.put(8					)
        assert t.root is not None
        assert t.root.parent is None
        assert t.root.label == 8

        t.put(10					)
        assert t.root.right is not None
        assert t.root.right.parent == t.root
        assert t.root.right.label == 10

        t.put(3					)
        assert t.root.left is not None
        assert t.root.left.parent == t.root
        assert t.root.left.label == 3

        t.put(6					)
        assert t.root.left.right is not None
        assert t.root.left.right.parent == t.root.left
        assert t.root.left.right.label == 6

        t.put(1					)
        assert t.root.left.left is not None
        assert t.root.left.left.parent == t.root.left
        assert t.root.left.left.label == 1

        with self.assertRaises(_a					):  # noqa: B017
             t.put(1					)
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							List[Any]  					=       self._get_binary_search_tree()

        lowercase_       :							int  					=       t.search(6					)
        assert node.label == 6

        lowercase_       :							str  					=       t.search(13					)
        assert node.label == 13

        with self.assertRaises(_a					):  # noqa: B017
             t.search(2					)
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Any  					=       self._get_binary_search_tree()

        t.remove(13					)
        assert t.root is not None
        assert t.root.right is not None
        assert t.root.right.right is not None
        assert t.root.right.right.right is None
        assert t.root.right.right.left is None

        t.remove(7					)
        assert t.root.left is not None
        assert t.root.left.right is not None
        assert t.root.left.right.left is not None
        assert t.root.left.right.right is None
        assert t.root.left.right.left.label == 4

        t.remove(6					)
        assert t.root.left.left is not None
        assert t.root.left.right.right is not None
        assert t.root.left.left.label == 1
        assert t.root.left.right.label == 4
        assert t.root.left.right.right.label == 5
        assert t.root.left.right.left is None
        assert t.root.left.left.parent == t.root.left
        assert t.root.left.right.parent == t.root.left

        t.remove(3					)
        assert t.root is not None
        assert t.root.left.label == 4
        assert t.root.left.right.label == 5
        assert t.root.left.left.label == 1
        assert t.root.left.parent == t.root
        assert t.root.left.left.parent == t.root.left
        assert t.root.left.right.parent == t.root.left

        t.remove(4					)
        assert t.root.left is not None
        assert t.root.left.left is not None
        assert t.root.left.label == 5
        assert t.root.left.right is None
        assert t.root.left.left.label == 1
        assert t.root.left.parent == t.root
        assert t.root.left.left.parent == t.root.left
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Union[str, Any]  					=       self._get_binary_search_tree()

        t.remove(3					)
        assert t.root is not None
        assert t.root.left is not None
        assert t.root.left.left is not None
        assert t.root.left.right is not None
        assert t.root.left.right.left is not None
        assert t.root.left.right.right is not None
        assert t.root.left.label == 4
        assert t.root.left.right.label == 6
        assert t.root.left.left.label == 1
        assert t.root.left.right.right.label == 7
        assert t.root.left.right.left.label == 5
        assert t.root.left.parent == t.root
        assert t.root.left.right.parent == t.root.left
        assert t.root.left.left.parent == t.root.left
        assert t.root.left.right.left.parent == t.root.left.right
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Union[str, Any]  					=       self._get_binary_search_tree()
        t.empty()
        assert t.root is None
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							str  					=       self._get_binary_search_tree()
        assert not t.is_empty()

        t.empty()
        assert t.is_empty()
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							List[Any]  					=       self._get_binary_search_tree()

        assert t.exists(6					)
        assert not t.exists(-1					)
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Dict  					=       self._get_binary_search_tree()

        assert t.get_max_label() == 14

        t.empty()
        with self.assertRaises(_a					):  # noqa: B017
             t.get_max_label()
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Tuple  					=       self._get_binary_search_tree()

        assert t.get_min_label() == 1

        t.empty()
        with self.assertRaises(_a					):  # noqa: B017
             t.get_min_label()
   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Tuple  					=       self._get_binary_search_tree()

        lowercase_       :							List[str]  					=       [i.label for i in t.inorder_traversal()]
        assert inorder_traversal_nodes == [1, 3, 4, 5, 6, 7, 8, 10, 13, 14]





   def 		_lowerCamelCase			(self					)     ->  None:
        lowercase_       :							Tuple  					=       self._get_binary_search_tree()

        lowercase_       :							List[Any]  					=       [i.label for i in t.preorder_traversal()]
        assert preorder_traversal_nodes == [8, 3, 1, 6, 4, 5, 7, 10, 14, 13]



def _UpperCamelCase (							):
     lowercase_       :							int  					=       BinarySearchTree()
     t.put(8				)
     t.put(3				)
     t.put(6				)
     t.put(1				)
     t.put(10				)
     t.put(14				)
     t.put(13				)
     t.put(4				)
     t.put(7				)
     t.put(5				)

     print(
         '\n            8\n           / \\\n          3   10\n         / \\    \\\n        1   6    14\n           / \\   /\n          4   7 13\n           \\\n            5\n        '				)

     print('Label 6 exists:'	,							t.exists(6				)				)
     print('Label 13 exists:'	,							t.exists(13				)				)
     print('Label -1 exists:'	,							t.exists(-1				)				)
     print('Label 12 exists:'	,							t.exists(12				)				)

     # Prints all the elements of the list in inorder traversal
     lowercase_       :							Optional[int]  					=       [i.label for i in t.inorder_traversal()]
     print('Inorder traversal:'	,							SCREAMING_SNAKE_CASE_				)

     # Prints all the elements of the list in preorder traversal
     lowercase_       :							str  					=       [i.label for i in t.preorder_traversal()]
     print('Preorder traversal:'	,							SCREAMING_SNAKE_CASE_				)

     print('Max. label:'	,							t.get_max_label()				)
     print('Min. label:'	,							t.get_min_label()				)

     # Delete elements
     print('\nDeleting elements 13, 10, 8, 3, 6, 14'				)
     print(
         '\n          4\n         / \\\n        1   7\n             \\\n              5\n        '				)
     t.remove(13				)
     t.remove(10				)
     t.remove(8				)
     t.remove(3				)
     t.remove(6				)
     t.remove(14				)

     # Prints all the elements of the list in inorder traversal after delete
     lowercase_       :							Dict  					=       [i.label for i in t.inorder_traversal()]
     print('Inorder traversal after delete:'	,							SCREAMING_SNAKE_CASE_				)

     # Prints all the elements of the list in preorder traversal after delete
     lowercase_       :							Dict  					=       [i.label for i in t.preorder_traversal()]
     print('Preorder traversal after delete:'	,							SCREAMING_SNAKE_CASE_				)

     print('Max. label:'	,							t.get_max_label()				)
     print('Min. label:'	,							t.get_min_label()				)


if __name__ == "__main__":
 binary_search_tree_example()




