



'''simple docstring'''






from __future__ import annotations

import queue



class 							lowercase       :


 """simple docstring"""


 def __init__(							self  ,a_							)		->   str:
        _UpperCAmelCase  :						Optional[Any]			     = data
        _UpperCAmelCase  :						Optional[int]			     = None
        _UpperCAmelCase  :						Union[str, Any]			     = None







def     snake_case_					(     )->		TreeNode:





       '''simple docstring'''


       print("""\n********Press N to stop entering at any point of time********\n"""		)
       _UpperCAmelCase  :						Any			     = input("""Enter the value of the root node: """		).strip().lower()
       _UpperCAmelCase  :						queue.Queue			     = queue.Queue()
       _UpperCAmelCase  :						List[str]			     = TreeNode(int(lowerCAmelCase_		)		)
       q.put(lowerCAmelCase_		)
       while not q.empty():
              _UpperCAmelCase  :						str			     = q.get()
              _UpperCAmelCase  :						Any			     = F'''Enter the left node of {node_found.data}: '''
              _UpperCAmelCase  :						Union[str, Any]			     = input(lowerCAmelCase_		).strip().lower() or """n"""
              if check == "n":
                     return tree_node
              _UpperCAmelCase  :						List[str]			     = TreeNode(int(lowerCAmelCase_		)		)
              _UpperCAmelCase  :						Optional[int]			     = left_node
              q.put(lowerCAmelCase_		)
              _UpperCAmelCase  :						Dict			     = F'''Enter the right node of {node_found.data}: '''
              _UpperCAmelCase  :						Tuple			     = input(lowerCAmelCase_		).strip().lower() or """n"""
              if check == "n":
                     return tree_node
              _UpperCAmelCase  :						Any			     = TreeNode(int(lowerCAmelCase_		)		)
              _UpperCAmelCase  :						Optional[Any]			     = right_node
              q.put(lowerCAmelCase_		)
       raise




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       print(node.data       ,   end=""","""		)
       pre_order(node.left		)
       pre_order(node.right		)




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       in_order(node.left		)
       print(node.data       ,   end=""","""		)
       in_order(node.right		)




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       post_order(node.left		)
       post_order(node.right		)
       print(node.data       ,   end=""","""		)




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       _UpperCAmelCase  :						queue.Queue			     = queue.Queue()
       q.put(lowerCAmelCase_		)
       while not q.empty():
              _UpperCAmelCase  :						Dict			     = q.get()
              print(node_dequeued.data       ,   end=""","""		)
              if node_dequeued.left:
                     q.put(node_dequeued.left		)
              if node_dequeued.right:
                     q.put(node_dequeued.right		)




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       _UpperCAmelCase  :						queue.Queue			     = queue.Queue()
       q.put(lowerCAmelCase_		)
       while not q.empty():
              _UpperCAmelCase  :						Optional[int]			     = []
              while not q.empty():
                     _UpperCAmelCase  :						Optional[int]			     = q.get()
                     print(node_dequeued.data       ,   end=""","""		)
                     if node_dequeued.left:
                            list_.append(node_dequeued.left		)
                     if node_dequeued.right:
                            list_.append(node_dequeued.right		)
              print()
              for node in list_:
                     q.put(lowerCAmelCase_		)




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       _UpperCAmelCase  :						list[TreeNode]			     = []
       _UpperCAmelCase  :						Optional[Any]			     = node
       while n or stack:
              while n:  # start from root node, find its left child
                     print(n.data       ,   end=""","""		)
                     stack.append(lowerCAmelCase_		)
                     _UpperCAmelCase  :						Union[str, Any]			     = n.left
              # end of while means current node doesn't have left child
              _UpperCAmelCase  :						int			     = stack.pop()
              # start to traverse its right child
              _UpperCAmelCase  :						Any			     = n.right




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       _UpperCAmelCase  :						list[TreeNode]			     = []
       _UpperCAmelCase  :						Optional[Any]			     = node
       while n or stack:
              while n:
                     stack.append(lowerCAmelCase_		)
                     _UpperCAmelCase  :						Tuple			     = n.left
              _UpperCAmelCase  :						Union[str, Any]			     = stack.pop()
              print(n.data       ,   end=""","""		)
              _UpperCAmelCase  :						Any			     = n.right




def     snake_case_					(     lowerCAmelCase_		)->		None:





       '''simple docstring'''


       if not isinstance(lowerCAmelCase_       ,   lowerCAmelCase_		) or not node:
              return
       _UpperCAmelCase     ,_UpperCAmelCase  :						str			     = [], []
       _UpperCAmelCase  :						Dict			     = node
       stacka.append(lowerCAmelCase_		)
       while stacka:  # to find the reversed order of post order, store it in stack2
              _UpperCAmelCase  :						Optional[int]			     = stacka.pop()
              if n.left:
                     stacka.append(n.left		)
              if n.right:
                     stacka.append(n.right		)
              stacka.append(lowerCAmelCase_		)
       while stacka:  # pop up from stack2 will be the post order
              print(stacka.pop().data       ,   end=""","""		)




def     snake_case_					(     lowerCAmelCase_ = ""       ,   lowerCAmelCase_=50       ,   lowerCAmelCase_="*"		)->		str:





       '''simple docstring'''


       if not s:
              return "\n" + width * char
       _UpperCAmelCase     ,_UpperCAmelCase  :						Optional[Any]			     = divmod(width - len(lowerCAmelCase_		) - 2       ,   2		)
       return F'''{left * char} {s} {(left + extra) * char}'''


if __name__ == "__main__":
   import doctest

   doctest.testmod()
   print(prompt("""Binary Tree Traversals"""))

   A_							: TreeNode						 =	build_tree()
   print(prompt("""Pre Order Traversal"""))
   pre_order(node)
   print(prompt() + """\n""")

   print(prompt("""In Order Traversal"""))
   in_order(node)
   print(prompt() + """\n""")

   print(prompt("""Post Order Traversal"""))
   post_order(node)
   print(prompt() + """\n""")

   print(prompt("""Level Order Traversal"""))
   level_order(node)
   print(prompt() + """\n""")

   print(prompt("""Actual Level Order Traversal"""))
   level_order_actual(node)
   print("""*""" * 5_0 + """\n""")

   print(prompt("""Pre Order Traversal - Iteration Version"""))
   pre_order_iter(node)
   print(prompt() + """\n""")

   print(prompt("""In Order Traversal - Iteration Version"""))
   in_order_iter(node)
   print(prompt() + """\n""")

   print(prompt("""Post Order Traversal - Iteration Version"""))
   post_order_iter(node)
   print(prompt())








'''simple docstring'''






class 							lowercase       :


 """simple docstring"""


 def __init__(							self							)		->   List[str]:
        _UpperCAmelCase  :						int			     = 0
        _UpperCAmelCase  :						Union[str, Any]			     = 0
        _UpperCAmelCase  :						Optional[int]			     = {}







 def _snake_case (							self  ,a_							)		->   Optional[Any]:
        if vertex not in self.adjacency:
               _UpperCAmelCase  :						int			     = {}
               self.num_vertices += 1







 def _snake_case (							self  ,a_  ,a_  ,a_							)		->   int:
        self.add_vertex(a_							)
        self.add_vertex(a_							)

        if head == tail:
               return

        _UpperCAmelCase  :						List[Any]			     = weight
        _UpperCAmelCase  :						Dict			     = weight







 def _snake_case (							self							)		->   Dict:
        _UpperCAmelCase  :						Optional[int]			     = self.get_edges()
        for edge in edges:
               _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						Dict			     = edge
               edges.remove((tail, head, weight)							)
        for i in range(len(a_							)							):
               _UpperCAmelCase  :						str			     = list(edges[i]							)

        edges.sort(key=lambda a_							: e[2]							)
        for i in range(len(a_							) - 1							):
               if edges[i][2] >= edges[i + 1][2]:
                      _UpperCAmelCase  :						Optional[Any]			     = edges[i][2] + 1
        for edge in edges:
               _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						Union[str, Any]			     = edge
               _UpperCAmelCase  :						str			     = weight
               _UpperCAmelCase  :						List[str]			     = weight







 def __str__(							self							)		->   Any:
        _UpperCAmelCase  :						List[Any]			     = """"""
        for tail in self.adjacency:
               for head in self.adjacency[tail]:
                      _UpperCAmelCase  :						List[str]			     = self.adjacency[head][tail]
                      string += f'''{head} -> {tail} == {weight}\n'''
        return string.rstrip("""\n"""							)







 def _snake_case (							self							)		->   str:
        _UpperCAmelCase  :						int			     = []
        for tail in self.adjacency:
               for head in self.adjacency[tail]:
                      output.append((tail, head, self.adjacency[head][tail])							)
        return output







 def _snake_case (							self							)		->   Optional[int]:
        return self.adjacency.keys()







 @staticmethod
 def _snake_case (							a_=None  ,a_=None							)		->   Tuple:
        _UpperCAmelCase  :						List[Any]			     = Graph()
        if vertices is None:
               _UpperCAmelCase  :						List[str]			     = []
        if edges is None:
               _UpperCAmelCase  :						Optional[Any]			     = []
        for vertex in vertices:
               g.add_vertex(a_							)
        for edge in edges:
               g.add_edge(*a_							)
        return g



 class 							lowercase       :


  """simple docstring"""


  def __init__(							self							)		->   int:
         _UpperCAmelCase  :						List[str]			     = {}
         _UpperCAmelCase  :						int			     = {}







  def __len__(							self							)		->   Tuple:
         return len(self.parent							)







  def _snake_case (							self  ,a_							)		->   str:
         if item in self.parent:
                return self.find(a_							)

         _UpperCAmelCase  :						Optional[Any]			     = item
         _UpperCAmelCase  :						List[Any]			     = 0
         return item







  def _snake_case (							self  ,a_							)		->   List[str]:
         if item not in self.parent:
                return self.make_set(a_							)
         if item != self.parent[item]:
                _UpperCAmelCase  :						List[Any]			     = self.find(self.parent[item]							)
         return self.parent[item]







  def _snake_case (							self  ,a_  ,a_							)		->   Union[str, Any]:
         _UpperCAmelCase  :						Any			     = self.find(a_							)
         _UpperCAmelCase  :						List[str]			     = self.find(a_							)

         if roota == roota:
                return roota

         if self.rank[roota] > self.rank[roota]:
                _UpperCAmelCase  :						Any			     = roota
                return roota

         if self.rank[roota] < self.rank[roota]:
                _UpperCAmelCase  :						Any			     = roota
                return roota

         if self.rank[roota] == self.rank[roota]:
                self.rank[roota] += 1
                _UpperCAmelCase  :						List[str]			     = roota
                return roota
         return None







 @staticmethod
 def _snake_case (							a_							)		->   List[Any]:
        _UpperCAmelCase  :						int			     = graph.num_vertices

        _UpperCAmelCase  :						int			     = Graph.UnionFind()
        _UpperCAmelCase  :						Optional[int]			     = []
        while num_components > 1:
               _UpperCAmelCase  :						int			     = {}
               for vertex in graph.get_vertices():
                      _UpperCAmelCase  :						Union[str, Any]			     = -1

               _UpperCAmelCase  :						Tuple			     = graph.get_edges()
               for edge in edges:
                      _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						str			     = edge
                      edges.remove((tail, head, weight)							)
               for edge in edges:
                      _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						Optional[Any]			     = edge
                      _UpperCAmelCase  :						Any			     = union_find.find(a_							)
                      _UpperCAmelCase  :						Any			     = union_find.find(a_							)
                      if seta != seta:
                             if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                                    _UpperCAmelCase  :						Tuple			     = [head, tail, weight]

                             if cheap_edge[seta] == -1 or cheap_edge[seta][2] > weight:
                                    _UpperCAmelCase  :						List[str]			     = [head, tail, weight]
               for vertex in cheap_edge:
                      if cheap_edge[vertex] != -1:
                             _UpperCAmelCase     ,_UpperCAmelCase     ,_UpperCAmelCase  :						str			     = cheap_edge[vertex]
                             if union_find.find(a_							) != union_find.find(a_							):
                                    union_find.union(a_  ,a_							)
                                    mst_edges.append(cheap_edge[vertex]							)
                                    _UpperCAmelCase  :						Tuple			     = num_components - 1
        _UpperCAmelCase  :						Optional[int]			     = Graph.build(edges=a_							)
        return mst



