-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvlTree.js
143 lines (118 loc) · 3.56 KB
/
AvlTree.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const Node = require("../node/Node");
const BinarySearchTree = require("../binary-search-tree/BinarySearchTree");
class AvlNode extends Node {
constructor(val) {
super(val);
this.height = 0;
}
}
class AvlTree extends BinarySearchTree {
insert(val) {
if (typeof val === 'undefined') {
throw 'No parameter value was passed!';
}
this.root = this._insertRecursive(this.root, val);
}
remove(val) {
if (typeof val === 'undefined') {
throw 'No parameter value was passed!';
}
if (!this.find(val)) {
throw `The value ${val} does not exist in the tree!`;
}
this.root = this._removeRecursive(this.root, val);
}
_insertRecursive(node, val) {
if (!node) {
return new AvlNode(val);
}
if (val < node.val) {
node.left = this._insertRecursive(node.left, val);
} else if (val > node.val) {
node.right = this._insertRecursive(node.right, val);
} else {
throw 'Tree does not support duplicates!';
}
this._updateHeight(node);
return this._balance(node);
}
_updateHeight(node) {
return 1 + Math.max(
this._height(node.left),
this._height(node.right)
);
}
_balance(node) {
const balanceFactor = this._balanceFactor(node);
if (balanceFactor < -1) {
if (this._balanceFactor(node.right) > 0) {
node.right = this._rotateRight(node.right);
}
node = this._rotateLeft(node);
} else if (balanceFactor > 1) {
if (this._balanceFactor(node.left) < 0) {
node.left = this._rotateLeft(node.left);
}
node = this._rotateRight(node);
}
return node;
}
_balanceFactor(node) {
return this._height(node.left) - this._height(node.right);
}
_height(node) {
return node ? node.height : -1;
}
_rotateLeft(node) {
const rightChild = node.right;
node.right = rightChild.left;
rightChild.left = node;
this._updateHeight(node);
this._updateHeight(rightChild);
return rightChild;
}
_rotateRight(node) {
const leftChild = node.left;
node.left = leftChild.right;
leftChild.right = node;
this._updateHeight(node);
this._updateHeight(leftChild);
return leftChild;
}
_updateHeight(node) {
node.height = 1 + Math.max(this._height(node.left), this._height(node.right));
}
_removeRecursive(node, val) {
if (val < node.val) {
node.left = this._removeRecursive(node.left, val);
}
else if (val > node.val) {
node.right = this._removeRecursive(node.right, val);
}
else {
if (node.left == null) {
return node.right;
}
else if (node.right == null) {
return node.left;
}
else {
const temp = node;
node = this._findMinRecursive(temp.right);
node.right = this._deleteMin(temp.right);
node.left = temp.left;
}
}
this._updateHeight(node);
return this._balance(node);
}
_deleteMin(node) {
if (!node.left) {
return node.right;
}
node.left = this._deleteMin(node.left);
this._updateHeight(node);
return this._balance(node);
}
}
module.exports = AvlTree;