0
0
DSA C++programming~10 mins

Morris Traversal Inorder Without Stack in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Morris Traversal Inorder Without Stack
Start at root
Check if current.left is NULL?
YesPrint current.data
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
Traverse the tree without stack or recursion by creating temporary threads to predecessors, printing nodes in inorder.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

void morrisInorder(Node* root) {
  Node* current = root;
  while (current != NULL) {
    if (current->left == NULL) {
      std::cout << current->data << " ";
      current = current->right;
    } else {
      Node* 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;
        std::cout << current->data << " ";
        current = current->right;
      }
    }
  }
}
This code prints the inorder traversal of a binary tree without using stack or recursion by temporarily modifying the tree.
Execution Table
StepOperationCurrent NodePredecessor NodeThread Created/RemovedPrinted NodeVisual State
1Start at root1NULLNoneNone1 / \ 2 3
2current->left != NULL, find predecessor12NoneNone1 / \ 2 3
3predecessor->right == NULL, create thread12Thread 2->right = 1None1 / \ 2->1 3 (thread from 2 to 1)
4Move current to left child2NULLNoneNone1 / \ 2->1 3 (thread)
5current->left == NULL, print current2NULLNone21 / \ 2->1 3 (thread)
6Move current to right child (thread)12NoneNone1 / \ 2->1 3 (thread)
7current->left != NULL, find predecessor12NoneNone1 / \ 2->1 3 (thread)
8predecessor->right == current, remove thread12Remove thread 2->rightNone1 / \ 2 3
9Print current node1NULLNone11 / \ 2 3
10Move current to right child3NULLNoneNone1 / \ 2 3
11current->left == NULL, print current3NULLNone31 / \ 2 3
12Move current to right child (NULL)NULLNULLNoneNoneTraversal complete
13ExitNULLNULLNoneNoneTraversal complete
💡 current becomes NULL, traversal ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6After Step 8After Step 9After Step 10After Step 11Final
current112211133NULL
predecessorNULL2NULLNULLNULL2NULLNULLNULLNULL
threadNoneCreated 2->right=1NoneNoneNoneRemoved 2->rightNoneNoneNoneNone
printedNoneNoneNone2NoneNone1None3None
Key Moments - 3 Insights
Why do we create a thread from predecessor.right to current?
Creating the thread allows us to return to the current node after finishing the left subtree without using a stack. See Step 3 where thread 2->right=1 is created.
Why do we remove the thread after visiting the left subtree?
Removing the thread restores the original tree structure and prevents infinite loops. This happens at Step 8 where thread 2->right is removed.
Why do we print the node only when current.left is NULL or after removing the thread?
Printing at these points ensures inorder sequence: left subtree first, then node, then right subtree. See Steps 5, 9, and 11 for printing moments.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 5, which node is printed?
ANode 1
BNode 2
CNode 3
DNo node printed
💡 Hint
Check the 'Printed Node' column at Step 5 in the execution_table.
At which step is the thread from node 2 to node 1 removed?
AStep 3
BStep 6
CStep 8
DStep 10
💡 Hint
Look at the 'Thread Created/Removed' column for removal events.
If the tree had no left children, how would the printed nodes appear in the execution table?
APrinted nodes only when threads are removed
BPrinted nodes immediately and no threads created
CNo nodes printed
DThreads created but no nodes printed
💡 Hint
Refer to the logic when current->left == NULL in the code and execution_table Steps 5 and 11.
Concept Snapshot
Morris Traversal Inorder Without Stack:
- Uses threaded binary tree concept
- For each node, find predecessor in left subtree
- Create thread from predecessor.right to current if NULL
- If thread exists, remove it and print current
- If no left child, print current and move right
- Traverses inorder without extra space
Full Transcript
Morris Traversal is a way to do inorder traversal of a binary tree without using stack or recursion. It works by temporarily changing the tree structure. Starting at the root, if the current node has no left child, we print it and move to the right child. If it has a left child, we find its inorder predecessor in the left subtree. If the predecessor's right pointer is NULL, we create a thread to the current node and move current to its left child. If the thread already exists, we remove it, print the current node, and move to the right child. This process repeats until current becomes NULL, completing the traversal. This method uses O(1) extra space and restores the tree after traversal.