Sunday, January 12, 2025
HomeComputer ScienceBinary Search Tree (BST)

Binary Search Tree (BST)

A Binary Search Tree (BST) is a type of binary tree where each node has the following properties:

  1. Key Value Property:
    • For any node N:
      • All values in the left subtree of N are less than the value of N.
      • All values in the right subtree of N are greater than the value of N.
  2. No Duplicates:
    • Each value in the BST is unique.

Characteristics of BST

  1. Binary Structure: Each node can have at most two children: left and right.
  2. Sorted Order: An in-order traversal of a BST results in values being sorted in ascending order.
  3. Efficient Search: BST allows for fast searching, insertion, and deletion operations, with time complexity O(h)O(h), where hh is the height of the tree.

Basic Operations in BST

1. Search

To search for a value in a BST:

  • Start at the root.
  • Compare the value with the current node:
    • If it matches, the search is successful.
    • If it’s smaller, move to the left child.
    • If it’s larger, move to the right child.
See also  Data Types in C

Algorithm:

java
public boolean search(TreeNode root, int key) {
if (root == null) {
return false; // Value not found
}
if (key == root.val) {
return true; // Value found
}
return key < root.val ? search(root.left, key) : search(root.right, key);
}

2. Insertion

To insert a value into a BST:

  • Start at the root and traverse to the appropriate leaf position based on the key value property.
  • Insert the new value as a leaf node.

Algorithm:

java
public TreeNode insert(TreeNode root, int key) {
if (root == null) {
return new TreeNode(key); // Create new node
}
if (key < root.val) {
root.left = insert(root.left, key);
} else if (key > root.val) {
root.right = insert(root.right, key);
}
return root;
}

3. Deletion

To delete a node:

  • Locate the node to be deleted.
  • Handle three cases:
    1. No Children: Remove the node.
    2. One Child: Replace the node with its child.
    3. Two Children: Replace the node with its in-order successor or predecessor.
See also  What are the methods for formatting dates in JavaScript?

Algorithm:

java
public TreeNode delete(TreeNode root, int key) {
if (root == null) {
return null;
}
if (key < root.val) {
root.left = delete(root.left, key);
} else if (key > root.val) {
root.right = delete(root.right, key);
} else {
// Node found
if (root.left == null) return root.right;
if (root.right == null) return root.left;
// Two children: Find in-order successor
TreeNode successor = findMin(root.right);
root.val = successor.val;
root.right = delete(root.right, successor.val);
}
return root;
}

private TreeNode findMin(TreeNode root) {
while (root.left != null) {
root = root.left;
}
return root;
}

Advantages of BST

  1. Efficient Searches: Allows O(h)O(h) search, where hh is the tree height.
  2. Dynamic: BST can grow dynamically with insertions and deletions.
  3. In-Order Traversal: Provides sorted data effortlessly.

Limitations

  1. Unbalanced Trees: If the tree becomes skewed (e.g., inserting sorted data), operations can degrade to O(n)O(n).
  2. Height Dependency: Efficiency depends on the tree height.

Applications of BST

  1. Search Operations: Fast retrieval of data in dictionaries, phonebooks, and databases.
  2. Sorting: Use in-order traversal to retrieve sorted data.
  3. Range Queries: Efficiently find all values within a range.

Example Tree

For the values: [50, 30, 70, 20, 40, 60, 80]

  • Structure:
markdown
50
/ \
30 70
/ \ / \
20 40 60 80

A Binary Search Tree is a fundamental data structure that provides efficient data organization for many computational problems.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

HTML Form

What are some funny memes?

Recent Comments

0
Would love your thoughts, please comment.x
()
x