Created
January 27, 2019 12:29
-
-
Save hk-skit/67c5b59acabb1052549c601c80d282b3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export class BinaryTreeNode { | |
constructor(value) { | |
this.value = value; | |
this.left = null; | |
this.right = null; | |
} | |
/** | |
* Inserts node in level order. | |
* | |
* @param {*} x | |
* @returns | |
* @memberof BinaryTreeNode | |
*/ | |
add(x) { | |
const queue = [this]; | |
const newNode = new BinaryTreeNode(x); | |
while (true) { | |
const node = queue.shift(); | |
if (node.left === null) { | |
node.left = newNode; | |
return this; | |
} | |
if (node.right === null) { | |
node.right = newNode; | |
return this; | |
} | |
queue.push(node.left); | |
queue.push(node.right); | |
} | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment