mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +08:00
Fix binary tree node.
This commit is contained in:
parent
30c080ba02
commit
9eefd13615
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user