2022-04-27 21:37:58 +02:00
|
|
|
class Queue {
|
|
|
|
constructor() {
|
|
|
|
this.q = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueue(x) {
|
|
|
|
this.q.push(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
dequeue() {
|
|
|
|
return this.q.shift();
|
|
|
|
}
|
|
|
|
|
|
|
|
isEmpty() {
|
|
|
|
return this.q.length == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RankedTree {
|
|
|
|
constructor() {
|
|
|
|
this.root = null;
|
2022-05-01 17:10:24 +02:00
|
|
|
this.recorder = null;
|
2022-04-27 21:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getUnwrappedGraph() {
|
2022-05-01 17:10:24 +02:00
|
|
|
let result = [];
|
2022-04-27 21:37:58 +02:00
|
|
|
|
|
|
|
let q = new Queue();
|
|
|
|
q.enqueue(this.root);
|
|
|
|
|
|
|
|
let edges = [];
|
|
|
|
while (!q.isEmpty()) {
|
|
|
|
let node = q.dequeue();
|
|
|
|
if (!q) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-05 20:28:58 +02:00
|
|
|
let [value, rank] = [node.value, node.rank];
|
|
|
|
result.push(`\t"Node(${value})" [label="${value}, ${rank}"];`);
|
2022-04-27 21:37:58 +02:00
|
|
|
|
2022-05-01 17:10:24 +02:00
|
|
|
[node.left, node.right]
|
|
|
|
.filter((child) => child)
|
|
|
|
.forEach((child) => {
|
|
|
|
edges.push([node, child]);
|
|
|
|
q.enqueue(child);
|
|
|
|
});
|
2022-04-27 21:37:58 +02:00
|
|
|
}
|
|
|
|
|
2022-05-01 17:10:24 +02:00
|
|
|
result.push("");
|
|
|
|
|
|
|
|
edges.forEach((vertices) => {
|
2022-04-27 21:37:58 +02:00
|
|
|
let [u, v] = vertices;
|
2022-05-01 17:10:24 +02:00
|
|
|
result.push(
|
2022-05-05 20:28:58 +02:00
|
|
|
`\t"Node(${u.value})" -> "Node(${
|
|
|
|
v.value
|
|
|
|
})" [label="${nodeDifference(v)}"]`
|
2022-05-01 17:10:24 +02:00
|
|
|
);
|
2022-04-27 21:37:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:10:24 +02:00
|
|
|
toDot() {
|
|
|
|
return ["digraph {", ...this._getUnwrappedGraph(), "}"];
|
|
|
|
}
|
|
|
|
|
2022-04-27 21:37:58 +02:00
|
|
|
toString() {
|
2022-05-01 17:10:24 +02:00
|
|
|
return this.toDot().join("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
record() {
|
|
|
|
if (!this.recorder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.recorder.record(this);
|
2022-04-27 21:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rank() {
|
|
|
|
return nodeRank(this.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
isCorrect() {
|
|
|
|
return this.isCorrectNode(this.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
search(value, node) {
|
|
|
|
return nodeSearch(value, node || this.root);
|
|
|
|
}
|
|
|
|
|
|
|
|
insert(value) {
|
|
|
|
let insertedNode = new Node(value);
|
|
|
|
|
|
|
|
if (!this.root) {
|
|
|
|
this.root = insertedNode;
|
2022-05-01 17:10:24 +02:00
|
|
|
this.record();
|
2022-04-27 21:37:58 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let parent = findParentNode(value, this.root);
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
insertedNode.parent = parent;
|
|
|
|
|
|
|
|
if (value < parent.value) {
|
|
|
|
parent.left = insertedNode;
|
|
|
|
} else {
|
|
|
|
parent.right = insertedNode;
|
|
|
|
}
|
2022-05-01 17:10:24 +02:00
|
|
|
this.record();
|
2022-04-27 21:37:58 +02:00
|
|
|
|
|
|
|
this.insertRebalance(insertedNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
transplant(u, v) {
|
|
|
|
if (!u.parent) {
|
|
|
|
this.root = v;
|
|
|
|
} else if (u.parent.left === u) {
|
|
|
|
u.parent.left = v;
|
|
|
|
} else {
|
|
|
|
u.parent.right = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v) {
|
|
|
|
v.rank = u.rank;
|
|
|
|
v.parent = u.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteNode(node) {
|
|
|
|
if (!node) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let [y, parent] = [null, node.parent];
|
|
|
|
|
|
|
|
if (!node.left) {
|
|
|
|
y = node.right;
|
|
|
|
this.transplant(node, node.right);
|
|
|
|
} else if (!node.right) {
|
|
|
|
y = node.left;
|
|
|
|
this.transplant(node, node.left);
|
|
|
|
} else {
|
|
|
|
let successor = nodeMinimum(node.right);
|
2022-05-01 17:09:46 +02:00
|
|
|
parent = successor.parent != node ? successor.parent : successor;
|
2022-04-27 21:37:58 +02:00
|
|
|
|
|
|
|
if (successor.parent != node) {
|
2022-05-01 17:09:46 +02:00
|
|
|
parent = successor.right ? successor.right : successor.parent;
|
2022-04-27 21:37:58 +02:00
|
|
|
this.transplant(successor, successor.right);
|
|
|
|
successor.right = node.right;
|
|
|
|
successor.right.parent = successor;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.transplant(node, successor);
|
|
|
|
successor.left = node.left;
|
|
|
|
node.left.parent = successor;
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:10:24 +02:00
|
|
|
this.record();
|
|
|
|
|
2022-04-27 21:37:58 +02:00
|
|
|
return [y, parent];
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(value) {
|
|
|
|
let node = this.root;
|
|
|
|
while (node && node.value != value) {
|
2022-05-01 17:09:46 +02:00
|
|
|
node = value < node.value ? node.left : node.right;
|
2022-04-27 21:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let toRebalance = this.deleteNode(node);
|
|
|
|
if (toRebalance) {
|
|
|
|
let [y, parent] = toRebalance;
|
|
|
|
this.deleteRebalance(y, parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* abstract methods */
|
|
|
|
isCorrectNode(node) {
|
|
|
|
throw "not implemented!";
|
|
|
|
}
|
|
|
|
|
|
|
|
insertRebalance(node) {
|
|
|
|
throw "not implemented!";
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteRebalance(node, parent) {
|
|
|
|
throw "not implemented!";
|
|
|
|
}
|
2022-05-01 17:09:46 +02:00
|
|
|
}
|