Created
January 5, 2020 05:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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