-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathMorrisTraversal.js
More file actions
63 lines (57 loc) · 1.42 KB
/
MorrisTraversal.js
File metadata and controls
63 lines (57 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Morris Inorder Traversal
// Reference: https://www.geeksforgeeks.org/dsa/inorder-tree-traversal-without-recursion-and-without-stack/
/*
* Author: Saad Arqam
* Morris Inorder Traversal Algorithm implementation in JavaScript
*
* Morris Traversal is a tree traversal algorithm that allows
* inorder traversal without using recursion or a stack.
* It achieves O(1) extra space by temporarily modifying
* the tree structure (creating and removing "threads").
*
* Reference:
* https://en.wikipedia.org/wiki/Threaded_binary_tree#Morris_traversal
*/
// Node class
export class Node {
constructor(val) {
this.val = val
this.left = null
this.right = null
}
}
// Morris Inorder Traversal function
export function morrisTraversal(node) {
const result = []
let curr = node
while (curr !== null) {
if (curr.left === null) {
result.push(curr.val)
curr = curr.right
} else {
let predecessor = curr.left
while (predecessor.right !== null && predecessor.right !== curr) {
predecessor = predecessor.right
}
if (predecessor.right === null) {
predecessor.right = curr
curr = curr.left
} else {
predecessor.right = null
result.push(curr.val)
curr = curr.right
}
}
}
return result
}
// Example Tree:
// 7
// / \
// 5 8
// / \
// 3 6
// \
// 9
//
// Morris inorder traversal: [3, 5, 6, 9, 7, 8]