Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fiber tree traversal example
const root = {
val: 1
}
const node2 = {
val: 2,
returnNode: root
}
root.child = node2
const node3 = {
val: 3,
}
node2.child = node3
const node4 = {
val: 4
}
node3.sibling = node4
const node5 = {
val: 5,
returnNode: node2
}
node4.sibling = node5
const node6 = {
val: 6,
}
node4.child = node6
const node7 = {
val: 7,
returnNode: node4
}
node6.sibling = node7
const node8 = {
val: 8,
returnNode: node7
}
node7.child = node8
// -------------------------
let nextNode = root
function begin() {
while (nextNode) {
console.log('begin ', nextNode.val)
if (nextNode.child) {
nextNode = nextNode.child
} else {
complete()
}
}
}
function complete() {
while (nextNode) {
console.log('complete ', nextNode.val)
if (nextNode.sibling) {
nextNode = nextNode.sibling
// go to sibling and begin new
return
} else {
nextNode = nextNode.returnNode
}
}
}
begin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment