Chapter 9 · Simulation

General Tree Simulator

← Chapter 9
Click a node to select it.

General Tree Simulator

A rooted tree in which a node may have any number of children. Click a node to select it; every operation acts on the selected node.

Controls

  • Add child hangs a new node with the given label under the selected node (12 nodes at most). Select root selects the root.
  • Delete subtree removes the selected node together with all its descendants.
  • Ancestors highlights the path up to the root; Descendants highlights the whole subtree.
  • DFS preorder and BFS level order animate the two traversals. Stop interrupts an animation, Clear highlights removes the colors, and Load example brings back the sample tree.

Terms

  • A leaf is a node with no children. The degree of a node is its number of children.
  • The depth of a node is the number of edges from the root to it; the height of the tree is the largest depth.
  • In the Selected box, d is the depth and deg the degree.

Traversals

  • Preorder DFS visits a node first and then its subtrees from left to right. The orange edges trace the current recursion path down from the root.
  • BFS uses a queue: dequeue the front node, enqueue its children, and the tree is visited level by level.
  • Both traversals visit every node exactly once, so each takes O(n) time.