UNPKG

maximum-matching

Version:

Implementation of Blossom's Algorithm for Maximum Matching

55 lines (54 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Tree = void 0; class Tree { constructor(p) { this.parent = p.parent; this.elem = p.elem; this.children = [...p.children]; } static withRoot(root) { return new Tree({ elem: root, children: [] }); } findSubtree(node) { if (node === this.elem) return this; for (const child of this.children) { const found = child.findSubtree(node); if (found) return found; } return undefined; } findSubtreeOrFail(node) { const found = this.findSubtree(node); if (!found) throw new Error(`Node ${node} was not found in the tree`); return found; } has(node) { return Boolean(this.findSubtree(node)); } addChild(node) { const child = new Tree({ parent: this, elem: node, children: [], }); this.children.push(child); return child; } pathToRoot(node) { let currentPointer = this.findSubtreeOrFail(node); const path = []; while (currentPointer) { path.push(currentPointer.elem); currentPointer = currentPointer.parent; } return path; } distanceToRoot(node) { return this.pathToRoot(node).length - 1; } } exports.Tree = Tree;