compound-binary-file-js
Version:
This is an implementation of [Compound Binary File v.3](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/53989ce4-7b05-4f8d-829b-d08d6148375b) \ Allows reading existing files, creation of the/write operation
55 lines (49 loc) • 1.88 kB
text/typescript
import {TreeNode} from "./Node";
import {RedBlackTree} from "./RedBlackTree";
export class UpdateHandler<T> {
protected readonly tree: RedBlackTree<T>;
constructor(tree: RedBlackTree<T>) {
this.tree = tree;
}
rightRotate(subTreeRoot: TreeNode<T>, pivot: TreeNode<T>): void {
const parent = subTreeRoot.getParent();
if(parent == null) {
subTreeRoot.setLeftChild(pivot.getRightChild());
pivot.setRightChild(subTreeRoot);
this.tree.setRoot(pivot);
} else {
const isLeftSubTree = parent.isLeftChild(subTreeRoot);
subTreeRoot.setLeftChild(pivot.getRightChild());
pivot.setRightChild(subTreeRoot);
if(isLeftSubTree) {
parent.setLeftChild(pivot);
} else {
parent.setRightChild(pivot);
}
}
this.swapColor(subTreeRoot, pivot);
}
leftRotate(subTreeRoot: TreeNode<T>, pivot: TreeNode<T>): void {
const parent = subTreeRoot.getParent();
if(parent == null) {
subTreeRoot.setRightChild(pivot.getLeftChild());
pivot.setLeftChild(subTreeRoot);
this.tree.setRoot(pivot);
} else {
const isLeftSubTree = parent.isLeftChild(subTreeRoot);
subTreeRoot.setRightChild(pivot.getLeftChild());
pivot.setLeftChild(subTreeRoot);
if(isLeftSubTree) {
parent.setLeftChild(pivot);
} else {
parent.setRightChild(pivot);
}
}
this.swapColor(subTreeRoot, pivot);
}
swapColor(node1: TreeNode<T>, node2: TreeNode<T>): void {
const node1Color = node1.getColor();
node1.setColor(node2.getColor());
node2.setColor(node1Color);
}
}