Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance BinaryTreeNode: Add type checking for child nodes #1163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/data-structures/tree/BinaryTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ export default class BinaryTreeNode {
* @return {BinaryTreeNode}
*/
setLeft(node) {

// Check if it is a proper node.
if (node && !(node instanceof BinaryTreeNode)) {
throw new Error('The left node must be an instance of BinaryTreeNode');
}

// Reset parent for left node since it is going to be detached.
if (this.left) {
this.left.parent = null;
Expand All @@ -121,6 +127,12 @@ export default class BinaryTreeNode {
* @return {BinaryTreeNode}
*/
setRight(node) {

// Check if it is a proper node.
if (node && !(node instanceof BinaryTreeNode)) {
throw new Error('The right node must be an instance of BinaryTreeNode');
}

// Reset parent for right node since it is going to be detached.
if (this.right) {
this.right.parent = null;
Expand Down