0
0
DSA Javascriptprogramming~10 mins

Why BST Over Plain Binary Tree in DSA Javascript - Why It Works

Choose your learning style9 modes available
Concept Flow - Why BST Over Plain Binary Tree
Start with Plain Binary Tree
Insert nodes anywhere
No order: left/right child any value
Search: must check all nodes
Time: O(n) worst case
Switch to BST
Insert nodes with order: left < parent < right
Search: go left or right based on value
Time: O(log n) average
More efficient search, insert, delete
Shows how a plain binary tree inserts nodes without order causing slow search, while BST keeps order for faster search.
Execution Sample
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}
Defines a node structure used in both plain binary tree and BST.
Execution Table
StepOperationNodes in TreePointer ChangesVisual State
1Insert 10 in Plain Binary Tree10root -> Node(10)10
2Insert 5 in Plain Binary Tree (no order)10, 5root.left -> Node(5) 10 / 5
3Insert 15 in Plain Binary Tree (no order)10, 5, 15root.right -> Node(15) 10 / \ 5 15
4Search 15 in Plain Binary Tree10, 5, 15Traverse nodes 10 -> 5 -> 15Must check all nodes until found
5Insert 10 in BST10root -> Node(10)10
6Insert 5 in BST (5 < 10)10, 5root.left -> Node(5) 10 / 5
7Insert 15 in BST (15 > 10)10, 5, 15root.right -> Node(15) 10 / \ 5 15
8Search 15 in BST10, 5, 15Traverse nodes 10 -> 15 (skip 5)Search faster by choosing right branch
9Search 7 in BST (not found)10, 5, 15Traverse nodes 10 -> 5 (stop, no right child)Stops early, no full traversal
10End10, 5, 15No changesSearch and insert efficient in BST
💡 Execution stops after demonstrating search and insert differences between plain binary tree and BST.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 6After Step 7After Step 8Final
root (Plain Binary Tree)nullNode(10)Node(10) with left=Node(5)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)
root (BST)nullnullnullnullNode(10)Node(10) with left=Node(5)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)Node(10) with left=Node(5), right=Node(15)
Key Moments - 3 Insights
Why does searching in a plain binary tree take longer?
Because nodes are inserted without order, search must check every node (see execution_table steps 4 and 9).
How does BST improve search speed?
BST keeps nodes ordered so search goes left or right based on value, skipping many nodes (see execution_table step 8).
Why does BST stop searching early when value not found?
Because it follows order rules, if the path ends without finding the value, it stops early (see execution_table step 9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does BST insert the node with value 5?
AStep 2
BStep 3
CStep 6
DStep 7
💡 Hint
Check the 'Operation' column for BST insertions after step 4.
At which step does searching in the plain binary tree check all nodes?
AStep 8
BStep 4
CStep 9
DStep 6
💡 Hint
Look for 'Search' operation in plain binary tree in the execution_table.
If we insert nodes in BST without order, how would the search time change?
AIt would become O(n)
BIt would stay O(log n)
CIt would become O(1)
DIt would be unpredictable
💡 Hint
Refer to key_moments about search efficiency and execution_table steps 4 and 8.
Concept Snapshot
Plain Binary Tree inserts nodes anywhere without order.
Search must check all nodes, time O(n).
BST inserts nodes with order: left < parent < right.
Search goes left or right, time O(log n) average.
BST is faster for search, insert, and delete operations.
Full Transcript
This visual shows why a Binary Search Tree (BST) is better than a plain binary tree. In a plain binary tree, nodes are added anywhere without order, so searching means checking every node, which takes longer. In a BST, nodes are added in order: smaller values go left, bigger go right. This order lets us skip many nodes when searching, making it faster. The execution table shows inserting and searching nodes in both trees, highlighting how BST improves efficiency by reducing the number of nodes checked.