Chapter 10 · Simulation

Binary Search Tree Traversal Simulator

← Chapter 10
Sample tree loaded.

Binary Search Tree Traversal Simulator

In a binary search tree, every key in a node's left subtree is smaller than the node and every key in its right subtree is larger. Each comparison rules out one subtree, so an operation follows a single path from the root toward a leaf.

How to use it

  • Type a Value and press Insert, Search or Delete. Minimum and Maximum follow the leftmost or rightmost path.
  • Inorder, Preorder, Postorder and Level order animate the traversals.
  • Sample restores the sample tree, Random builds a random one, Reset empties the tree, and Stop interrupts the animation. The tree holds at most 12 nodes.

Deletion

  • Leaf: removed directly.
  • One child: the child takes the node's place.
  • Two children: the inorder successor (the minimum of the right subtree) is copied into the node, and then the successor is deleted.

Notes

  • An inorder traversal of a binary search tree visits the keys in sorted order.
  • Each node is drawn at its position in inorder, so a left child always appears to the left of its parent.
  • Search, insertion and deletion take O(h) time: O(log n) in a balanced tree, but O(n) when the keys arrive in sorted order and the tree degenerates into a chain.