Bird
Raised Fist0
Data Structures Theoryknowledge~10 mins

Searching in BST in Data Structures Theory - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a value exists in a BST.

Data Structures Theory
def search_bst(node, value):
    if node is None:
        return False
    if node.val == [1]:
        return True
    elif value < node.val:
        return search_bst(node.left, value)
    else:
        return search_bst(node.right, value)
Drag options to blanks, or click blank then click option'
Anode.left
Bnode.val
Cvalue
Dnode.right
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing node.val to itself instead of the search value.
2fill in blank
medium

Complete the code to decide which subtree to search next in a BST.

Data Structures Theory
def search_bst(node, value):
    if node is None:
        return False
    if node.val == value:
        return True
    elif value [1] node.val:
        return search_bst(node.left, value)
    else:
        return search_bst(node.right, value)
Drag options to blanks, or click blank then click option'
A>=
B>
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes searching the wrong subtree.
3fill in blank
hard

Fix the error in the base case of the BST search function.

Data Structures Theory
def search_bst(node, value):
    if node == [1]:
        return False
    if node.val == value:
        return True
    elif value < node.val:
        return search_bst(node.left, value)
    else:
        return search_bst(node.right, value)
Drag options to blanks, or click blank then click option'
ANone
BTrue
CFalse
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing node to 0 or boolean values instead of None.
4fill in blank
hard

Fill both blanks to complete the recursive BST search function correctly.

Data Structures Theory
def search_bst(node, value):
    if node == [1]:
        return False
    if node.val == value:
        return True
    elif value [2] node.val:
        return search_bst(node.left, value)
    else:
        return search_bst(node.right, value)
Drag options to blanks, or click blank then click option'
ANone
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators or incorrect base case check.
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps node values to their search result in BST.

Data Structures Theory
results = {: search_bst(root, {BLANK_2}}) for {{BLANK_2}} in values
Drag options to blanks, or click blank then click option'
A{
Bv
Cvalue
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or missing braces.

Practice

(1/5)
1. What is the main advantage of searching in a Binary Search Tree (BST)?
easy
A. It allows faster search by using the order of elements
B. It stores elements in random order for quick access
C. It uses hashing to find elements instantly
D. It searches all nodes one by one sequentially

Solution

  1. Step 1: Understand BST property

    A BST keeps elements ordered so smaller values are on the left and larger on the right.
  2. Step 2: Use order for searching

    This order lets us decide to go left or right, skipping half the tree each step, making search faster.
  3. Final Answer:

    It allows faster search by using the order of elements -> Option A
  4. Quick Check:

    BST order speeds search = It allows faster search by using the order of elements [OK]
Hint: BST order guides search direction quickly [OK]
Common Mistakes:
  • Thinking BST stores elements randomly
  • Confusing BST with hashing
  • Assuming linear search in BST
2. Which of the following is the correct way to decide the next node to visit when searching for a value in a BST?
easy
A. Go left if target is greater than current node
B. Go left if target is smaller than current node
C. Go right if target is smaller than current node
D. Always go to the root node

Solution

  1. Step 1: Recall BST search rule

    If the target is smaller than the current node's value, we move to the left child.
  2. Step 2: Apply rule to options

    Go left if target is smaller than current node correctly states to go left if target is smaller, which matches BST property.
  3. Final Answer:

    Go left if target is smaller than current node -> Option B
  4. Quick Check:

    Smaller target -> left child = Go left if target is smaller than current node [OK]
Hint: Smaller target means go left in BST [OK]
Common Mistakes:
  • Reversing left and right directions
  • Ignoring BST ordering rules
  • Always going to root node
3. Consider the BST below:
      15
     /  \
    10  20
   /    / \
  8    17 25

Which nodes will be visited when searching for the value 17?
medium
A. [10, 8, 17]
B. [15, 10, 8]
C. [15, 20, 25]
D. [15, 20, 17]

Solution

  1. Step 1: Start at root and compare with 17

    Root is 15. Since 17 > 15, move right to 20.
  2. Step 2: Compare 20 with 17

    17 < 20, so move left to 17, which matches the target.
  3. Final Answer:

    [15, 20, 17] -> Option D
  4. Quick Check:

    Path to 17 = 15 -> 20 -> 17 [OK]
Hint: Follow BST rules: left if smaller, right if larger [OK]
Common Mistakes:
  • Going left from 15 when target is larger
  • Skipping nodes in path
  • Confusing node values
4. You wrote code to search a BST but it always returns None even when the value exists. What is the most likely mistake?
medium
A. Not moving to left child when target is smaller
B. Using a queue instead of recursion
C. Always moving to left child regardless of target
D. Checking only the root node

Solution

  1. Step 1: Understand BST search logic

    Search must move left if target is smaller, right if larger.
  2. Step 2: Identify error in always moving left

    If code always moves left, it misses nodes on the right side where target might be.
  3. Final Answer:

    Always moving to left child regardless of target -> Option C
  4. Quick Check:

    Wrong direction causes search failure = Always moving to left child regardless of target [OK]
Hint: Move left or right based on comparison, not always left [OK]
Common Mistakes:
  • Ignoring right subtree
  • Checking only root node
  • Using wrong data structure for traversal
5. Given a BST where some nodes have duplicate values on the right subtree, how should the search algorithm be adapted to find all occurrences of a target value?
hard
A. Traverse both left and right subtrees when node equals target
B. Search left subtree only once target is found
C. Stop search immediately after first match
D. Ignore duplicates and return first found

Solution

  1. Step 1: Understand duplicates in BST

    Duplicates are stored in right subtree, so multiple matches can exist there.
  2. Step 2: Adapt search to find all matches

    When a node equals target, search both left (for smaller) and right (for duplicates) subtrees to find all occurrences.
  3. Final Answer:

    Traverse both left and right subtrees when node equals target -> Option A
  4. Quick Check:

    Check both sides for duplicates = Traverse both left and right subtrees when node equals target [OK]
Hint: Check both subtrees when value matches to find duplicates [OK]
Common Mistakes:
  • Stopping after first match
  • Ignoring right subtree duplicates
  • Searching only one subtree