Consider the following Binary Search Tree (BST) and the search operation for the value 15.
What will be the output of the search method?
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BST { public Node Root; public BST() { Root = null; } public bool Search(int val) { Node current = Root; while (current != null) { if (val == current.Value) return true; else if (val < current.Value) current = current.Left; else current = current.Right; } return false; } } // Tree structure: // 20 // / \ // 10 30 // \ \ // 15 40 BST tree = new BST(); tree.Root = new Node(20); tree.Root.Left = new Node(10); tree.Root.Right = new Node(30); tree.Root.Left.Right = new Node(15); tree.Root.Right.Right = new Node(40); bool result = tree.Search(15); System.Console.WriteLine(result);
Remember, the search moves left if the value is smaller, right if larger, and returns true if found.
The value 15 exists in the BST as the right child of 10. The search will find it and return true.
Using the same BST as before, what will be the output of searching for the value 25?
class Node { public int Value; public Node Left, Right; public Node(int val) { Value = val; Left = Right = null; } } class BST { public Node Root; public BST() { Root = null; } public bool Search(int val) { Node current = Root; while (current != null) { if (val == current.Value) return true; else if (val < current.Value) current = current.Left; else current = current.Right; } return false; } } // Tree structure: // 20 // / \ // 10 30 // \ \ // 15 40 BST tree = new BST(); tree.Root = new Node(20); tree.Root.Left = new Node(10); tree.Root.Right = new Node(30); tree.Root.Left.Right = new Node(15); tree.Root.Right.Right = new Node(40); bool result = tree.Search(25); System.Console.WriteLine(result);
Check if 25 exists by moving left or right at each node.
The value 25 is not in the BST. The search will reach a null node and return false.
For the search operation in a Binary Search Tree (BST) to work correctly, which property must the tree satisfy?
Think about how the search decides to go left or right.
The BST property requires left subtree nodes to be smaller and right subtree nodes to be larger than the current node for search to work efficiently.
Consider this modified search method in a BST. What error will it cause when searching for any value?
public bool Search(int val) { Node current = Root; while (current != null) { if (val == current.Value) return true; else if (val < current.Value) current = current.Right; else current = current.Left; } return false; }
Check the directions the search moves when value is less or greater.
The search moves right when value is less and left when greater, which is opposite of BST property, so it will not find existing values and return false.
Given the BST below, how many nodes will the search method visit when searching for the value 40?
Count the nodes checked including the root and the found node.
// Tree structure: // 25 // / \ // 15 50 // / \ / \ // 10 22 35 70 // \ // 40 // Search method is standard BST search moving left if val < node, right if val > node.
Trace the path from root to 40 step by step.
The search visits nodes 25, then 50, then 35, then 40, totaling 4 nodes.
