0
0
Data Structures Theoryknowledge~10 mins

Insertion 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 insert a new node in a Binary Search Tree (BST).

Data Structures Theory
if root is None:
    root = [1](key)
Drag options to blanks, or click blank then click option'
ANode
BTree
CBSTNode
DNodeKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not exist like 'Tree' or 'BSTNode'.
Trying to insert the key directly without creating a node.
2fill in blank
medium

Complete the code to decide where to insert the new key in the BST.

Data Structures Theory
if key < root.key:
    root.left = insert(root.left, [1])
Drag options to blanks, or click blank then click option'
Aroot.left
Bnew_key
Croot.key
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root.key instead of key to the recursive call.
Passing root.left which is a node, not the key.
3fill in blank
hard

Fix the error in the code to insert the key in the right subtree.

Data Structures Theory
elif key > root.key:
    root.right = insert(root.right, [1])
Drag options to blanks, or click blank then click option'
Aroot.key
Bkey
Croot.right
Dnew_key
Attempts:
3 left
💡 Hint
Common Mistakes
Passing root.key instead of key.
Passing root.right which is a node, not the key.
4fill in blank
hard

Fill both blanks to complete the insertion function that returns the updated root.

Data Structures Theory
def insert(root, key):
    if root is None:
        return [1](key)
    if key < root.key:
        root.left = insert(root.left, key)
    elif key > root.key:
        root.right = insert(root.right, key)
    else:
        return root
    return [2]
Drag options to blanks, or click blank then click option'
ANode
Broot
Ckey
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the key instead of the root.
Not returning anything after insertion.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps node keys to their depths if depth is greater than 1.

Data Structures Theory
depths = {node.key: depth for node, depth in nodes if depth [1] 1 and node.key [2] [3]
Drag options to blanks, or click blank then click option'
A>
B!=
CNone
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of != for the key check.
Using <= or < instead of > for depth.