Search and insert in binary search trees
Quick answer
A binary search tree (BST) places smaller keys in the left subtree and larger keys in the right subtree under a chosen duplicate policy. Search follows only the relevant branch.
Working examples
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
const root={key:4,left:{key:2,left:null,right:null},right:{key:7,left:null,right:null}};function has(n,key){while(n){if(key===n.key)return true;n=key<n.key?n.left:n.right;}return false;}console.log(has(root,7),has(root,3));Expected console output:
true falseSearch follows one branch at each comparison.
Run as an independent script in Node.js v22.16.0. ByteMind editor support has not been verified.
function insert(n,key){if(!n)return {key,left:null,right:null}; if(key<n.key)n.left=insert(n.left,key);else if(key>n.key)n.right=insert(n.right,key);return n;}let root=null;for(const k of [2,1,3,2])root=insert(root,k);const out=[];function walk(n){if(n){walk(n.left);out.push(n.key);walk(n.right);}}walk(root);console.log(JSON.stringify(out));Expected console output:
[1,2,3]Equal keys are ignored and inorder preserves BST order.
How it works and cost model
With unique finite numeric keys, compare the target to the current key. If equal, return true; if smaller follow left, else right. Null means absent. Insertion follows the same route and fills an empty child position; ignore duplicate keys in this example. Work O(h) per search or insertion for height h; a balanced tree has logarithmic height, but ascending insertion into an ordinary unbalanced BST can create a chain of height n. Balancing and deletion are separate algorithms. Iterative search needs O(1) auxiliary references.
Common mistakes
Checking only a node against its immediate children does not prove the full BST invariant. Every key in a subtree must obey ancestor bounds.
Practice
Insert 4,2,6,1,3 and duplicate 4 with the stated ignore policy. Search for 3 and 7, and compare height with insertion of 1,2,3,4,5.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Search and insert in binary search trees — Knowledge Check
Reason about the algorithm or data structure, its invariants, trade-offs and deterministic outputs.
What is the ordinary unbalanced BST search bound in terms of height h?
Which insertion sequence can create a chain in a plain BST?
Why must BST validation track ancestor bounds?
What is the exact console output in the Binary search tree example 1?
const root={key:4,left:{key:2,left:null,right:null},right:{key:7,left:null,right:null}};function has(n,key){while(n){if(key===n.key)return true;n=key<n.key?n.left:n.right;}return false;}console.log(has(root,7),has(root,3));What is the exact console output in the Binary search tree example 2?
function insert(n,key){if(!n)return {key,left:null,right:null}; if(key<n.key)n.left=insert(n.left,key);else if(key>n.key)n.right=insert(n.right,key);return n;}let root=null;for(const k of [2,1,3,2])root=insert(root,k);const out=[];function walk(n){if(n){walk(n.left);out.push(n.key);walk(n.right);}}walk(root);console.log(JSON.stringify(out));