Bird
Raised Fist0
Data Structures Theoryknowledge~10 mins

BST property and invariant 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 sentence to describe the BST property: In a Binary Search Tree, the left subtree of a node contains only nodes with keys {{BLANK_1}} the node's key.

Data Structures Theory
In a Binary Search Tree, the left subtree of a node contains only nodes with keys [1] the node's key.
Drag options to blanks, or click blank then click option'
Agreater than
Bunrelated to
Cequal to
Dless than
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing left subtree keys as greater than the node's key.
Thinking keys can be equal in the left subtree.
2fill in blank
medium

Complete the sentence to describe the BST property: The right subtree of a node contains only nodes with keys {{BLANK_1}} the node's key.

Data Structures Theory
The right subtree of a node contains only nodes with keys [1] the node's key.
Drag options to blanks, or click blank then click option'
Agreater than
Bunrelated to
Cequal to
Dless than
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming right subtree keys are less than the node's key.
Allowing equal keys in the right subtree.
3fill in blank
hard

Fix the error in this BST invariant statement: "For every node, all keys in the left subtree are {{BLANK_1}} or equal to the node's key."

Data Structures Theory
For every node, all keys in the left subtree are [1] or equal to the node's key.
Drag options to blanks, or click blank then click option'
Agreater than
Bless than
Cgreater than or equal to
Dless than or equal to
Attempts:
3 left
💡 Hint
Common Mistakes
Allowing equal keys in the left subtree.
Confusing less than with greater than.
4fill in blank
hard

Fill both blanks to complete the BST invariant: "For every node, all keys in the left subtree are {{BLANK_1}} the node's key, and all keys in the right subtree are {{BLANK_2}} the node's key."

Data Structures Theory
For every node, all keys in the left subtree are [1] the node's key, and all keys in the right subtree are [2] the node's key.
Drag options to blanks, or click blank then click option'
Aless than
Bequal to
Cgreater than
Dunrelated to
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up left and right subtree key comparisons.
Allowing equal keys on either side.
5fill in blank
hard

Fill all three blanks to complete the BST property check code snippet: "if node is not None: if node.left is not None and node.left.key {{BLANK_1}} node.key: raise ValueError('BST property violated'); if node.right is not None and node.right.key {{BLANK_2}} node.key: raise ValueError('BST property violated'); check(node.left); check(node.right)"

Data Structures Theory
if node is not None:
    if node.left is not None and node.left.key [1] node.key:
        raise ValueError('BST property violated')
    if node.right is not None and node.right.key [2] node.key:
        raise ValueError('BST property violated')
    check(node.left)
    check(node.right)
Drag options to blanks, or click blank then click option'
A>=
B<=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators that do not detect violations.
Confusing which side uses which comparison.

Practice

(1/5)
1. What is the main property that defines a Binary Search Tree (BST)?
easy
A. All nodes have exactly two children.
B. Nodes are arranged in a circular linked list.
C. Left child nodes are smaller, right child nodes are larger than the parent node.
D. Each node stores a unique key with no order.

Solution

  1. Step 1: Understand BST node arrangement

    A BST arranges nodes so that left children have smaller values and right children have larger values than their parent node.
  2. Step 2: Compare options with BST definition

    Left child nodes are smaller, right child nodes are larger than the parent node. correctly states this property. Other options describe different or incorrect structures.
  3. Final Answer:

    Left child nodes are smaller, right child nodes are larger than the parent node. -> Option C
  4. Quick Check:

    BST property = left < parent < right [OK]
Hint: Remember BST means left smaller, right larger [OK]
Common Mistakes:
  • Thinking all nodes must have two children
  • Confusing BST with other tree types
  • Ignoring the order property in BST
2. Which of the following is the correct way to check if a node's left child maintains the BST property?
easy
A. left_child.value > node.value
B. left_child.value < node.value
C. left_child.value == node.value
D. left_child.value >= node.value

Solution

  1. Step 1: Recall BST left child rule

    In a BST, the left child's value must be less than the parent's value.
  2. Step 2: Match this rule to options

    left_child.value < node.value correctly states left_child.value < node.value. Other options violate this rule.
  3. Final Answer:

    left_child.value < node.value -> Option B
  4. Quick Check:

    Left child < parent [OK]
Hint: Left child must be smaller than parent [OK]
Common Mistakes:
  • Using greater than or equal instead of less than
  • Allowing equal values on left child
  • Confusing left and right child rules
3. Given the BST below, what is the correct in-order traversal output?
      10
     /  \
    5    15
   / \     \
  3   7     20
medium
A. [3, 5, 7, 10, 15, 20]
B. [10, 5, 3, 7, 15, 20]
C. [20, 15, 10, 7, 5, 3]
D. [5, 3, 7, 10, 15, 20]

Solution

  1. Step 1: Understand in-order traversal

    In-order traversal visits left subtree, then node, then right subtree, producing sorted order in BST.
  2. Step 2: Traverse the tree in-order

    Left subtree of 10: nodes 3, 5, 7 in order; then 10; then right subtree 15, 20.
  3. Final Answer:

    [3, 5, 7, 10, 15, 20] -> Option A
  4. Quick Check:

    In-order traversal = sorted nodes [OK]
Hint: In-order traversal of BST gives sorted list [OK]
Common Mistakes:
  • Confusing pre-order or post-order with in-order
  • Listing nodes in insertion order
  • Reversing the traversal order
4. Consider the following BST insertion code snippet. What is the error?
def insert(node, value):
    if node is null:
        return Node(value)
    if value < node.value:
        node.left = insert(node.left, value)
    else:
        node.right = insert(node.right, value)
    return node
medium
A. The code does not handle duplicate values correctly.
B. The base case for node is incorrect.
C. The recursive calls do not update the tree.
D. The function does not return the updated node.

Solution

  1. Step 1: Analyze duplicate handling in insertion

    The code inserts duplicates into the right subtree without restriction, which may violate BST uniqueness if duplicates are not allowed.
  2. Step 2: Check other parts of the code

    Base case and recursive updates are correct; function returns updated node properly.
  3. Final Answer:

    The code does not handle duplicate values correctly. -> Option A
  4. Quick Check:

    Duplicates need special handling in BST insert [OK]
Hint: Check how duplicates are treated in insertion [OK]
Common Mistakes:
  • Assuming duplicates are automatically handled
  • Ignoring return statements in recursion
  • Confusing base case with recursive case
5. You want to verify if a binary tree is a valid BST. Which approach correctly checks the BST property for all nodes?
hard
A. Check if the tree has unique values only.
B. Check if the tree is balanced and has no cycles.
C. Check if each node's left child is smaller and right child is larger, recursively.
D. Check if all left descendants are smaller and all right descendants are larger than the node, using min and max limits.

Solution

  1. Step 1: Understand BST validation requirements

    Each node must satisfy that all nodes in its left subtree are smaller and all in right subtree are larger, not just immediate children.
  2. Step 2: Evaluate approaches

    Check if all left descendants are smaller and all right descendants are larger than the node, using min and max limits. uses min and max limits to ensure all descendants satisfy BST property, which is correct. Check if each node's left child is smaller and right child is larger, recursively. only checks immediate children, which is insufficient.
  3. Final Answer:

    Check if all left descendants are smaller and all right descendants are larger than the node, using min and max limits. -> Option D
  4. Quick Check:

    BST validation requires range checks, not just immediate children [OK]
Hint: Use min/max limits to validate BST recursively [OK]
Common Mistakes:
  • Checking only immediate children values
  • Confusing BST property with tree balance
  • Assuming unique values guarantee BST