@everwhen/temporal
Version:
85 lines • 2.48 kB
JavaScript
import { Interval } from '../interval.js';
export class TemporalNode {
constructor(interval, data) {
Object.defineProperty(this, "interval", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "left", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "right", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "height", {
enumerable: true,
configurable: true,
writable: true,
value: 0
});
Object.defineProperty(this, "data", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
this.interval = Interval.from(interval);
this.left = null;
this.right = null;
this.data = data;
}
get balanceFactor() {
const left = this.left?.height ?? 0;
const right = this.right?.height ?? 0;
return left - right;
}
updateHeight() {
const left = this.left?.height ?? 0;
const right = this.right?.height ?? 0;
this.height = Math.max(left, right) + 1;
}
rotateRight() {
const newRoot = this.left;
const transferredNode = newRoot.right;
newRoot.right = this;
this.left = transferredNode;
this.updateHeight();
newRoot.updateHeight();
return newRoot;
}
rotateLeft() {
const newRoot = this.right;
const transferredNode = newRoot.left;
newRoot.left = this;
this.right = transferredNode;
this.updateHeight();
newRoot.updateHeight();
return newRoot;
}
balance() {
this.updateHeight();
const bf = this.balanceFactor;
if (bf > 1) {
if (this.left.balanceFactor < 0) {
this.left = this.left.rotateLeft();
}
return this.rotateRight();
}
if (bf < -1) {
if (this.right.balanceFactor > 0) {
this.right = this.right.rotateRight();
}
return this.rotateLeft();
}
return this;
}
}
//# sourceMappingURL=temporal-node.js.map