0
0
DSA Typescriptprogramming~10 mins

Morris Traversal Inorder Without Stack in DSA Typescript - 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
Find predecessor in left subtree
Check if predecessor.right is null?
YesMake thread: predecessor.right = current
Remove thread: predecessor.right = null
Move current to current.left
Repeat until current is null
This flow shows how Morris Traversal uses temporary threads to traverse the tree inorder without stack or recursion.
Execution Sample
DSA Typescript
function morrisInorder(root) {
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      visit(current);
      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;
        visit(current);
        current = current.right;
      }
    }
  }
}
This code performs inorder traversal of a binary tree without using stack or recursion by creating temporary threads.
Execution Table
StepOperationCurrent NodePredecessor NodeThread Created/RemovedVisual State
1Start at root1nullNone1 / \ 2 3
2current.left != null, find predecessor12None1 / \ 2 3
3predecessor.right == null, create thread12Thread 2.right -> 11 / \ 2->1 3
4Move current to left child2nullNone1 / \ 2->1 3
5current.left == null, no predecessor needed2nullNone1 / \ 2->1 3
6current.left == null, visit node2nullNoneVisited: 2 1 / \ 2->1 3
7Move current to right child12NoneVisited: 2 1 / \ 2->1 3
8predecessor.right == current, remove thread12Remove thread 2.rightVisited: 2 1 / \ 2 3
9Visit current node12NoneVisited: 2,1 1 / \ 2 3
10Move current to right child3nullNoneVisited: 2,1 1 / \ 2 3
11current.left == null, visit node3nullNoneVisited: 2,1,3 1 / \ 2 3
12Move current to right childnullnullNoneVisited: 2,1,3
13current is null, traversal endsnullnullNoneTraversal complete: 2 -> 1 -> 3
💡 current becomes null, traversal ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 9After Step 11Final
current112213null
predecessornull2nullnull2nullnull
threadNone2.right->12.right->12.right->1RemovedNoneNone
visited_nodes[][][][2][2,1][2,1,3][2,1,3]
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 when predecessor.right equals current?
Removing the thread (step 8) restores the original tree structure and signals that the left subtree has been fully visited.
What happens when current.left is null?
When current.left is null (steps 6 and 11), we visit the current node and move to its right child directly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the 'current' node at step 6?
A1
B2
C3
Dnull
💡 Hint
Check the 'Current Node' column in row with Step 6.
At which step is the thread from predecessor.right to current removed?
AStep 8
BStep 11
CStep 3
DStep 13
💡 Hint
Look for 'Remove thread' in the 'Thread Created/Removed' column.
If the tree had no left children, how would the traversal proceed?
AIt would create threads for all nodes
BIt would get stuck in an infinite loop
CIt would visit nodes and move right without creating threads
DIt would skip visiting nodes
💡 Hint
Refer to steps where current.left is null and no threads are created.
Concept Snapshot
Morris Traversal Inorder Without Stack:
- Uses temporary threads to traverse left subtree.
- If current.left is null, visit node and move right.
- Else find predecessor, create thread if none.
- Remove thread after left subtree visited.
- No stack or recursion needed.
- Restores tree structure after traversal.
Full Transcript
Morris Traversal Inorder Without Stack uses temporary threads to traverse a binary tree inorder without extra memory. Starting at the root, if the current node has no left child, it is visited and traversal moves to the right child. If there is a left child, the algorithm finds the rightmost node (predecessor) in the left subtree. If the predecessor's right pointer is null, a thread is created to the current node, and traversal moves to the left child. If the thread already exists, it is removed, the current node is visited, and traversal moves to the right child. This process repeats until all nodes are visited and the current pointer becomes null. The tree structure is restored by removing all threads. This method avoids using stack or recursion by temporarily modifying the tree.