-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl-tree.js
189 lines (167 loc) · 4.92 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class Node {
constructor(item) {
this.item = item;
this.height = 1;
this.left = null;
this.right = null;
}
}
class AVLTree {
constructor() {
this.root = null;
}
height(node) {
return (node === null) ? 0 : node.height;
}
rightRotate(y) {
let x = y.left;
let xRightChild = x.right;
x.right = y;
y.left = xRightChild;
y.height = Math.max(this.height(y.left), this.height(y.right)) + 1;
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
return x;
}
leftRotate(x) {
let y = x.right;
let yLeftChild = y.left;
y.left = x;
x.right = yLeftChild;
x.height = Math.max(this.height(x.left), this.height(x.right)) + 1;
y.height = Math.max(this.height(y.left), this.height(y.right)) + 1;
return y;
}
getBalanceFactor(node) {
return (node == null) ? 0 : this.height(node.left) - this.height(node.right);
}
insertNodeHelper(node, item) {
if (node === null) {
return new Node(item);
}
if (item < node.item) {
node.left = this.insertNodeHelper(node.left, item);
}
else if (item > node.item) {
node.right = this.insertNodeHelper(node.right, item);
}
else {
return node;
}
node.height = 1 + Math.max(this.height(node.left), this.height(node.right));
let balanceFactor = this.getBalanceFactor(node);
if (balanceFactor > 1) {
if (item < node.left.item) {
return this.rightRotate(node);
}
else if (item > node.left.item) {
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
}
if (balanceFactor < -1) {
if (item > node.right.item) {
return this.leftRotate(node);
}
else if (item < node.right.item) {
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
}
return node;
}
insertNode(item) {
this.root = this.insertNodeHelper(this.root, item);
}
nodeWithMinimumValue(node) {
let current = node;
while (current.left !== null) {
current = current.left;
}
return current;
}
deleteNodeHelper(root, item) {
if (root == null) {
return root;
}
if (item < root.item) {
root.left = this.deleteNodeHelper(root.left, item);
}
else if (item > root.item) {
root.right = this.deleteNodeHelper(root.right, item);
}
else {
if ((root.left === null) || (root.right === null)) {
let temp = null;
if (temp == root.left) {
temp = root.right;
}
else {
temp = root.left;
}
if (temp == null) {
temp = root;
root = null;
}
else {
root = temp;
}
}
else {
let temp = this.nodeWithMinimumValue(root.right);
root.item = temp.item;
root.right = this.deleteNodeHelper(root.right, temp.item);
}
}
if (root == null) {
return root;
}
root.height = Math.max(this.height(root.left), this.height(root.right)) + 1;
let balanceFactor = this.getBalanceFactor(root);
if (balanceFactor > 1) {
if (this.getBalanceFactor(root.left) >= 0) {
return this.rightRotate(root);
}
else {
root.left = this.leftRotate(root.left);
return this.rightRotate(root);
}
}
if (balanceFactor < -1) {
if (this.getBalanceFactor(root.right) <= 0) {
return this.leftRotate(root);
}
else {
root.right = this.rightRotate(root.right);
return this.leftRotate(root);
}
}
return root;
}
deleteNode(item) {
this.root = this.deleteNodeHelper(this.root, item);
}
preOrder() {
return this.preOrderHelper(this.root, []);
}
preOrderHelper(node, arr = []) {
if (node) {
arr.push(node.item);
this.preOrderHelper(node.left, arr);
this.preOrderHelper(node.right, arr);
}
return arr
}
}
let tree = new AVLTree();
tree.insertNode(33);
tree.insertNode(13);
tree.insertNode(53);
tree.insertNode(9);
tree.insertNode(21);
tree.insertNode(61);
tree.insertNode(8);
tree.insertNode(11);
console.log(tree.preOrder());
tree.deleteNode(13);
console.log("After Deletion: ");
console.log(tree.preOrder());