Skip to content

Commit

Permalink
Fix binary tree node.
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb committed Apr 6, 2018
1 parent 30c080b commit 9eefd13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/data-structures/tree/BinaryTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ export default class BinaryTreeNode {

setLeft(node) {
this.left = node;
this.left.parent = this;
if (node) {
this.left.parent = this;
}
return this;
}

setRight(node) {
this.right = node;
this.right.parent = this;
if (node) {
this.right.parent = this;
}
return this;
}

Expand Down
18 changes: 18 additions & 0 deletions src/data-structures/tree/__test__/BinaryTreeNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,22 @@ describe('BinaryTreeNode', () => {
expect(right.height).toBe(0);
expect(root.balanceFactor).toBe(-1);
});

it('should set null for left and right node', () => {
const root = new BinaryTreeNode(2);
const left = new BinaryTreeNode(1);
const right = new BinaryTreeNode(3);

root.setLeft(left);
root.setRight(right);

expect(root.left.value).toBe(1);
expect(root.right.value).toBe(3);

root.setLeft(null);
root.setRight(null);

expect(root.left).toBeNull();
expect(root.right).toBeNull();
});
});

0 comments on commit 9eefd13

Please sign in to comment.