index.d.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Node from './node';
  2. import { Key } from './types';
  3. export declare type Comparator<Key> = (a: Key, b: Key) => number;
  4. export declare type Visitor<Key, Value> = (node: Node<Key, Value>) => void;
  5. export declare type NodePrinter<Key, Value> = (node: Node<Key, Value>) => string;
  6. declare function DEFAULT_COMPARE(a: Key, b: Key): number;
  7. export default class Tree<Key = number, Value = any> {
  8. private _comparator;
  9. private _root;
  10. private _size;
  11. constructor(comparator?: typeof DEFAULT_COMPARE);
  12. /**
  13. * Inserts a key, allows duplicates
  14. */
  15. insert(key: Key, data?: Value): Node<Key, Value>;
  16. /**
  17. * Adds a key, if it is not present in the tree
  18. */
  19. add(key: Key, data?: Value): Node<Key, Value>;
  20. /**
  21. * @param {Key} key
  22. * @return {Node|null}
  23. */
  24. remove(key: Key): void;
  25. /**
  26. * Deletes i from the tree if it's there
  27. */
  28. private _remove;
  29. /**
  30. * Removes and returns the node with smallest key
  31. */
  32. pop(): {
  33. key: Key;
  34. data: Value;
  35. } | null;
  36. /**
  37. * Find without splaying
  38. */
  39. findStatic(key: Key): Node<Key, Value> | null;
  40. find(key: Key): Node<Key, Value> | null;
  41. contains(key: Key): boolean;
  42. forEach(visitor: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
  43. /**
  44. * Walk key range from `low` to `high`. Stops if `fn` returns a value.
  45. */
  46. range(low: Key, high: Key, fn: Visitor<Key, Value>, ctx?: any): Tree<Key, Value>;
  47. /**
  48. * Returns array of keys
  49. */
  50. keys(): Key[];
  51. /**
  52. * Returns array of all the data in the nodes
  53. */
  54. values(): Value[];
  55. min(): Key | null;
  56. max(): Key | null;
  57. minNode(t?: Node<Key, Value>): Node<Key, Value> | null;
  58. maxNode(t?: Node<Key, Value>): Node<Key, Value> | null;
  59. /**
  60. * Returns node at given index
  61. */
  62. at(index: number): Node<Key, Value> | null;
  63. next(d: Node<Key, Value>): Node<Key, Value> | null;
  64. prev(d: Node<Key, Value>): Node<Key, Value> | null;
  65. clear(): Tree<Key, Value>;
  66. toList(): Node<any, any>;
  67. /**
  68. * Bulk-load items. Both array have to be same size
  69. */
  70. load(keys: Key[], values?: Value[], presort?: boolean): this;
  71. isEmpty(): boolean;
  72. readonly size: number;
  73. readonly root: Node<Key, Value> | null;
  74. toString(printNode?: NodePrinter<Key, Value>): string;
  75. update(key: Key, newKey: Key, newData?: Value): void;
  76. split(key: Key): {
  77. left: Node<any, any>;
  78. right: Node<any, any>;
  79. };
  80. [Symbol.iterator](): IterableIterator<Node<Key, Value>>;
  81. }
  82. export {};