UNPKG

i2bplustree

Version:

A package to implement the Improved Interval B+ tree, in TypeScript

126 lines (125 loc) 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const internal_1 = require("./internal"); /** * Class implementation of a Interval B+-Tree. * This is the class the user will be importing and using, so it works as an user interface for the IB+-tree * * Based on Article: https://pdfs.semanticscholar.org/1343/91e2537a8a9db15a1b5cce2ef66aa3352506.pdf * B+tree where each node is augmented with the same kind of information as in the binary Interval-trees. */ class IBplusTree { /** * IBplusTree Constructor. * Alpha is a parameter (0 < alpha < 1) which can be tuned to adjust space(hence update cost)/query_time tradeoff. * Higher values will lead to fewer splits and hence less storage expansion, but also to worse query efficiency. * Smaller values will lead to better query efficiency, but increase storage requirements and update costs (due to * reinsertion of split parts). * * @param order The tree's maximum number of children per node. * @param alpha If alpha is inferior to 0 time splits are not used, and otherwise they are (see consideration regarding * alpha above). */ constructor(order, alpha = 0) { this.alpha = alpha; if (order < 2) throw new Error('IB+ tree must have order >= 2'); if (alpha >= 1) throw new Error('Alpha can either be negative or a value belonging to ]0, 1['); this.root = new internal_1.IBplusInternalNode(order); } /** * Insert the given Interval in the tree * * @param int The interval to be inserted */ insert(int) { this.root.insert(int, this.alpha); while (!this.root.isRoot()) this.root = this.root.getParent(); } /** * Delete the given Interval from the tree * * @param int The Interval to be removed */ delete(int) { this.root.delete(int, this.alpha); if (this.root.isChildNewRoot()) // It is forcibly of type IBPlusInternalNode this.root = this.root.getChildren()[0]; } /** * Destroy all intervals stored in the tree that are fully * contained in the given bounds. * * @param lowerBound The range query lower bound * @param upperBound The range query upper bound */ rangeDelete(lowerBound, upperBound) { this.root.rangeDelete(lowerBound, upperBound); while (this.root.isChildNewRoot()) // It is forcibly of type IBPlusInternalNode this.root = this.root.getChildren()[0]; } /** * Verify if the given interval exists in the tree. * * @param interval the interval to be searched * @returns the object if exists, null otherwise. */ exists(interval) { return this.root.exists(interval); } /** * Get all intervals stored in the tree with equal bounds to the given ones. * * @param lowerBound The interval query lower bound * @param upperBound The interval query upper bound * @returns Array of found intervals. */ search(lowerBound, upperBound) { return this.root.search(new internal_1.FlatInterval(lowerBound, upperBound)); } /** * Gets the Interval with the lowest bound that intersects the * given interval. * * @param lowerBound The interval query lower bound * @param upperBound The interval query upper bound * @returns the FlatInterval that was found, null if none intersected. */ loneRangeSearch(lowerBound, upperBound) { return this.root.loneRangeSearch(new internal_1.FlatInterval(lowerBound, upperBound)); } /** * Find all intervals stored in the tree that intersect the given range. * * @param lowerBound The range query lower bound * @param upperBound The range query upper bound * @returns Set of intervals that intersect the range */ allRangeSearch(lowerBound, upperBound) { return this.root.allRangeSearch(new internal_1.FlatInterval(lowerBound, upperBound)); } /** * Find all intervals stored in the tree that are contained in the given range. * * @param lowerBound The range query lower bound * @param upperBound The range query upper bound * @returns Set of intervals that are contained in the range */ containedRangeSearch(lowerBound, upperBound) { return this.root.containedRangeSearch(new internal_1.FlatInterval(lowerBound, upperBound)); } /** * Represents the current tree as a string. * Useful for printing purposes. * * @returns the root as a string */ asString() { return this.root.asString(); } } exports.IBplusTree = IBplusTree;