DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Tools
  3. Algorithm Visualizer
  4. BST Insert & Search

BST Insert & Search

Treesmedium#14

Insert values maintaining BST property — left smaller, right larger

Ready

Step 1 of 0

Speed

Input Data

Comma-separated numbers (0-999). Min 2 values.

Algorithm Code

1function bstInsert(root, value) {
2 if (!root) return { value, left: null, right: null };
3 if (value < root.value) {
4 root.left = bstInsert(root.left, value);
5 } else {
6 root.right = bstInsert(root.right, value);
7 }
8 return root;
9}
Best

O(log n)

Average

O(log n)

Worst

O(n)

Space

O(n)

About BST Insert & Search

A Binary Search Tree maintains the property that for every node, all values in the left subtree are smaller and all values in the right subtree are larger. Insert and search operations follow this property to navigate the tree efficiently. Worst case (skewed tree) degrades to O(n).

Time Complexity: Best: O(log n), Average: O(log n), Worst: O(n). Space: O(n).

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.