Public
Edited
Jan 23, 2021
3 forks
Importers
4 stars
Insert cell
Insert cell
Insert cell
class RootedTree {
constructor (data, ...descendants) {
this.data = data;
this.descendants = descendants;
}
*preorder () {
yield this;
for (let d of this.descendants) yield* d.preorder()
}
*postorder () {
for (let d of this.descendants) yield* d.postorder()
yield this;
}
}
Insert cell
Insert cell
RT = function (...args) { return new RootedTree (...args) };
Insert cell
Insert cell
rt1 = RT("A",RT("B",RT("C"),RT("D")),RT("E"),RT("F",RT("G"),RT("H",RT("I"),RT("J"),RT("K"))))
Insert cell
Insert cell
draw(rt1)
Insert cell
Insert cell
[...rt1.preorder()].map(node => node.data)
Insert cell
[...rt1.postorder()].map(node => node.data)
Insert cell
Insert cell
class RootedTreeBinary {
constructor (data,firstChild=null,nextSibling=null) {
[this.data,this.firstChild,this.nextSibling] = [data,firstChild,nextSibling]
}
*preorder () {
yield this;
let child = this.firstChild;
while (child != null) {
yield* child.preorder();
child = child.nextSibling;
}
}
*postorder () {
let child = this.firstChild;
while (child != null) {
yield* child.postorder();
child = child.nextSibling;
}
yield this;
}
}
Insert cell
Insert cell
RTB = (d,f,n) => new RootedTreeBinary (d,f,n)
Insert cell
Insert cell
rtb1 = RTB("A",RTB("B", RTB("C", null, RTB("D")), RTB("E",null,RTB("F",RTB("G",null,RTB("H",RTB("I",null,RTB("J",null,RTB("K")))))))))
Insert cell
[...rt1.postorder()].map(node => node.data)
Insert cell
Insert cell
draw(rtb1)
Insert cell
Insert cell
class BinaryTree {
constructor (entry,left=null,right=null) {
// Alternative to line below: Object.assign(this,{entry,left,right})
[this.entry,this.left,this.right] = [entry,left,right]
}
*inorder () {
if (this.left) yield* this.left.inorder()
yield this;
if (this.right) yield* this.right.inorder()
}
*preorder () {
yield this;
if (this.left) yield* this.left.preorder()
if (this.right) yield* this.right.preorder()
}
*postorder () {
if (this.left) yield* this.left.postorder()
if (this.right) yield* this.right.postorder()
yield this;
}
}
Insert cell
Insert cell
BT = (e,l,r) => new BinaryTree (e,l,r)
Insert cell
Insert cell
bt1 = BT("a",BT("b",BT("d", BT("g"),BT("h"))),BT("c",BT("e",null,BT("i")),BT("f")))
Insert cell
Insert cell
draw(bt1)
Insert cell
Insert cell
[...bt1.inorder()].map(node => node.entry)
Insert cell
[...bt1.preorder()].map(node => node.entry)
Insert cell
[...bt1.postorder()].map(node => node.entry)
Insert cell
Insert cell
Insert cell
class ThreadedBinaryTree {
constructor (entry, left=null, right=null) {
[this.entry,this.left,this.right] = [entry,left,right]
}
*inorder () {
if (this.left && !this.left.isThread) yield* this.left.to.inorder()
yield this;
if (this.right && !this.right.isThread) yield* this.right.to.inorder()
}
thread () {
let a = [...this.inorder()];
for (let i = 0; i < a.length; i++) {
if (i > 0 && !a[i].left) a[i].left = new Link(a[i-1], true);
if (i+1 < a.length && !a[i].right) a[i].right = new Link(a[i+1], true);
}
return this
}
inorderSuccessor () {
let u = this.right;
if (!u) return null;
if (u.isThread) return u.to;
while (u.to.left && !u.to.left.isThread) {
u = u.to.left
}
if (u) return u.to
return null
}
inorderPredecessor () {
}
}
Insert cell
Insert cell
L = (to) => to ? new Link(to) : null
Insert cell
TBT = (e,l,r) => new ThreadedBinaryTree (e, L(l), L(r))
Insert cell
Insert cell
tbt1 = TBT("a",TBT("b",TBT("d", TBT("g"),TBT("h"))),TBT("c",TBT("e",null,TBT("i")),TBT("f"))).thread()
Insert cell
[...tbt1.inorder()].map(x => x.entry)
Insert cell
tbt1.inorderSuccessor().inorderSuccessor().inorderSuccessor()
Insert cell
Insert cell
draw(tbt1)
Insert cell
Insert cell
class CompleteBinaryTree {
constructor (array, index = 0) {
[this.array,this.index] = [array,index];
}
get left () {
return new CompleteBinaryTree (this.array, this.index*2+1)
}
get right () {
return new CompleteBinaryTree (this.array, this.index*2+2)
}
get parent () {
return new CompleteBinaryTree (this.array, ~~((this.index-1)/2))
}
get entry () {
return this.array[this.index]
}
set entry (value) {
this.array [this.index] = value
}
*inOrder () {
if (this.entry != undefined) {
yield* this.left.inOrder();
yield this;
yield* this.right.inOrder()
}
}
}
Insert cell
Insert cell
cbt1 = new CompleteBinaryTree([0,1,2,3,4,5,6,7]);
Insert cell
Insert cell
[...cbt1.inOrder()].map(x => x.entry)
Insert cell
Insert cell
draw(cbt1)
Insert cell
{
let cbt = new CompleteBinaryTree([0,1,2,3,4,5,6,7]);
cbt.entry="root";
cbt.left.entry = "leftSubtree";
cbt.left.parent.entry="ROOT";
cbt.left.right.parent.entry = "Left";
return [...cbt.inOrder()].map(x => x.entry)
}
Insert cell
Insert cell
Insert cell
Insert cell
md`## Libraries`
Insert cell
md`### GraphViz`
Insert cell
dot = require("@observablehq/graphviz@0.2")
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