-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
214 lines (177 loc) · 5.19 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class Order {
constructor(id, name, category, prepTime, timestamp) {
this.id = id;
this.name = name;
this.category = category;
this.prepTime = prepTime;
this.timestamp = timestamp;
this.originalPrepTime = prepTime;
}
compareTo(other) {
if (this.prepTime !== other.prepTime) {
return this.prepTime - other.prepTime;
}
return this.timestamp - other.timestamp;
}
}
class AVLNode {
constructor(order) {
this.order = order;
this.left = null;
this.right = null;
this.height = 1;
}
}
class AVLTree {
constructor() {
this.root = null;
}
height(node) {
return node ? node.height : 0;
}
rotateRight(y) {
const x = y.left;
const T2 = x.right;
x.right = y;
y.left = T2;
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;
}
rotateLeft(x) {
const y = x.right;
const T2 = y.left;
y.left = x;
x.right = T2;
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;
}
getBalance(node) {
return node ? this.height(node.left) - this.height(node.right) : 0;
}
insert(node, order) {
if (!node) return new AVLNode(order);
if (order.compareTo(node.order) < 0) {
node.left = this.insert(node.left, order);
} else {
node.right = this.insert(node.right, order);
}
node.height = 1 + Math.max(this.height(node.left), this.height(node.right));
return this.balance(node);
}
balance(node) {
const balanceFactor = this.getBalance(node);
if (balanceFactor > 1 && node.left && node.left.order.compareTo(node.order) > 0) {
return this.rotateRight(node);
}
if (balanceFactor < -1 && node.right && node.right.order.compareTo(node.order) < 0) {
return this.rotateLeft(node);
}
if (balanceFactor > 1 && node.left && node.left.order.compareTo(node.order) < 0) {
node.left = this.rotateLeft(node.left);
return this.rotateRight(node);
}
if (balanceFactor < -1 && node.right && node.right.order.compareTo(node.order) > 0) {
node.right = this.rotateRight(node.right);
return this.rotateLeft(node);
}
return node;
}
findMinNode(node) {
let current = node;
while (current.left !== null) current = current.left;
return current;
}
delete(node, order) {
if (!node) return node;
if (order.compareTo(node.order) < 0) {
node.left = this.delete(node.left, order);
} else if (order.compareTo(node.order) > 0) {
node.right = this.delete(node.right, order);
} else {
if (!node.left || !node.right) {
node = node.left || node.right;
} else {
const minLargerNode = this.findMinNode(node.right);
node.order = minLargerNode.order;
node.right = this.delete(node.right, minLargerNode.order);
}
}
if (!node) return node;
node.height = 1 + Math.max(this.height(node.left), this.height(node.right));
return this.balance(node);
}
inorderTraversal(node, orders = []) {
if (node) {
this.inorderTraversal(node.left, orders);
orders.push(node.order);
this.inorderTraversal(node.right, orders);
}
return orders;
}
}
const orderQueue = new AVLTree();
function generateUniqueID() {
return Math.floor(100 + Math.random() * 900);
}
function addOrder() {
const orderName = document.getElementById("orderName").value.trim();
const orderCategory = document.getElementById("orderCategory");
const preparationTime = parseInt(orderCategory.value, 10);
const categoryText = orderCategory.options[orderCategory.selectedIndex].text;
if (orderName === "") {
alert("Please enter an order name");
return;
}
const newOrder = new Order(
generateUniqueID(),
orderName,
categoryText,
preparationTime,
Date.now()
);
orderQueue.root = orderQueue.insert(orderQueue.root, newOrder);
updateQueue();
document.getElementById("orderName").value = "";
}
function dequeueOrder() {
if (!orderQueue.root) {
alert("No orders in the queue");
return;
}
const minOrderNode = orderQueue.findMinNode(orderQueue.root);
orderQueue.root = orderQueue.delete(orderQueue.root, minOrderNode.order);
updateQueue();
}
function clearQueue() {
orderQueue.root = null;
updateQueue();
}
function ageOrders() {
function ageNode(node) {
if (!node) return;
if (node.order.prepTime > 1) {
node.order.prepTime -= 1;
}
ageNode(node.left);
ageNode(node.right);
}
ageNode(orderQueue.root);
updateQueue();
}
setInterval(ageOrders, 15000);
function updateQueue() {
const orders = orderQueue.inorderTraversal(orderQueue.root);
const queueContainer = document.getElementById("orderQueue");
queueContainer.innerHTML = "";
orders.forEach((order) => {
const orderDiv = document.createElement("div");
orderDiv.className = "order-item";
orderDiv.innerHTML = `
<span>ID: ${order.id} - ${order.name} - <em>${order.category}</em></span>
<span class="order-priority">Prep Time: ${order.prepTime}</span>
`;
queueContainer.appendChild(orderDiv);
});
}