Skip to content

Instantly share code, notes, and snippets.

@JSerZANP
Created January 16, 2022 09:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JSerZANP/dd9ba9b7d5a3d2ef75f2ce8ffc78cab4 to your computer and use it in GitHub Desktop.
Save JSerZANP/dd9ba9b7d5a3d2ef75f2ce8ffc78cab4 to your computer and use it in GitHub Desktop.
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