Theory
A Binary Search Tree is a binary tree where values smaller than a node are placed in the left subtree and values greater than the node are placed in the right subtree. This ordering can make searching efficient when the tree is balanced.
Example
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
console.log(root.left.value);
console.log(root.right.value);
Expected Output
10 and 30
Practice
Build a BST using the values 50, 30, 70, 20 and 40.
Interview Questions
- What is a binary search tree?
- What property does a BST follow?
- Why can BST searching be efficient?
- What happens when a BST becomes unbalanced?