Class BinarySearchTree

java.lang.Object
com.amritpandey23.dsalibrary.tree.BinarySearchTree

public class BinarySearchTree extends Object
Binary Search Tree Algorithms: Search, Insert, Delete, Validate. All the algorithms are implemented on TreeNode node.
  • Constructor Details

    • BinarySearchTree

      public BinarySearchTree()
  • Method Details

    • search

      public static boolean search(TreeNode<Integer> node, Integer val)
      Searches for a specified value in the binary search tree.
      Parameters:
      node - the root of the binary search tree
      val - the value to search for
      Returns:
      true if the value is found in the tree, false otherwise
    • insert

      public static TreeNode<Integer> insert(TreeNode<Integer> node, Integer val)
      Inserts a specified value into the binary search tree. If the value already exists in the tree, it will not be duplicated.
      Parameters:
      node - the root of the binary search tree
      val - the value to insert
      Returns:
      the root of the tree after insertion
    • delete

      public static TreeNode<Integer> delete(TreeNode<Integer> node, Integer val)
      Deletes a specified value from the binary search tree.
      Parameters:
      node - the root of the binary search tree
      val - the value to delete
      Returns:
      the root of the tree after deletion
    • validate

      public static boolean validate(TreeNode<Integer> node)
      Validates if the given binary tree is a binary search tree (BST).
      Parameters:
      node - the root of the binary tree to validate
      Returns:
      true if the tree is a valid binary search tree, false otherwise