ScienceComputer Science

Binary Search Tree
calculator.

Insert keys into a binary search tree, then see its shape, traversals and search path.

How it's worked out

A binary search tree (BST) keeps one rule at every node, which Sedgewick and Wayne call symmetric order: every key in the left subtree is smaller than the node's key, and every key in the right subtree is larger.

left keys\displaystyle \text{left keys}<node\displaystyle {}<\;\text{node}<right keys\displaystyle {}<\;\text{right keys}

To insert a key, start at the root. If the key is smaller, go left; if it is larger, go right. Repeat until you reach an empty spot, and put the key there. A key equal to one already in the tree is skipped, as in a set. Searching follows exactly the same path and stops when it finds the key or runs out of tree.

The keys go in one at a time, in the order you type them, so the order decides the shape. The steps list each insertion with the keys it was compared with, and the last step checks the rule: an in-order walk must give the keys in sorted order.

Height is the number of edges on the longest path from the root down to a leaf, so a tree with one node has height 0, as NIST defines it. A tree is height-balanced when, at every node, the two subtrees differ in height by at most 1. The shortest possible height for n keys is the one where every level is full except perhaps the last.

  • In-order visits the left subtree, then the node, then the right subtree. On a BST this gives the keys in sorted order.
  • Pre-order visits the node first, then left, then right. Inserting the keys in this order rebuilds the same tree.
  • Post-order visits left, then right, then the node, so children come before their parent.
  • Level order goes row by row from the root, left to right, using a queue.

Worked examples

Nadia checks a textbook exercise

Nadia inserts 50, 30, 70, 20, 40, 60, 80. 50 becomes the root. 30 is smaller, so it goes left; 70 goes right. 60 is compared with 50, 70 and becomes the left child of 70. The tree has height 2 and is height-balanced, with every level full.

In-order gives 20, 30, 40, 50, 60, 70, 80, which is sorted, as it must be. Pre-order is 50, 30, 20, 40, 70, 60, 80, post-order is 20, 40, 30, 60, 80, 70, 50 and level order is 50, 30, 70, 20, 40, 60, 80. The search for 60 visits 50, 70, 60: 3 comparisons. On average a search for a key in this tree takes 2.43.

Open this example in the tool

Omar sees what sorted input does

Omar inserts 1, 2, 3, 4, 5, 6 in order. Each key is larger than every key before it, so each one goes to the right of the last. The tree is a chain of height 5, and finding 6 takes 6 comparisons, no better than reading a list.

The same six keys inserted middle first, 3, 1, 5, 2, 4, 6, give height 2, the shortest possible. That is the job a self-balancing tree, such as an AVL or red-black tree, does for you as keys arrive.

Open this example in the tool

Ten keys in a random order

Inserting 41, 20, 65, 11, 29, 50, 91, 32, 72, 99 gives height 3, and the shortest possible for 10 keys is 3. The search for 72 visits 41, 65, 91, 72.

The same keys sorted first would make a chain of height 9. Random orders usually land close to the minimum: Sedgewick and Wayne give about 1.39 lg N comparisons for an average search in a tree built from N random keys.

Open this example in the tool

Reading your result

The big number is the height. Search, insert and delete each follow one path from the root, so they take time in proportion to the height. A balanced tree gains a level only when the number of keys roughly doubles, while a chain gets one level deeper with every key.

Compare the height with the shortest possible. If the two match, the shape is as good as a plain BST can get. A height far above it means the keys arrived in a near-sorted order. The table of orders shows the same keys sorted, reversed and middle first.

Different orders can still give the same tree. 2, 1, 3 and 2, 3, 1 both put 2 at the root with 1 and 3 below: their pre-orders are 2, 1, 3 and 2, 1, 3. What matters is which key arrives first in each subtree.

Duplicates are skipped. In 5, 3, 5, 8 the second 5 meets the root and goes no further, so the tree holds 3 keys and lists 5 as skipped. Some courses keep duplicates with a count in each node instead.

Common mistakes

  • Counting nodes instead of edges for the height. A path with 3 nodes has 2 edges. Check which your course uses: NIST and most textbooks count edges, while some libraries, such as GLib, count nodes.
  • Comparing with the parent only. The rule holds for the whole subtree. In a tree with root 50, a key of 55 cannot sit in the left subtree, even under a node of 30.
  • Sorting the keys before inserting. Sorted input is the worst case and makes a chain. Insert in the order the question gives.
  • Mixing up pre-order and level order. Both start at the root, but pre-order finishes the whole left subtree before it moves right.

Questions people ask

Yes, often. Each new key goes where the comparisons send it, so whichever key comes first in a group becomes that group's root. Some different orders do give the same tree, as long as each subtree's first key is the same.

Sources