Skip to content
Open
Show file tree
Hide file tree
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
70 changes: 59 additions & 11 deletions src/data-structures/tree/BinaryTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default class BinaryTreeNode {
this.right = null;
this.parent = null;
this.value = value;
this.heightValue = 0;

// Any node related meta information may be stored here.
this.meta = new HashTable();
Expand All @@ -26,7 +27,7 @@ export default class BinaryTreeNode {
return 0;
}

return this.left.height + 1;
return this.left.heightValue + 1;
}

/**
Expand All @@ -37,14 +38,14 @@ export default class BinaryTreeNode {
return 0;
}

return this.right.height + 1;
return this.right.heightValue + 1;
}

/**
* @return {number}
*/
get height() {
return Math.max(this.leftHeight, this.rightHeight);
return this.heightValue;
}

/**
Expand Down Expand Up @@ -100,8 +101,8 @@ export default class BinaryTreeNode {
* @return {BinaryTreeNode}
*/
setLeft(node) {
// Reset parent for left node since it is going to be detached.
if (this.left) {
// Preserve the new parent if a rotation already moved this child.
if (this.left && this.left.parent === this) {
this.left.parent = null;
}

Expand All @@ -113,6 +114,8 @@ export default class BinaryTreeNode {
this.left.parent = this;
}

this.updateHeight();

return this;
}

Expand All @@ -121,8 +124,8 @@ export default class BinaryTreeNode {
* @return {BinaryTreeNode}
*/
setRight(node) {
// Reset parent for right node since it is going to be detached.
if (this.right) {
// Preserve the new parent if a rotation already moved this child.
if (this.right && this.right.parent === this) {
this.right.parent = null;
}

Expand All @@ -134,6 +137,51 @@ export default class BinaryTreeNode {
this.right.parent = this;
}

this.updateHeight();

return this;
}

/**
* Update this node's cached height and propagate the change to its ancestors.
*
* @return {BinaryTreeNode}
*/
updateHeight() {
let currentNode = this;
const updatedNodes = new Set();

while (currentNode && !updatedNodes.has(currentNode)) {
updatedNodes.add(currentNode);

const newHeight = Math.max(currentNode.leftHeight, currentNode.rightHeight);
if (currentNode.heightValue === newHeight) {
break;
}

currentNode.heightValue = newHeight;
currentNode = currentNode.parent;
}

return this;
}

/**
* Recalculate cached heights for this subtree from the leaves upward.
*
* @return {BinaryTreeNode}
*/
refreshHeight() {
if (this.left) {
this.left.refreshHeight();
}

if (this.right) {
this.right.refreshHeight();
}

this.heightValue = Math.max(this.leftHeight, this.rightHeight);

return this;
}

Expand All @@ -143,12 +191,12 @@ export default class BinaryTreeNode {
*/
removeChild(nodeToRemove) {
if (this.left && this.nodeComparator.equal(this.left, nodeToRemove)) {
this.left = null;
this.setLeft(null);
return true;
}

if (this.right && this.nodeComparator.equal(this.right, nodeToRemove)) {
this.right = null;
this.setRight(null);
return true;
}

Expand All @@ -166,12 +214,12 @@ export default class BinaryTreeNode {
}

if (this.left && this.nodeComparator.equal(this.left, nodeToReplace)) {
this.left = replacementNode;
this.setLeft(replacementNode);
return true;
}

if (this.right && this.nodeComparator.equal(this.right, nodeToReplace)) {
this.right = replacementNode;
this.setRight(replacementNode);
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/data-structures/tree/__test__/BinaryTreeNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ describe('BinaryTreeNode', () => {
.setRight(rightNode);

expect(rootNode.traverseInOrder()).toEqual([1, 2, 3]);
expect(rootNode.height).toBe(1);

expect(rootNode.removeChild(rootNode.left)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([2, 3]);
expect(rootNode.height).toBe(1);

expect(rootNode.removeChild(rootNode.right)).toBe(true);
expect(rootNode.traverseInOrder()).toEqual([2]);
expect(rootNode.height).toBe(0);

expect(rootNode.removeChild(rootNode.right)).toBe(false);
expect(rootNode.traverseInOrder()).toEqual([2]);
Expand Down
3 changes: 3 additions & 0 deletions src/data-structures/tree/avl-tree/AvlTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export default class AvlTree extends BinarySearchTree {
// Do standard BST removal.
super.remove(value);

// Refresh cached heights after the BST value-copy removal path.
this.root.refreshHeight();

// Balance the tree starting from the root node.
this.balance(this.root);
}
Expand Down
10 changes: 10 additions & 0 deletions src/data-structures/tree/avl-tree/__test__/AvlTRee.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import AvlTree from '../AvlTree';

describe('AvlTree', () => {
it.each([
['left', 1],
['right', -1],
])('should update height after inserting into a rotated %s subtree', (side, sign) => {
const tree = new AvlTree();
[1, 24, 7, 28, 71, 8, 88, 74, 19].forEach((value) => tree.insert(sign * value));

expect(tree.root[side].height).toBe(2);
});

it('should do simple left-left rotation', () => {
const tree = new AvlTree();

Expand Down