Пример #1
0
    def get_max(current: BinaryTreeNode):
        """
        This method returns the maximum value in a BST.

        :param current: The current node.
        :return: The maximum value in the BST
        """
        while current.has_right():
            current = current.get_right()
        return current.get_data()
Пример #2
0
    def post_order(current: BinaryTreeNode, result: list):
        """
        This method implements the Depth First - Post-order Traversal of a Binary Tree.

        :param current: The current node in the tree.
        :param result: The list to collect the result of the traversal.
        """
        if current is None:
            return
        BinaryTreeTraversal.post_order(current.get_left(), result)
        BinaryTreeTraversal.post_order(current.get_right(), result)
        result.append(current.get_data())
Пример #3
0
    def search(self, current: BinaryTreeNode, search_target):
        """
        This method tells whether a given data point is there in the BST.

        :param current: The current node.
        :param search_target: The data to be searched.
        :return: The boolean flag whether the data is present.
        """
        if current is None:
            return False
        elif current.get_data() == search_target:
            return True
        elif search_target <= current.get_data():
            return self.search(current.get_left(), search_target)
        else:
            return self.search(current.get_right(), search_target)
Пример #4
0
    def is_bst(current: BinaryTreeNode,
               min_val=float("-inf"),
               max_val=float("inf")):
        """
        This method checks whether a given Binary Tree is a BST.

        :param current: The current node.
        :param min_val: The minimum value which should be less than or equal to the current node data to be a BST.
        :param max_val: The maximum value which should be greater than or equal to the current node data to be a BST.
        :return: The boolean flag indicating whether the given Binary Tree is a BST.
        """
        if current is None:
            return True

        if (min_val <= current.get_data() <= max_val
                and BinarySearchTree.is_bst(current.get_left(), min_val,
                                            current.get_data())
                and BinarySearchTree.is_bst(current.get_right(),
                                            current.get_data(), max_val)):
            return True

        return False
Пример #5
0
    def level_order(current: BinaryTreeNode, result: list):
        """
        This method implements the Breadth First - Level Order Traversal of a Binary Tree.

        :param current: The current node in the tree.
        :param result: The list to collect the result of the traversal.
        """
        if current is None:
            return

        queue = Queues()
        queue.enqueue(current)

        while not queue.is_empty():
            current = queue.de_queue()

            result.append(current.get_data())

            if current.has_left():
                queue.enqueue(current.get_left())

            if current.has_right():
                queue.enqueue(current.get_right())
Пример #6
0
    def insert(self,
               current: BinaryTreeNode,
               data,
               parent=None,
               add_parent=False):
        """
        This method inserts a new data node in the BST using recursion.

        :param current: The current node.
        :param data: The data to be inserted.
        :param parent: The parent for the new node.
        :param add_parent: The flag to add parent.
        :return: The tree with the new data node.
        """
        if current is None:
            return BinaryTreeNode(data, parent=parent)
        elif data <= current.get_data():
            current.set_left(
                self.insert(current.get_left(), data, current, add_parent))
        elif current.get_data() < data:
            current.set_right(
                self.insert(current.get_right(), data, current, add_parent))
        return current
Пример #7
0
    def remove(self, current: BinaryTreeNode, to_remove):
        """
        This method removes the data node from the BST if the data is present.

        todo - how to update parents?

        :param current: The current node.
        :param to_remove: The data to be removed from BST.
        :return: The updated current node.
        """
        if current is None:
            return current
        elif to_remove < current.get_data():
            current.set_left(self.remove(current.get_left(), to_remove))
        elif current.get_data() < to_remove:
            current.set_right(self.remove(current.get_right(), to_remove))
        else:
            if not current.has_left() and not current.has_right():
                current = None
            elif not current.has_left():
                current = current.get_right()
            elif not current.has_right():
                current = current.get_left()
            else:
                current.set_data(self.get_min(current.get_right()))
                current.set_right(
                    self.remove(current.get_right(), current.get_data()))
        return current