Why BST Over Plain Binary Tree in DSA Go - Performance Analysis
We want to understand why a Binary Search Tree (BST) is faster than a plain Binary Tree for searching data.
How does the structure affect the time it takes to find a value?
Analyze the time complexity of searching a value in these trees.
// Search in plain binary tree (no order)
func SearchPlain(root *Node, val int) bool {
if root == nil {
return false
}
if root.Value == val {
return true
}
return SearchPlain(root.Left, val) || SearchPlain(root.Right, val)
}
// Search in BST (ordered)
func SearchBST(root *Node, val int) bool {
if root == nil {
return false
}
if val == root.Value {
return true
} else if val < root.Value {
return SearchBST(root.Left, val)
} else {
return SearchBST(root.Right, val)
}
}
This code shows searching for a value in a plain binary tree and in a BST.
Look at what repeats during the search.
- Primary operation: Visiting nodes to check their values.
- How many times:
- Plain tree: May visit almost all nodes (traverse entire tree).
- BST: Visits nodes along one path from root to leaf.
As the number of nodes (n) grows, how many nodes do we check?
| Input Size (n) | Approx. Operations Plain Tree | Approx. Operations BST |
|---|---|---|
| 10 | Up to 10 nodes | Up to 4 nodes (height ~ log2 10) |
| 100 | Up to 100 nodes | Up to 7 nodes (height ~ log2 100) |
| 1000 | Up to 1000 nodes | Up to 10 nodes (height ~ log2 1000) |
Pattern observation: Plain tree search grows linearly with n, but BST search grows slowly with tree height, which is about log n.
Time Complexity: O(n) for plain binary tree search, O(log n) for balanced BST search
This means searching in a BST is much faster because it skips large parts of the tree by using order.
[X] Wrong: "A BST always searches in O(log n) time no matter what."
[OK] Correct: If the BST is unbalanced (like a linked list), search can degrade to O(n), same as a plain tree.
Understanding why BSTs are faster helps you explain data structure choices clearly and shows you know how structure affects speed.
"What if the BST is not balanced? How would that affect the search time complexity?"
Practice
Solution
Step 1: Understand the structure difference
A plain Binary Tree has no order among nodes, while BST keeps nodes sorted so left child is smaller and right child is larger.Step 2: Analyze search efficiency
Because BST is sorted, searching can skip half the tree at each step, making search faster than in a plain Binary Tree.Final Answer:
Because BST keeps elements in a sorted order allowing faster search -> Option BQuick Check:
Sorted structure = faster search [OK]
- Thinking BST uses less memory
- Confusing number of children per node
- Believing BST does not use pointers
Solution
Step 1: Understand pointer usage in Go structs
BST nodes must point to child nodes, so left and right should be pointers to Node type.Step 2: Check each option
type Node struct { value int; left *Node; right *Node } uses *Node pointers correctly. type Node struct { value int; left Node; right Node } uses Node directly, causing infinite size. type Node struct { value int; left *int; right *int } uses *int which is wrong type. type Node struct { value int; left interface{}; right interface{} } uses interface{} which is too generic and not typical for BST.Final Answer:
type Node struct { value int; left *Node; right *Node } -> Option AQuick Check:
Pointers to Node = correct struct [OK]
- Using non-pointer fields causing infinite struct size
- Using wrong pointer types like *int
- Using interface{} instead of concrete types
package main
import "fmt"
type Node struct {
value int
left, right *Node
}
func insert(root *Node, val int) *Node {
if root == nil {
return &Node{value: val}
}
if val < root.value {
root.left = insert(root.left, val)
} else {
root.right = insert(root.right, val)
}
return root
}
func inorder(root *Node) {
if root != nil {
inorder(root.left)
fmt.Print(root.value, " ")
inorder(root.right)
}
}
func main() {
var root *Node
values := []int{7, 3, 9, 1, 5}
for _, v := range values {
root = insert(root, v)
}
inorder(root)
}
Solution
Step 1: Insert values into BST
Insert 7 as root, then 3 to left, 9 to right, 1 to left of 3, 5 to right of 3.Step 2: Perform inorder traversal
Inorder visits left subtree, root, then right subtree. So nodes print in sorted order: 1 3 5 7 9.Final Answer:
1 3 5 7 9 -> Option AQuick Check:
Inorder traversal = sorted values [OK]
- Confusing preorder or postorder with inorder
- Misplacing nodes during insertion
- Printing values without spaces
package main
import "fmt"
type Node struct {
value int
left, right *Node
}
func insert(root *Node, val int) {
if root == nil {
root = &Node{value: val}
return
}
if val < root.value {
insert(root.left, val)
} else {
insert(root.right, val)
}
}
func main() {
var root *Node
insert(root, 10)
insert(root, 5)
insert(root, 15)
fmt.Println(root)
}
Solution
Step 1: Analyze insert function parameter
insert takes root as value, not pointer to pointer, so changes to root inside insert do not affect caller.Step 2: Understand effect on root variable
root in main remains nil because insert does not update it outside its scope, so tree is never built.Final Answer:
root remains nil because insert does not update root pointer -> Option DQuick Check:
Pointer not updated = root nil [OK]
- Assuming root updates inside insert affect main
- Ignoring pointer semantics in Go
- Confusing runtime error with logical error
Solution
Step 1: Compare search efficiency
BST keeps elements sorted, so average search is O(log n), faster than plain Binary Tree which has no order.Step 2: Consider worst-case scenario
If BST becomes unbalanced (like a linked list), search degrades to O(n) time.Final Answer:
BST allows faster average search (O(log n)) but worst-case can be O(n) if unbalanced -> Option CQuick Check:
Average O(log n), worst O(n) in BST [OK]
- Thinking BST always has O(1) search
- Confusing plain Binary Tree search time
- Ignoring unbalanced BST worst case
Ready to prove it?
Pass the test and this pattern moves unknown
