binary_search_tree
==================

.. py:module:: binary_search_tree


Classes
-------

.. autoapisummary::

   binary_search_tree.BinarySearchTree


Module Contents
---------------

.. py:class:: BinarySearchTree

   A class representing a Binary Search Tree (BST).

   .. method:: insert(key)

      Inserts a key into the BST while maintaining the BST properties.

   .. method:: search(key, algorithm='dfs')

      Searches for a key in the BST using the specified search algorithm ('dfs' or 'bfs').

   .. method:: delete(key)

      Deletes a key from the BST while preserving the BST structure.

   .. method:: list_to_tree(elements)

      Constructs a BST from a list of integers.



   .. py:attribute:: root
      :value: None



   .. py:method:: insert(key)

      Inserts a key into the Binary Search Tree (BST) while maintaining its properties.

      - Values smaller than the current node's key go to the left subtree.
      - Values larger than the current node's key go to the right subtree.
      - Duplicate values are not allowed.

      :param key: The value to insert into the BST.
      :type key: int

      :raises TypeError: If the key is not an integer or is None.

      .. rubric:: Examples

      >>> bst = BinarySearchTree()
      >>> bst.insert(10)
      >>> bst.insert(5)
      >>> bst.insert(15)
      >>> bst.root.left.key
      5
      >>> bst.root.right.key
      15



   .. py:method:: search(key)

      Searches for a key in the Binary Search Tree (BST).

      :param key: The value to search for in the BST.
      :type key: int

      :returns:

                - The Node object containing the specified key if found.
                - None if the key does not exist or the tree is empty.
      :rtype: Node or None

      :raises TypeError: If the key is not an integer or is None.

      .. rubric:: Examples

      >>> bst = BinarySearchTree()
      >>> bst.insert(10)
      >>> bst.insert(5)
      >>> bst.insert(15)
      >>> bst.search(5).key
      5
      >>> bst.search(20) is None
      True



   .. py:method:: delete(key)

      Deletes a key from the Binary Search Tree (BST) while preserving its structure.

      - If the node has no children, it is simply removed.
      - If the node has one child, it is replaced by its child.
      - If the node has two children, it is replaced by the in-order successor (smallest node in the right subtree).

      :param key: The value to delete from the BST.
      :type key: int

      :returns:

                - True if the key was found and deleted.
                - False if the key was not found.
      :rtype: bool

      :raises TypeError: If the key is not an integer or is None.

      .. rubric:: Examples

      >>> bst = BinarySearchTree()
      >>> bst.insert(10)
      >>> bst.insert(5)
      >>> bst.insert(15)
      >>> bst.delete(10)
      True
      >>> bst.delete(20)
      False



   .. py:method:: list_to_tree(elements)
      :staticmethod:


      Constructs a Binary Search Tree (BST) from a list of integers.

      :param elements: A list of integers to be inserted into the BST.
      :type elements: list of int

      :returns: A BinarySearchTree object containing all elements from the input list.
      :rtype: BinarySearchTree

      :raises ValueError: If the input is not a list or contains non-integer elements.

      .. rubric:: Examples

      >>> elements = [10, 5, 15, 12, 20]
      >>> bst = BinarySearchTree.list_to_tree(elements)
      >>> bst.root.key
      10
      >>> bst.root.left.key
      5
      >>> bst.root.right.key
      15



