Published
Edited
Aug 29, 2020
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function insert(root, value)
{
// Always return the inserted node (or the found node if
// it is already there)
// root of (sub)tree is empty, create and return node
if(root === null)
return {value: value, left: null, right: null};
// Check to see if value found; no need to insert then
else if(root.value === value)
return root;
// Value not found; check left or right subtree
// If a new node is returned (the child was null),
// attach it appropriately
else if(value < root.value)
{
let node = insert(root.left, value);
if(root.left === null)
root.left = node;
return node;
}
else
{
let node = insert(root.right, value);
if(root.right === null)
root.right = node;
return node;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
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