Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: apantzar/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: enhancement/type-checking-binary-tree-node
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Aug 21, 2024

  1. Copy the full SHA
    fc5e0d0 View commit details
Showing with 12 additions and 0 deletions.
  1. +12 −0 src/data-structures/tree/BinaryTreeNode.js
12 changes: 12 additions & 0 deletions src/data-structures/tree/BinaryTreeNode.js
Original file line number Diff line number Diff line change
@@ -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;
@@ -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;