feat: implement comparator

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-07 16:55:12 +02:00
parent e327524d91
commit 6d804155e1
Signed by: mfocko
GPG key ID: 7C47D46246790496
3 changed files with 156 additions and 0 deletions

62
comparator.html Executable file
View file

@ -0,0 +1,62 @@
<!DOCTYPE html>
<head>
<meta charset="UTF-8" />
<link
href="https://fonts.googleapis.com/css?family=Open+Sans:600"
rel="stylesheet"
/>
<link href="style.css" rel="stylesheet" />
</head>
<body>
<form id="insertTree" onSubmit="return insertCallback()">
<input
id="insertInput"
type="text"
placeholder="Number to insert"
maxlength="4"
size="14"
autofocus
/>
<input type="submit" value="Insert" />
</form>
<form id="deleteTree" onSubmit="return deleteCallback()">
<input
id="deleteInput"
type="text"
placeholder="Number to delete"
maxlength="4"
size="14"
/>
<input type="submit" value="Delete" />
</form>
<table>
<tr>
<td>
<div id="left" style="text-align: center"></div>
</td>
<td>
<div id="right" style="text-align: center"></div>
</td>
</tr>
</table>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js"></script>
<script src="https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js"></script>
<script src="utils.js"></script>
<script src="node.js"></script>
<script src="ranked_tree.js"></script>
<script src="avl.js"></script>
<script src="wavl.js"></script>
<script src="ravl.js"></script>
<script src="recorder.js"></script>
<script src="comparator_visualization.js"></script>
</body>

39
comparator.js Normal file
View file

@ -0,0 +1,39 @@
function nodesEquals(left, right, same) {
same = same ?? true;
if (!left || !right) {
return left === right;
}
return (
left.value === right.value &&
(!same || left.rank === right.rank) &&
nodesEquals(left.left, right.left, same) &&
nodesEquals(left.right, right.right, same)
);
}
class Comparator {
constructor(left, right) {
this.left = left;
this.right = right;
}
insert(value) {
this.left.insert(value);
this.right.insert(value);
}
delete(value) {
this.left.delete(value);
this.right.delete(value);
}
equals() {
return nodesEquals(this.left.root, this.right.root);
}
similar() {
return nodesEquals(this.left.root, this.right.root, false);
}
}

View file

@ -0,0 +1,55 @@
let lRecorder = new Recorder(
d3.select("#left").graphviz(),
"left"
).renderAtOnce();
let rRecorder = new Recorder(
d3.select("#right").graphviz(),
"right"
).renderAtOnce();
let left = new AVLTree();
let right = new WAVLTree();
left.recorder = lRecorder;
right.recorder = rRecorder;
function insertCallback() {
let number = document.getElementById("insertInput").value;
if (number === "") {
return false;
}
let value = parseInt(number);
left.insert(value);
right.insert(value);
document.getElementById("insertInput").value = "";
return false;
}
function deleteCallback() {
let number = document.getElementById("deleteInput").value;
if (number === "") {
return false;
}
let value = parseInt(number);
left.delete(value);
right.delete(value);
document.getElementById("deleteInput").value = "";
return false;
}
async function render() {
await Promise.all([
new Promise(() => lRecorder.render()),
new Promise(() => rRecorder.render()),
]);
// lRecorder.render();
// rRecorder.render();
setTimeout(render);
}
render();