web/comparator.js
Matej Focko 6d804155e1
feat: implement comparator
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-05-07 16:55:12 +02:00

39 lines
818 B
JavaScript

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);
}
}