0
0
DSA Javascriptprogramming~10 mins

Morris Traversal Inorder Without Stack in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Morris Traversal Inorder Without Stack
Start at root
Check if current.left is null?
YesVisit current node, move to current.right
No
Find predecessor in left subtree
Check if predecessor.right is null?
YesMake thread: predecessor.right = current, move current to current.left
No
Remove thread: predecessor.right = null, visit current, move current to current.right
Repeat until current is null
Traversal complete
The traversal moves through the tree without stack by creating temporary threads to predecessors, visiting nodes in inorder sequence.
Execution Sample
DSA Javascript
function morrisInorder(root) {
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      console.log(current.val);
      current = current.right;
    } else {
      let predecessor = current.left;
      while (predecessor.right !== null && predecessor.right !== current) {
        predecessor = predecessor.right;
      }
      if (predecessor.right === null) {
        predecessor.right = current;
        current = current.left;
      } else {
        predecessor.right = null;
        console.log(current.val);
        current = current.right;
      }
    }
  }
}
This code prints the inorder traversal of a binary tree using Morris Traversal without using stack or recursion.
Execution Table
StepOperationCurrent NodePredecessorThread Created/RemovedVisited NodeVisual Tree State
1Start at root1N/ANoNo1 / \ 2 3
2current.left != null, find predecessor12NoNo1 / \ 2 3
3predecessor.right == null, create thread12Thread created: 2.right = 1No1 / \ 2→ 3
4Move current to left child2N/ANoNo1 / \ 2→ 3
5current.left == null, visit node2N/ANoVisited 21 / \ 2→ 3
6Move current to right child1N/ANoNo1 / \ 2→ 3
7current.left != null, find predecessor12NoNo1 / \ 2→ 3
8predecessor.right == current, remove thread12Thread removed: 2.right = nullNo1 / \ 2 3
9Visit current node1N/ANoVisited 11 / \ 2 3
10Move current to right child3N/ANoNo1 / \ 2 3
11current.left == null, visit node3N/ANoVisited 31 / \ 2 3
12Move current to right childnullN/ANoNo1 / \ 2 3
13current is null, traversal endsnullN/ANoNo1 / \ 2 3
💡 Traversal ends when current becomes null after visiting all nodes.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 8After Step 9After Step 11Final
current112113null
predecessorN/A2N/A2N/AN/AN/A
threadNoYes (2.right=1)Yes (2.right=1)No (2.right=null)NoNoNo
Key Moments - 3 Insights
Why do we create a thread from predecessor.right to current?
Creating the thread (see Step 3) allows us to return to the current node after finishing the left subtree without using a stack.
Why do we remove the thread later?
Removing the thread (see Step 8) restores the original tree structure after visiting the current node.
Why do we visit nodes only when current.left is null or after removing a thread?
Visiting nodes at these points (Steps 5, 9, 11) ensures inorder sequence: left subtree, node, then right subtree.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the thread from node 2 to node 1 created?
AStep 5
BStep 8
CStep 3
DStep 10
💡 Hint
Check the 'Thread Created/Removed' column in the execution table.
At which step does the traversal visit node 1?
AStep 5
BStep 9
CStep 8
DStep 11
💡 Hint
Look at the 'Visited Node' column in the execution table.
If the tree had no left children, how would the traversal proceed?
AIt would visit nodes immediately and move to right child.
BIt would create threads for every node.
CIt would get stuck in an infinite loop.
DIt would skip visiting nodes.
💡 Hint
Refer to the condition when current.left is null in the code and execution table.
Concept Snapshot
Morris Traversal Inorder Without Stack:
- Uses threaded binary tree concept.
- Creates temporary right links (threads) to predecessors.
- Visits nodes when left subtree is done.
- Removes threads to restore tree.
- Traverses in O(n) time and O(1) space.
Full Transcript
Morris Traversal is a way to do inorder traversal of a binary tree without using extra space like stack or recursion. It works by creating temporary threads from a node's inorder predecessor back to the node. This allows the traversal to return to the node after finishing its left subtree. When the thread is encountered again, it is removed and the node is visited. If a node has no left child, it is visited immediately and traversal moves to the right child. This process continues until all nodes are visited and the tree is restored to its original shape. The execution table shows each step including thread creation, removal, and node visits. The variable tracker shows how current and predecessor pointers move and how threads are created and removed. Key moments clarify why threads are needed and when nodes are visited. The visual quiz tests understanding of these steps. This method achieves inorder traversal in linear time with constant space.