Binary Search Tree(BST)

What is BST Tree?

A Binary Search Tree (BST) is a data structure used to storing data in a sorted manner. Each node in a Binary Search Tree has at most two children, a left child and a right child, with the left child containing values less than the parent node and the right child containing values greater than the parent node.

Tree Traversal

In order Traversal

In order traversal visits the node in the order: Left -> Root -> Right

Pre Order Traversal

Preorder traversal visits the node in the order: Root -> Left -> Right

Post Order Traversal

Post order traversal visits the node in the order: Left -> Right -> Root

What is Perfectly Balanced Binary Tree?

A balanced binary tree will follow the following conditions:

  1. The absolute difference of heights of left and right subtrees at any node is less than 1.

  2. For each node, its left subtree is a balanced binary tree.

  3. For each node, its right subtree is a balanced binary tree.

When BST Best and Worst Complexity?

Best Case Scenario

The best-case scenario for a BST occurs when the tree is perfectly balanced. In a balanced BST, each level of the tree is fully populated, leading to optimal performance.

  • Search, Insertion, Deletion:

    • Time Complexity: O(log⁡n)

    • Space Complexity: O(n)

    • Reason: In a balanced BST, the height of the tree is log(n). Operations like search, insertion, and deletion involve traversing from the root to a leaf, which takes log⁡n steps in the best case.

Worst Case Scenario

The worst-case scenario for a BST occurs when the tree is completely unbalanced, resembling a linked list. This can happen if elements are inserted in sorted order.

  • Search, Insertion, Deletion:

    • Time Complexity: O(n)

    • Space Complexity: O(n)

    • Reason: In an unbalanced BST, the height of the tree can be N. Operations like search, insertion, and deletion involve traversing from the root to a leaf, which takes N steps in the worst case.

You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

Given the root of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.

  • The right subtree of a node contains only nodes with keys greater than the node's key.

  • Both the left and right subtrees must also be binary search trees.

Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

897. Increasing Order Search Tree :

Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Last updated