DECMATRIX

Mathematical Tools

Binary Search Tree Exercises

Configure or restart
Next exercise settings

How to practice BST insertions

In Configure or restart, choose 1 to 30 nodes and start the exercise. The current value, selected node and actions are grouped together. Select the parent in the tree or list, then choose a side. Help and hints reveals progressive clues and counts each hint used. When finished, check correct actions, errors and score; retry the same sequence or generate another one. Changing the count configures only the next New exercise; it does not modify the current tree.

How to find a value's position

  1. In an empty tree, use Insert at root. The root is the first value in the sequence.
  2. Compare the next value with the root: go left if it is smaller, or right if it is larger.
  3. Repeat the comparison at the child you reached. When the correct side is empty, select that parent and insert on that side.
  4. Ancestor bounds still apply. No insertion requires a rotation: a BST may be deep or asymmetric and still be correct.

Worked example: 40, 20, 60, 10, 30

Sequence: 40 → 20 → 60 → 10 → 30
4020601030

Smaller values go left; larger values go right. Repeat the comparison at each node until the correct position is empty.

Insert 40 at the root. Since 20 is smaller than 40, it goes left; 60 is larger and goes right. For 10, go left from 40 and left again from 20. For 30, go left from 40, then right from 20: it lies in the interval 20 < value < 40. In contrast, 50 cannot occupy that position because it belongs to the right subtree of 40.

Common BST construction mistakes

  • Comparing only with the parent: a position must respect every ancestor on the path.
  • Choosing an occupied position: keep searching through the existing child until the correct side is empty.
  • Trying to balance the tree: inserting 10, 20 and 30 should leave a right-hand chain. BSTs do not have AVL balance constraints.
  • Repeating values: exercises use distinct values so that each position is unambiguous. Incorrect attempts neither change the tree nor advance the value.

FAQ

Decmatrix - Your open-source mathematical toolkit for calculations, conversions, and more.
Terms of Use
Privacy Policy
Contact: decimatrix25@gmail.com