import {TreeNode} from "./Node"; import {RedBlackTree} from "./RedBlackTree"; export class UpdateHandler { protected readonly tree: RedBlackTree; constructor(tree: RedBlackTree) { this.tree = tree; } rightRotate(subTreeRoot: TreeNode, pivot: TreeNode): 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, pivot: TreeNode): 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, node2: TreeNode): void { const node1Color = node1.getColor(); node1.setColor(node2.getColor()); node2.setColor(node1Color); } }