-
Notifications
You must be signed in to change notification settings - Fork 927
/
avl-tree.js
90 lines (84 loc) · 2 KB
/
avl-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const BinarySearchTree = require('./binary-search-tree');
const {
leftRotation,
rightRotation,
leftRightRotation,
rightLeftRotation,
} = require('./tree-rotations');
// tag::balance[]
/**
* Balance tree doing rotations based on balance factor.
*
* Depending on the `node` balance factor and child's factor
* one of this rotation is performed:
* - LL rotations: single left rotation
* - RR rotations: single right rotation
* - LR rotations: double rotation left-right
* - RL rotations: double rotation right-left
*
* @param {BinaryTreeNode} node
*/
function balance(node) {
if (node.balanceFactor > 1) {
// left subtree is higher than right subtree
if (node.left.balanceFactor < 0) {
return leftRightRotation(node);
}
return rightRotation(node);
} if (node.balanceFactor < -1) {
// right subtree is higher than left subtree
if (node.right.balanceFactor > 0) {
return rightLeftRotation(node);
}
return leftRotation(node);
}
return node;
}
// end::balance[]
// tag::balanceUpstream[]
/**
* Bubbles up balancing nodes a their parents
*
* @param {BinaryTreeNode} node
*/
function balanceUpstream(node) {
let current = node;
let newParent;
while (current) {
newParent = balance(current);
current = current.parent;
}
return newParent;
}
// end::balanceUpstream[]
// tag::AvlTree[]
/**
* AVL Tree
* It's a self-balanced binary search tree optimized for fast lookups.
*/
class AvlTree extends BinarySearchTree {
/**
* Add node to tree. It self-balance itself.
* @param {any} value node's value
*/
add(value) {
const node = super.add(value);
this.root = balanceUpstream(node);
return node;
}
/**
* Remove node if it exists and re-balance tree
* @param {any} value
*/
remove(value) {
const node = super.find(value);
if (node) {
const found = super.remove(value);
this.root = balanceUpstream(node.parent);
return found;
}
return false;
}
}
// end::AvlTree[]
module.exports = AvlTree;