

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Trees
incomplete
2: Binary Trees
incomplete
3: Insert Nodes
incomplete
4: Insert Review
incomplete
5: Min and Max
incomplete
6: Delete
incomplete
7: Deletion Review
incomplete
8: Preorder Traversal
incomplete
9: Postorder Traversal
incomplete
10: Inorder Traversal
incomplete
11: Node Exists
incomplete
12: Height
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Inserting into a binary search tree (like most of its operations) is very fast. Picture the algorithm that you just wrote in your head: how many comparisons does it take to find the right spot for a new node?
It only requires one comparison for each level of the tree, making it O(log(n))! (At least in a balanced tree, we'll talk about this later).
Order log(n) is very fast - it's practically as good as O(1) in most cases. If our tree has 1,000,000 nodes, we only need to make 20 comparisons to find the right spot for a new node. If our tree is 2x larger (2,000,000 nodes), we only need to make one more comparison per insert, 21 total.