Bst Tree

10

About algorithm

Explanation

A Binary Search Tree (BST) is a node-based binary tree data structure where each node has a comparable key (and associated value) and satisfies the property that the key in any node is greater than all keys in the node's left subtree and less than those in its right subtree. BSTs allow efficient searching, insertion, and deletion operations.

Steps

  1. Start at the root node.
  2. Compare the value to be inserted with the current node’s value.
  3. If the value is less, move to the left child; if greater, move to the right child.
  4. Repeat this process until reaching a null position.
  5. Insert the new node at the null position.

Pseudocode

insertBST(node, value):
  if node is null:
    return new Node(value)
  if value < node.value:
    node.left = insertBST(node.left, value)
  else:
    node.right = insertBST(node.right, value)
  return node

Complexity

Time: O(h) - average case (h = tree height)

Space: O(n) - worst case (unbalanced tree)