AlgoScope

Morris Traversal

AlgorithmadvancedTree Traversals

O(1)-space traversal by temporarily threading each node's inorder predecessor to it.

Decision · step 2 of 18Tree Traversals: Inorder with threads instead of a stack
203040!50607080currentpred

50 has a left subtree, so something comes before it. Its inorder predecessor is 40, the rightmost node on its left. That node's right pointer is empty, so the left subtree has not been walked yet.

Open in the player →or start at step 2

What you will see

Temporary "thread" edges appear from rightmost leaves back up, then are removed.

How tree traversals works →

Cost

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(1)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next, try operations in any order.

Screen readers: Nodes announce value, left child and right child; each step announces the comparison and the direction taken or the structural change.

Reduced motion: Focus jumps node to node with a static ring; rotations become a crossfade between the two layouts.

Variants

  • Morris Inorder Thread predecessor to node.
  • Morris Preorder Output on thread creation.
  • Morris Postorder Via a dummy root and reversed right chains.