-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
199 lines (195 loc) · 6.65 KB
/
script.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
190
191
192
193
194
195
196
197
198
199
class AVLNode {
constructor(value) {
this.value = value;
this.height = 1;
this.left = null;
this.right = null;
}
}
class AVLTree {
/*constructor() {
this.root = null;
}*/
getHeight(node) {
return node ? node.height : 0;
}
getBF(node) {
return node ? this.getHeight(node.left) - this.getHeight(node.right) : 0;
}
rightRotate(y) {
const x = y.left;
const T2 = x.right;
x.right = y;
y.left = T2;
y.height = Math.max(this.getHeight(y.left), this.getHeight(y.right)) + 1;
x.height = Math.max(this.getHeight(x.left), this.getHeight(x.right)) + 1;
return x;
}
leftRotate(x) {
const y = x.right;
const T2 = y.left;
y.left = x;
x.right = T2;
x.height = Math.max(this.getHeight(x.left), this.getHeight(x.right)) + 1;
y.height = Math.max(this.getHeight(y.left), this.getHeight(y.right)) + 1;
return y;
}
insert(node, value) {
if (!node) {
console.log(`Inserting new node with value: ${value}`);
return new AVLNode(value);
}
if (value < node.value) {
node.left = this.insert(node.left, value);
} else if (value > node.value) {
node.right = this.insert(node.right, value);
} else {
return node; // Duplicate values are not allowed
}
node.height = Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1;
const balance = this.getBF(node);
// LL
if (balance > 1 && value < node.left.value) {
return this.rightRotate(node);
}
// RR
if (balance < -1 && value > node.right.value) {
return this.leftRotate(node);
}
// LR
if (balance > 1 && value > node.left.value) {
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
// RL
if (balance < -1 && value < node.right.value) {
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
return node;
}
delete(node, value) {
if (!node) {
console.log(`Value ${value} not found for deletion.`);
return node;
}
if (value < node.value) {
node.left = this.delete(node.left, value);
} else if (value > node.value) {
node.right = this.delete(node.right, value);
} else {
// Node with only one child or no child
if (!node.left || !node.right) {
const temp = node.left ? node.left : node.right;
return temp;
} else {
// Node with two children
const temp = this.getMinValueNode(node.right);
node.value = temp.value;
node.right = this.delete(node.right, temp.value);
}
}
// Update height of the current node
node.height = Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1;
const balance = this.getBF(node);
// Balancing the tree
// LL
if (balance > 1 && this.getBF(node.left) >= 0) {
return this.rightRotate(node);
}
// LR
if (balance > 1 && this.getBF(node.left) < 0) {
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
// RR
if (balance < -1 && this.getBF(node.right) <= 0) {
return this.leftRotate(node);
}
// RL
if (balance < -1 && this.getBF(node.right) > 0) {
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
return node;
}
getMinValueNode(node) {
let current = node;
while (current.left) {
current = current.left;
}
return current;
}
add(value) {
this.root = this.insert(this.root, value);
this.refreshTree();
}
remove(value) {
this.root = this.delete(this.root, value);
this.refreshTree();
}
visualizeTree(node, container) {
if (!node) return;
// Node creation
const div = document.createElement('div');
div.className = "node";
div.innerHTML = node.value;
container.appendChild(div);
if (node.left || node.right) {
const children = document.createElement('div');
children.className = "children";
container.appendChild(children);
if (node.left) {
const left = document.createElement('div');
left.className = "child left";
children.appendChild(left);
this.visualizeTree(node.left, left);
const link = document.createElement('div');
link.className = "link left";
left.appendChild(link);
}
if (node.right) {
const right = document.createElement('div');
right.className = "child right";
children.appendChild(right);
this.visualizeTree(node.right, right);
const link = document.createElement('div');
link.className = "link right";
right.appendChild(link);
}
}
}
refreshTree() {
const treeContainer = document.getElementById('treeContainer');
treeContainer.innerHTML = '';
const tree = document.createElement('div');
tree.className = "tree";
treeContainer.appendChild(tree);
this.visualizeTree(this.root, tree);
}
}
const tree = new AVLTree()
const inputField = document.getElementById("valueInput");
const addButton = document.getElementById("addButton");
const deleteButton = document.getElementById("deleteButton");
const treeContainer = document.getElementById("treeContainer");
addButton.addEventListener("click", () => {
const value = parseInt(inputField.value);
if (!isNaN(value)) {
console.log('Inserting value:', value);
tree.add(value);
} else {
console.log("Invalid input");
}
inputField.value = "";
});
deleteButton.addEventListener("click", () => {
const value = parseInt(inputField.value);
if (!isNaN(value)) {
console.log('Deleting value:', value);
tree.remove(value);
} else {
console.log("Invalid input");
}
inputField.value = "";
});