0
0
DSA Typescriptprogramming~10 mins

Morris Traversal Inorder Without Stack in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to move to the left child in Morris Traversal.

DSA Typescript
while (current !== null) {
  if (current.left === null) {
    console.log(current.data);
    current = current[1];
  } else {
    // Find the inorder predecessor
  }
}
Drag options to blanks, or click blank then click option'
A.left
B.parent
C.right
D.next
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right child when left is null.
Trying to access a non-existent parent property.
2fill in blank
medium

Complete the code to find the inorder predecessor of the current node.

DSA Typescript
let predecessor = current.left;
while (predecessor.right !== null && predecessor.right !== current) {
  predecessor = predecessor[1];
}
Drag options to blanks, or click blank then click option'
A.next
B.right
C.parent
D.left
Attempts:
3 left
💡 Hint
Common Mistakes
Moving to left child instead of right child when searching for predecessor.
Not checking if predecessor.right equals current to avoid infinite loop.
3fill in blank
hard

Fix the error in the code that creates a temporary link to the current node.

DSA Typescript
if (predecessor.right === null) {
  predecessor[1] = current;
  current = current.left;
}
Drag options to blanks, or click blank then click option'
A.next
B.left
C.parent
D.right
Attempts:
3 left
💡 Hint
Common Mistakes
Setting predecessor.left instead of predecessor.right.
Assigning current.left instead of current.
4fill in blank
hard

Fill both blanks to remove the temporary link and move to the right child.

DSA Typescript
else {
  predecessor[1] = null;
  console.log(current.data);
  current = current[2];
}
Drag options to blanks, or click blank then click option'
A.right
B.left
C.parent
D.next
Attempts:
3 left
💡 Hint
Common Mistakes
Removing predecessor.left instead of predecessor.right.
Moving to current.left instead of current.right after removing link.
5fill in blank
hard

Fill all three blanks to complete the Morris Traversal loop.

DSA Typescript
while (current !== null) {
  if (current.left === null) {
    console.log(current.data);
    current = current[1];
  } else {
    let predecessor = current.left;
    while (predecessor.right !== null && predecessor.right !== current) {
      predecessor = predecessor[2];
    }
    if (predecessor.right === null) {
      predecessor[3] = current;
      current = current.left;
    } else {
      predecessor.right = null;
      console.log(current.data);
      current = current.right;
    }
  }
}
Drag options to blanks, or click blank then click option'
A.right
B.left
C.parent
D.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using .left instead of .right in any of the blanks.
Setting predecessor.left instead of predecessor.right for the temporary link.