Animating Algorithms
Rotations on Binary Search Trees
To illustrate a few of the features available, consider the two
insets below.
These panels illustrate a repeating sequence of rotations on nodes of
a binary tree. On the left, we present a rather standard representation
of the tree; on the right, the nodes are laid out to emphasize their
inorder (sorted) ordering (which is relevant in this case, since
rotations do minor surgery on binary search trees while preserving
inorder ordering). Various other features are present to illustrate how
the tree is traversed during an operation (the small ball) and to
focus on the nodes where these operations are applied (the spotlight).
The animations also provide menus for changing these and other properties.
Also feel free to point, click and tug on them a bit!
Additional operations
The links above will take you to several demos, which construct an
initial tree, perform a few generic operations, and then allow you
to manipulate the resulting structures (insert, locate and delete
elements). Menus are provided to change aspects of the animation - the
node representation, presence or absence of labels, and tree layout
and physics. When labels are not display, you can always click
on a node to query its contents. There are just a few basic rules.
- For an insertion, click on or near a node and select insert.
Clicking to the left of a node inserts an immediate predecessor of that
node's key; to the right inserts an immediate successor.
- If the animation is unbearably slow on your machine, try smaller
(first menu) or no labels. The two animations above use the options
(1) large nodes with no labels and flexible layout, and (2) no nodes
with fancy labels and inorder layout, respectively. For red-black trees,
there is also an option to display them as 2-3-4 trees; in this case,
no nodes with fancy labels gives a good impression of the resulting
tree, while still exposing the underlying red-black tree
implementation.
Your feedback
is appreciated!
Doug Ierardi and
Ta-Wei Li, with the assistance of
Cavit Aydin,
Steven Deng,
Craig Kawahara,
Susanto Kolim and
Jack Tsou