Published
Edited
Apr 28, 2022
Fork of AA Tree
2 forks
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
root = randoms.reduce((tree, v) => insert(v, tree), null)
Insert cell
Insert cell
Insert cell
function insert(value, root) {
if (root === null) return node(value);

if (value < root.value) {
root.left = insert(value, root.left);
} else if (value > root.value) {
root.right = insert(value, root.right);
} else {
// duplicate value
// do nothing
}

return root;
}
Insert cell
function remove(value, T) {
if (nil(T)) return T;

if (value < T.value) {
T.left = remove(value, T.left);
} else if (value > T.value) {
T.right = remove(value, T.right);
} else {
if (leaf(T)) return null;

if (nil(T.left)) {
const L = successor(T);
T.value = L.value;
T.right = remove(L.value, T.right);
} else {
const L = predecessor(T);
T.value = L.value;
T.left = remove(L.value, T.left);
}
}
return T;
}
Insert cell
Insert cell
function successor(T) {
let next = T.right;
while (!nil(next.left)) next = next.left;
return next;
}
Insert cell
function predecessor(T) {
let prev = T.left;
while (!nil(prev.right)) prev = prev.right;
return prev;
}
Insert cell
Insert cell
function leaf(node) {
return nil(node.left) && nil(node.right);
}
Insert cell
Insert cell
nil = function(node) {
return node === null;
}
Insert cell
Insert cell
node = value => new Node(value, null, null)
Insert cell
class Node {
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
}
Insert cell
Insert cell
Insert cell
data = convert(root)
Insert cell
Insert cell
import { chart, viewof options, style } with {
data
} from "@stardisblue/vertical-tree"
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