0
0
Data Structures Theoryknowledge~10 mins

Searching in BST in Data Structures Theory - Interactive Code Practice

Choose your learning style9 modes available
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.