Consider searching for a value in a plain binary tree versus a binary search tree (BST). Why does a BST generally allow faster search?
Think about how the order of nodes helps decide which branch to follow during search.
A BST keeps nodes in order: left child smaller, right child larger. This lets you decide to go left or right at each step, skipping half the nodes, making search faster than checking every node in a plain binary tree.
Given the following Go code snippets for searching a value in two trees, what is the output?
package main import "fmt" type Node struct { Val int Left, Right *Node } // Plain binary tree search: checks all nodes func searchPlain(root *Node, target int) bool { if root == nil { return false } if root.Val == target { return true } return searchPlain(root.Left, target) || searchPlain(root.Right, target) } // BST search: uses ordering to decide path func searchBST(root *Node, target int) bool { if root == nil { return false } if root.Val == target { return true } else if target < root.Val { return searchBST(root.Left, target) } else { return searchBST(root.Right, target) } } func main() { // Plain binary tree rootPlain := &Node{Val: 10} rootPlain.Left = &Node{Val: 5} rootPlain.Right = &Node{Val: 15} rootPlain.Left.Left = &Node{Val: 3} rootPlain.Right.Right = &Node{Val: 20} // BST with same values rootBST := &Node{Val: 10} rootBST.Left = &Node{Val: 5} rootBST.Right = &Node{Val: 15} rootBST.Left.Left = &Node{Val: 3} rootBST.Right.Right = &Node{Val: 20} fmt.Println(searchPlain(rootPlain, 20)) fmt.Println(searchBST(rootBST, 20)) }
Both trees contain the value 20. Check if both search functions find it.
Both searchPlain and searchBST find the value 20 in their respective trees, so both print true.
What is the printed structure after inserting values 10, 5, 15, 3, 20 into a plain binary tree and a BST?
package main import "fmt" type Node struct { Val int Left, Right *Node } // Insert in plain binary tree: always to left if possible func insertPlain(root *Node, val int) *Node { if root == nil { return &Node{Val: val} } if root.Left == nil { root.Left = insertPlain(root.Left, val) } else { root.Right = insertPlain(root.Right, val) } return root } // Insert in BST func insertBST(root *Node, val int) *Node { if root == nil { return &Node{Val: val} } if val < root.Val { root.Left = insertBST(root.Left, val) } else { root.Right = insertBST(root.Right, val) } return root } func printInOrder(root *Node) { if root == nil { return } printInOrder(root.Left) fmt.Print(root.Val, " ") printInOrder(root.Right) } func main() { var rootPlain *Node vals := []int{10, 5, 15, 3, 20} for _, v := range vals { rootPlain = insertPlain(rootPlain, v) } var rootBST *Node for _, v := range vals { rootBST = insertBST(rootBST, v) } printInOrder(rootPlain) fmt.Println() printInOrder(rootBST) }
In-order traversal of BST prints sorted values. Plain binary tree insertion is not ordered but in-order traversal still visits left-root-right.
In-order traversal prints nodes in ascending order for BST ('3 5 10 15 20'). For plain binary tree, the left-preferred insertion results in '5 10 3 15 20', demonstrating no ordering guarantee.
Which reason best explains why searching in a plain binary tree can be slower than in a BST?
Think about how order helps reduce search steps.
Plain binary trees have no ordering rules, so to find a value you may need to check every node. BSTs use ordering to skip large parts of the tree during search.
You have a large set of numbers and need to perform many searches quickly. Which data structure is best and why?
Consider search time complexity and data ordering.
A balanced BST keeps data sorted and balanced, allowing searches in O(log n) time, which is much faster than linear search in arrays or linked lists.