Published
Edited
May 16, 2019
4 stars
Insert cell
Insert cell
Insert cell
Insert cell
tree = {
const stratify = d3.stratify()
.id(d => d.id)
.parentId(d => d.parentId)
const links = [
{ id: "a", parentId: "b" },
{ id: "b", parentId: "f" },
{ id: "c", parentId: "d" },
{ id: "d", parentId: "b" },
{ id: "e", parentId: "d" },
{ id: "f", parentId: null },
{ id: "g", parentId: "f" },
{ id: "i", parentId: "g" },
{ id: "h", parentId: "i" },
]
return stratify(links)
}
Insert cell
Insert cell
preorderTraverse = (rootNode,
visitFn = node => {},
childrenFn = node => node.children) => {
const stack = []
stack.push(rootNode)
while (stack.length) {
const node = stack.pop()
visitFn(node)
if (childrenFn(node)) {
stack.push(...childrenFn(node).reverse())
}
}
}
Insert cell
Insert cell
Insert cell
postorderTraverse = (rootNode,
visitFn = node => {},
childrenFn = node => node.children) => {
const stack = []
const nextStack = []
stack.push(rootNode)
while (stack.length) {
const node = stack.pop()
nextStack.push(node)
if (childrenFn(node)) {
stack.push(...childrenFn(node))
}
}
while (nextStack.length) {
const node = nextStack.pop()
visitFn(node)
}
}
Insert cell
Insert cell
Insert cell
Insert cell
levelorderTraverse = (rootNode,
visitFn = node => {},
childrenFn = node => node.children) => {
let stack = []
let nextStack = []
nextStack.push(rootNode)
while (true) {
stack = nextStack.reverse()
nextStack = []
while (stack.length) {
const node = stack.pop()
visitFn(node)
if (childrenFn(node)) {
nextStack.push(...childrenFn(node))
}
}
if (!nextStack.length) {
break
}
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more