





from collections.abc import Iterable
from typing import Any

class     snake_case_		:






 '''simple docstring'''





 def __init__(  self,		A_ = None		)	->       Union[str, Any]:
    UpperCAmelCase__						=value
    UpperCAmelCase__						=None  # Added in order to delete a node easier
    UpperCAmelCase__						=None
    UpperCAmelCase__						=None






 def __repr__(  self		)	->       Any:
    from pprint import pformat

    if self.left is None and self.right is None:
       return str(self.value		)
    return pformat({f"""{self.value}""": (self.left, self.right)},		indent=1		)

class     snake_case_		:






 '''simple docstring'''





 def __init__(  self,		A_ = None		)	->       Optional[Any]:
    UpperCAmelCase__						=root

 def __str__(  self		)	->       Optional[int]:
    return str(self.root		)

 def  __UpperCAmelCase						(  self,		A_,		A_		)	->       List[str]:
    if new_children is not None:  # reset its kids
       UpperCAmelCase__						=node.parent
    if node.parent is not None:  # reset its parent
       if self.is_right(_UpperCamelCase		):  # If it is the right children
          UpperCAmelCase__						=new_children
       else:
          UpperCAmelCase__						=new_children
    else:
       UpperCAmelCase__						=new_children

 def  __UpperCAmelCase						(  self,		A_		)	->       Union[str, Any]:
    if node.parent and node.parent.right:
       return node == node.parent.right
    return False

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    return self.root is None

 def  __UpperCAmelCase						(  self,		A_		)	->       Union[str, Any]:
    UpperCAmelCase__						=Node(_UpperCamelCase		)  # create a new Node
    if self.empty():  # if Tree is empty
       UpperCAmelCase__						=new_node  # set its root
    else:  # Tree is not empty
       UpperCAmelCase__						=self.root  # from root
       if parent_node is None:
          return
       while True:  # While we don't get to a leaf
          if value < parent_node.value:  # We go left
             if parent_node.left is None:
                UpperCAmelCase__						=new_node  # We insert the new node in a leaf
                break
             else:
                UpperCAmelCase__						=parent_node.left
          else:
             if parent_node.right is None:
                UpperCAmelCase__						=new_node
                break
             else:
                UpperCAmelCase__						=parent_node.right
       UpperCAmelCase__						=parent_node

 def  __UpperCAmelCase						(  self,		*A_		)	->       List[str]:
    for value in values:
       self.__insert(_UpperCamelCase		)

 def  __UpperCAmelCase						(  self,		A_		)	->       int:
    if self.empty():
       raise IndexError("Warning: Tree is empty! please use another."		)
    else:
       UpperCAmelCase__						=self.root
       # use lazy evaluation here to avoid NoneType Attribute error
       while node is not None and node.value is not value:
          UpperCAmelCase__						=node.left if value < node.value else node.right
       return node

 def  __UpperCAmelCase						(  self,		A_ = None		)	->       Optional[Any]:
    if node is None:
       if self.root is None:
          return None
       UpperCAmelCase__						=self.root

    if not self.empty():
       while node.right is not None:
          UpperCAmelCase__						=node.right
    return node

 def  __UpperCAmelCase						(  self,		A_ = None		)	->       List[Any]:
    if node is None:
       UpperCAmelCase__						=self.root
    if self.root is None:
       return None
    if not self.empty():
       UpperCAmelCase__						=self.root
       while node.left is not None:
          UpperCAmelCase__						=node.left
    return node

 def  __UpperCAmelCase						(  self,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=self.search(_UpperCamelCase		)  # Look for the node with that label
    if node is not None:
       if node.left is None and node.right is None:  # If it has no children
          self.__reassign_nodes(_UpperCamelCase,		_UpperCamelCase		)
       elif node.left is None:  # Has only right children
          self.__reassign_nodes(_UpperCamelCase,		node.right		)
       elif node.right is None:  # Has only left children
          self.__reassign_nodes(_UpperCamelCase,		node.left		)
       else:
          UpperCAmelCase__						=self.get_max(
              node.left		)  # Gets the max value of the left branch
          self.remove(tmp_node.value		)  # type: ignore
          UpperCAmelCase__						=(
              tmp_node.value  # type: ignore
          )  # Assigns the value to the node to delete and keep tree structure

 def  __UpperCAmelCase						(  self,		A_		)	->       Union[str, Any]:
    if node is not None:
       yield node  # Preorder Traversal
       yield from self.preorder_traverse(node.left		)
       yield from self.preorder_traverse(node.right		)

 def  __UpperCAmelCase						(  self,		A_=None		)	->       List[str]:
    if traversal_function is None:
       return self.preorder_traverse(self.root		)
    else:
       return traversal_function(self.root		)

 def  __UpperCAmelCase						(  self,		A_,		A_		)	->       str:
    if node:
       self.inorder(_UpperCamelCase,		node.left		)
       arr.append(node.value		)
       self.inorder(_UpperCamelCase,		node.right		)






 def  __UpperCAmelCase						(  self,		A_,		A_		)	->       Tuple:
    UpperCAmelCase__						=[]
    self.inorder(_UpperCamelCase,		_UpperCamelCase		)  # append all values to list using inorder traversal
    return arr[k - 1]






def 				_UpperCAmelCase			(    A     ):




   '''simple docstring'''

   UpperCAmelCase__						=[]
   if curr_node is not None:
      UpperCAmelCase__						=postorder(curr_node.left     ) + postorder(curr_node.right     ) + [curr_node]
   return node_list






def 				_UpperCAmelCase			(    ):




   '''simple docstring'''

   UpperCAmelCase__						=(8, 3, 6, 1, 10, 14, 13, 4, 7)
   UpperCAmelCase__						=BinarySearchTree()
   for i in testlist:
      t.insert(SCREAMING_SNAKE_CASE_     )

   # Prints all the elements of the list in order traversal
   print(SCREAMING_SNAKE_CASE_     )

   if t.search(6     ) is not None:
      print("The value 6 exists"     )
   else:
      print("The value 6 doesn\'t exist"     )

   if t.search(-1     ) is not None:
      print("The value -1 exists"     )
   else:
      print("The value -1 doesn\'t exist"     )

   if not t.empty():
      print("Max Value: "				,							t.get_max().value     )  # type: ignore
      print("Min Value: "				,							t.get_min().value     )  # type: ignore

   for i in testlist:
      t.remove(SCREAMING_SNAKE_CASE_     )
      print(SCREAMING_SNAKE_CASE_     )


if __name__ == "__main__":
    import doctest

    doctest.testmod(verbose=True)










from __future__ import annotations
def      A__       (    SCREAMING_SNAKE_CASE_   :      int						)  ->  bool:






   """simple docstring"""







   _UpperCAmelCase           =   str(SCREAMING_SNAKE_CASE_						)
   return len(SCREAMING_SNAKE_CASE_						) == 9 and set(SCREAMING_SNAKE_CASE_						) == set('''123456789'''						)
def      A__       (    )  ->  int | None:






   """simple docstring"""







   for base_num in range(99_99				,	49_99				,	-1						):
      _UpperCAmelCase           =   10_00_02 * base_num
      if is_9_pandigital(SCREAMING_SNAKE_CASE_						):
         return candidate

   for base_num in range(3_33				,	99				,	-1						):
      _UpperCAmelCase           =   1_00_20_03 * base_num
      if is_9_pandigital(SCREAMING_SNAKE_CASE_						):
         return candidate

   return None


if __name__ == "__main__":
   print(f'''{solution() = }''')