A Binary Search Tree (BST) is a type of binary tree that satisfies the following properties:
These properties make BSTs an efficient data structure for searching, insertion, and deletion operations.
Diagram:
6
/ \
4 8
/ \ / \
3 5 7 9
Code in Java:
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
left = null;
right = null;
}
}
class BinarySearchTree {
Node root;
public BinarySearchTree() {
root = null;
}
public void insert(int value) {
root = insertNode(root, value);
}
private Node insertNode(Node root, int value) {
if (root == null) {
root = new Node(value);
return root;
}
if (value < root.value) {
root.left = insertNode(root.left, value);
} else if (value > root.value) {
root.right = insertNode(root.right, value);
}
return root;
}
public boolean search(int value) {
return searchNode(root, value);
}
private boolean searchNode(Node root, int value) {
if (root == null) {
return false;
}
if (value == root.value) {
return true;
} else if (value < root.value) {
return searchNode(root.left, value);
} else {
return searchNode(root.right, value);
}
}
public void inorderTraversal() {
inorder(root);
}
private void inorder(Node root) {
if (root != null) {
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
}
}
public class Main {
public static void main(String[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.insert(6);
bst.insert(4);
bst.insert(8);
bst.insert(3);
bst.insert(5);
bst.insert(7);
bst.insert(9);
System.out.println("Inorder Traversal:");
bst.inorderTraversal();
int searchValue = 5;
if (bst.search(searchValue)) {
System.out.println("\n" + searchValue + " is found in the BST.");
} else {
System.out.println("\n" + searchValue + " is not found in the BST.");
}
}
}
Explanation:
Node
and BinarySearchTree
. Node
class represents a node in the BST with its value, left child, and right child. BinarySearchTree
class represents the BST and provides methods for the insertion, search, and inorder traversal.insert
method inserts a new node into the BST based on its value.It recursively traverses the tree to find the appropriate position for insertion.search
method searches for a given value in the BST. It recursively traverses the tree, comparing the value with each node’s value until a match is found or the end of the tree is reached.inorderTraversal
method performs an inorder traversal of the BST, which visits the nodes in ascending order. It recursively traverses the left subtree, visits the current node, and then recursively traverses the right subtree.main
method, a new BST is created, and nodes with values 6, 4, 8, 3, 5, 7, and 9 are inserted. The inorder traversal is performed, and the search method is used to search for the value 5.Output:
Inorder Traversal:
3 4 5 6 7 8 9
5 is found in the BST
Operation | Time Complexity | Space Complexity |
---|---|---|
Insertion | O(log n) | O(log n) |
Search | O(log n) | O(log n) |
Deletion | O(log n) | O(log n) |
Inorder Traversal | O(n) | O(log n) |
Preorder Traversal | O(n) | O(log n) |
Postorder Traversal | O(n) | O(log n) |
Height/Depth | O(n) | O(log n) |
height = log2(n + 1) - 1
n
represents the number of nodes in the BST.n = 2^(height+1) - 1
height
refers to the height of the BST.leafNodes = (n + 1) / 2
n
represents the number of nodes in the BST.internalNodes = n - leafNodes
n
refers to the number of nodes in the BST.leafNodes
represents the number of leaf nodes in the BST.n
is the number of nodes in the tree.Note: also read about Types of Views & Binary Trees
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…