Skip to content

Instantly share code, notes, and snippets.

@cozzbie
Created January 5, 2020 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cozzbie/b0060560cdcb803c96a50fce07b1b01e to your computer and use it in GitHub Desktop.
Save cozzbie/b0060560cdcb803c96a50fce07b1b01e to your computer and use it in GitHub Desktop.
Given a binary tree and a key, insert the key into the binary tree at first position available
const insert = (tree, value) => {
const plug = [];
plug.push(tree);
while(plug.length){
const elem = plug.shift();
if(!elem.left) {
elem.left = node(value);
break;
}else{
plug.push(elem.left);
}
if(!elem.right){
elem.right = node(value);
break;
}else{
plug.push(elem.right);
}
}
}
insert(tree, 12);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment