Binary search trees

Computer Science · Data structures

Question 1 of 10

0

    Values are inserted one at a time, in this order, into an empty binary search tree: 91, 36, 41, 51. What is the value of the left child of the root?

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Binary search trees explained

    A binary search tree is a tree in which each node has at most two children. Everything in a node's left subtree is smaller than the node's value, and everything in its right subtree is larger. This ordering makes searching fast in a balanced tree, because each comparison rules out one whole branch.

    To search, start at the root. If the target is smaller than the current node, go left; if it is larger, go right. Stop when you find it or when there is nowhere left to go. To insert a value, follow the same path until you reach an empty position, and add the new node there.

    A common mistake is putting a smaller value in the right subtree because it looks like it belongs there. Left is always smaller and right is always larger, at every node, not only at the children of the root.

    Worked example

    Insert 50, 30, 70, 20 and 40, in that order, into an empty tree.

    1. 50 becomes the root. 30 is smaller, so it goes left. 70 is larger, so it goes right.
    2. 20 is smaller than 50 and smaller than 30, so it goes left of 30. 40 is smaller than 50 and larger than 30, so it goes right of 30.
    3. To search for 40, compare with 50 and go left, then compare with 30 and go right, then compare with 40 and find it. That took 3 comparisons, and the tree holds 5 nodes.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      Values are inserted one at a time, in this order, into an empty binary search tree: 35, 75, 13, 43. What is the value of the left child of the root?

      • 35
      • 75
      • 13
      • 43
    • Medium

      Values are inserted one at a time, in this order, into an empty binary search tree: 14, 28, 61, 78, 87, 89. To search for 14, compare it with the current node. If it matches, stop. If it is smaller, go to the left child; if it is larger, go to the right child. How many comparisons are made, counting the one that finds 14?

    • Hard

      Values are inserted one at a time, in this order, into an empty binary search tree: 64, 51, 35, 65, 76, 78. What is the height of the tree? A single node has height 1.