data-structure-typed 1.37.3 → 1.37.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +141 -43
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +230 -42
- package/dist/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/dist/data-structures/binary-tree/binary-tree.js +257 -170
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +104 -59
- package/dist/data-structures/binary-tree/bst.js +105 -60
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/{lib/types/data-structures → dist/types/data-structures/binary-tree}/avl-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree.js.map +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js.map +1 -0
- package/dist/types/data-structures/{binary-tree.d.ts → binary-tree/binary-tree.d.ts} +1 -5
- package/dist/types/data-structures/binary-tree/binary-tree.js.map +1 -0
- package/dist/types/data-structures/{bst.d.ts → binary-tree/bst.d.ts} +1 -1
- package/dist/types/data-structures/binary-tree/bst.js.map +1 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +23 -0
- package/dist/types/data-structures/binary-tree/index.js.map +1 -0
- package/dist/types/data-structures/{rb-tree.d.ts → binary-tree/rb-tree.d.ts} +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.js.map +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js.map +1 -0
- package/dist/types/data-structures/{tree-multiset.d.ts → binary-tree/tree-multiset.d.ts} +1 -1
- package/dist/types/data-structures/binary-tree/tree-multiset.js.map +1 -0
- package/dist/types/data-structures/graph/abstract-graph.js.map +1 -0
- package/dist/types/data-structures/graph/directed-graph.js.map +1 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +20 -0
- package/dist/types/data-structures/graph/index.js.map +1 -0
- package/dist/types/data-structures/graph/map-graph.js.map +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +3 -0
- package/dist/types/data-structures/graph/undirected-graph.js.map +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +3 -0
- package/dist/types/data-structures/hash/coordinate-map.js.map +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +3 -0
- package/dist/types/data-structures/hash/coordinate-set.js.map +1 -0
- package/dist/types/data-structures/hash/hash-map.js +3 -0
- package/dist/types/data-structures/hash/hash-map.js.map +1 -0
- package/dist/types/data-structures/hash/hash-table.js +3 -0
- package/dist/types/data-structures/hash/hash-table.js.map +1 -0
- package/dist/types/data-structures/hash/index.js +3 -0
- package/dist/types/data-structures/hash/index.js.map +1 -0
- package/dist/types/data-structures/hash/tree-map.js +3 -0
- package/dist/types/data-structures/hash/tree-map.js.map +1 -0
- package/dist/types/data-structures/hash/tree-set.js +3 -0
- package/dist/types/data-structures/hash/tree-set.js.map +1 -0
- package/dist/types/data-structures/heap/heap.js.map +1 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +18 -0
- package/dist/types/data-structures/heap/index.js.map +1 -0
- package/dist/types/data-structures/heap/max-heap.js +3 -0
- package/dist/types/data-structures/heap/max-heap.js.map +1 -0
- package/dist/types/data-structures/heap/min-heap.js +3 -0
- package/dist/types/data-structures/heap/min-heap.js.map +1 -0
- package/dist/types/data-structures/index.d.ts +8 -13
- package/dist/types/data-structures/index.js +8 -11
- package/dist/types/data-structures/index.js.map +1 -1
- package/dist/types/data-structures/linked-list/doubly-linked-list.js.map +1 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +19 -0
- package/dist/types/data-structures/linked-list/index.js.map +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js.map +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js.map +1 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +18 -0
- package/dist/types/data-structures/matrix/index.js.map +1 -0
- package/dist/types/data-structures/matrix/matrix.js +3 -0
- package/dist/types/data-structures/matrix/matrix.js.map +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +3 -0
- package/dist/types/data-structures/matrix/matrix2d.js.map +1 -0
- package/dist/types/data-structures/matrix/navigator.js.map +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +3 -0
- package/dist/types/data-structures/matrix/vector2d.js.map +1 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +20 -0
- package/dist/types/data-structures/priority-queue/index.js.map +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js.map +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js.map +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +3 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js.map +1 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +3 -0
- package/dist/types/data-structures/queue/deque.js.map +1 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +19 -0
- package/dist/types/data-structures/queue/index.js.map +1 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +3 -0
- package/dist/types/data-structures/queue/queue.js.map +1 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +18 -0
- package/dist/types/data-structures/stack/index.js.map +1 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +3 -0
- package/dist/types/data-structures/stack/stack.js.map +1 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +18 -0
- package/dist/types/data-structures/tree/index.js.map +1 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/{hash.js → tree/tree.js} +1 -1
- package/dist/types/data-structures/tree/tree.js.map +1 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +18 -0
- package/dist/types/data-structures/trie/index.js.map +1 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +3 -0
- package/dist/types/data-structures/trie/trie.js.map +1 -0
- package/dist/types/helpers.d.ts +2 -0
- package/dist/utils/utils.d.ts +1 -0
- package/dist/utils/utils.js +8 -1
- package/dist/utils/utils.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/lib/data-structures/binary-tree/avl-tree.js +42 -34
- package/lib/data-structures/binary-tree/binary-indexed-tree.d.ts +141 -43
- package/lib/data-structures/binary-tree/binary-indexed-tree.js +230 -42
- package/lib/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/lib/data-structures/binary-tree/binary-tree.js +257 -170
- package/lib/data-structures/binary-tree/bst.d.ts +104 -59
- package/lib/data-structures/binary-tree/bst.js +105 -60
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/lib/data-structures/binary-tree/tree-multiset.js +47 -39
- package/{dist/types/data-structures → lib/types/data-structures/binary-tree}/avl-tree.d.ts +1 -1
- package/lib/types/data-structures/binary-tree/avl-tree.js +1 -0
- package/lib/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/lib/types/data-structures/binary-tree/binary-indexed-tree.js +1 -0
- package/lib/types/data-structures/{binary-tree.d.ts → binary-tree/binary-tree.d.ts} +1 -5
- package/lib/types/data-structures/{bst.d.ts → binary-tree/bst.d.ts} +1 -1
- package/lib/types/data-structures/binary-tree/index.d.ts +6 -0
- package/lib/types/data-structures/binary-tree/index.js +6 -0
- package/lib/types/data-structures/{rb-tree.d.ts → binary-tree/rb-tree.d.ts} +1 -1
- package/lib/types/data-structures/binary-tree/segment-tree.js +1 -0
- package/lib/types/data-structures/{tree-multiset.d.ts → binary-tree/tree-multiset.d.ts} +1 -1
- package/lib/types/data-structures/binary-tree/tree-multiset.js +1 -0
- package/lib/types/data-structures/graph/abstract-graph.js +1 -0
- package/lib/types/data-structures/graph/index.d.ts +3 -0
- package/lib/types/data-structures/graph/index.js +3 -0
- package/lib/types/data-structures/graph/map-graph.js +1 -0
- package/lib/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/lib/types/data-structures/graph/undirected-graph.js +1 -0
- package/lib/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/lib/types/data-structures/hash/coordinate-map.js +1 -0
- package/lib/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/lib/types/data-structures/hash/coordinate-set.js +1 -0
- package/lib/types/data-structures/hash/hash-map.d.ts +1 -0
- package/lib/types/data-structures/hash/hash-map.js +1 -0
- package/lib/types/data-structures/hash/hash-table.d.ts +1 -0
- package/lib/types/data-structures/hash/hash-table.js +1 -0
- package/lib/types/data-structures/hash/index.js +1 -0
- package/lib/types/data-structures/hash/tree-map.d.ts +1 -0
- package/lib/types/data-structures/hash/tree-map.js +1 -0
- package/lib/types/data-structures/hash/tree-set.d.ts +1 -0
- package/lib/types/data-structures/hash/tree-set.js +1 -0
- package/lib/types/data-structures/heap/heap.d.ts +1 -0
- package/lib/types/data-structures/heap/heap.js +1 -0
- package/lib/types/data-structures/heap/index.d.ts +1 -0
- package/lib/types/data-structures/heap/index.js +1 -0
- package/lib/types/data-structures/heap/max-heap.d.ts +1 -0
- package/lib/types/data-structures/heap/max-heap.js +1 -0
- package/lib/types/data-structures/heap/min-heap.d.ts +1 -0
- package/lib/types/data-structures/heap/min-heap.js +1 -0
- package/lib/types/data-structures/index.d.ts +8 -13
- package/lib/types/data-structures/index.js +8 -11
- package/lib/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/lib/types/data-structures/linked-list/doubly-linked-list.js +1 -0
- package/lib/types/data-structures/linked-list/index.d.ts +2 -0
- package/lib/types/data-structures/linked-list/index.js +2 -0
- package/lib/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/lib/types/data-structures/linked-list/singly-linked-list.js +1 -0
- package/lib/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/lib/types/data-structures/linked-list/skip-linked-list.js +1 -0
- package/lib/types/data-structures/matrix/index.d.ts +1 -0
- package/lib/types/data-structures/matrix/index.js +1 -0
- package/lib/types/data-structures/matrix/matrix.d.ts +1 -0
- package/lib/types/data-structures/matrix/matrix.js +1 -0
- package/lib/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/lib/types/data-structures/matrix/matrix2d.js +1 -0
- package/lib/types/data-structures/matrix/navigator.js +1 -0
- package/lib/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/lib/types/data-structures/matrix/vector2d.js +1 -0
- package/lib/types/data-structures/priority-queue/index.d.ts +3 -0
- package/lib/types/data-structures/priority-queue/index.js +3 -0
- package/lib/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/lib/types/data-structures/priority-queue/max-priority-queue.js +1 -0
- package/lib/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/lib/types/data-structures/priority-queue/min-priority-queue.js +1 -0
- package/lib/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/lib/types/data-structures/priority-queue/priority-queue.js +1 -0
- package/lib/types/data-structures/queue/deque.d.ts +1 -0
- package/lib/types/data-structures/queue/deque.js +1 -0
- package/lib/types/data-structures/queue/index.d.ts +2 -0
- package/lib/types/data-structures/queue/index.js +2 -0
- package/lib/types/data-structures/queue/queue.d.ts +1 -0
- package/lib/types/data-structures/queue/queue.js +1 -0
- package/lib/types/data-structures/stack/index.d.ts +1 -0
- package/lib/types/data-structures/stack/index.js +1 -0
- package/lib/types/data-structures/stack/stack.d.ts +1 -0
- package/lib/types/data-structures/stack/stack.js +1 -0
- package/lib/types/data-structures/tree/index.d.ts +1 -0
- package/lib/types/data-structures/tree/index.js +1 -0
- package/lib/types/data-structures/tree/tree.d.ts +1 -0
- package/lib/types/data-structures/tree/tree.js +1 -0
- package/lib/types/data-structures/trie/index.d.ts +1 -0
- package/lib/types/data-structures/trie/index.js +1 -0
- package/lib/types/data-structures/trie/trie.d.ts +1 -0
- package/lib/types/data-structures/trie/trie.js +1 -0
- package/lib/types/helpers.d.ts +2 -0
- package/lib/utils/utils.d.ts +1 -0
- package/lib/utils/utils.js +6 -0
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +42 -34
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +264 -42
- package/src/data-structures/binary-tree/binary-tree.ts +267 -172
- package/src/data-structures/binary-tree/bst.ts +111 -60
- package/src/data-structures/binary-tree/tree-multiset.ts +47 -39
- package/src/types/data-structures/{avl-tree.ts → binary-tree/avl-tree.ts} +1 -1
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/{binary-tree.ts → binary-tree/binary-tree.ts} +1 -13
- package/src/types/data-structures/{bst.ts → binary-tree/bst.ts} +1 -1
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/{rb-tree.ts → binary-tree/rb-tree.ts} +2 -2
- package/src/types/data-structures/{tree-multiset.ts → binary-tree/tree-multiset.ts} +1 -1
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +8 -14
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +4 -1
- package/src/utils/utils.ts +7 -0
- package/test/config.js +4 -0
- package/test/types/index.js +29 -0
- package/test/types/utils/big-o.js +2 -0
- package/test/types/utils/index.js +29 -0
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +284 -0
- package/test/utils/big-o.js +212 -0
- package/test/utils/index.js +30 -0
- package/test/utils/number.js +14 -0
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
- package/dist/types/data-structures/abstract-graph.js.map +0 -1
- package/dist/types/data-structures/avl-tree.js.map +0 -1
- package/dist/types/data-structures/binary-tree.js.map +0 -1
- package/dist/types/data-structures/bst.js.map +0 -1
- package/dist/types/data-structures/directed-graph.js.map +0 -1
- package/dist/types/data-structures/doubly-linked-list.js.map +0 -1
- package/dist/types/data-structures/hash.js.map +0 -1
- package/dist/types/data-structures/heap.js.map +0 -1
- package/dist/types/data-structures/map-graph.js.map +0 -1
- package/dist/types/data-structures/navigator.js.map +0 -1
- package/dist/types/data-structures/rb-tree.js.map +0 -1
- package/dist/types/data-structures/segment-tree.js.map +0 -1
- package/dist/types/data-structures/singly-linked-list.js.map +0 -1
- package/dist/types/data-structures/tree-multiset.js.map +0 -1
- /package/dist/types/data-structures/{avl-tree.js → binary-tree/avl-tree.js} +0 -0
- /package/dist/types/data-structures/{doubly-linked-list.d.ts → binary-tree/binary-indexed-tree.d.ts} +0 -0
- /package/dist/types/data-structures/{binary-tree.js → binary-tree/binary-tree.js} +0 -0
- /package/dist/types/data-structures/{bst.js → binary-tree/bst.js} +0 -0
- /package/dist/types/data-structures/{rb-tree.js → binary-tree/rb-tree.js} +0 -0
- /package/dist/types/data-structures/{segment-tree.d.ts → binary-tree/segment-tree.d.ts} +0 -0
- /package/dist/types/data-structures/{segment-tree.js → binary-tree/segment-tree.js} +0 -0
- /package/dist/types/data-structures/{tree-multiset.js → binary-tree/tree-multiset.js} +0 -0
- /package/dist/types/data-structures/{abstract-graph.d.ts → graph/abstract-graph.d.ts} +0 -0
- /package/dist/types/data-structures/{abstract-graph.js → graph/abstract-graph.js} +0 -0
- /package/dist/types/data-structures/{directed-graph.d.ts → graph/directed-graph.d.ts} +0 -0
- /package/dist/types/data-structures/{directed-graph.js → graph/directed-graph.js} +0 -0
- /package/dist/types/data-structures/{map-graph.d.ts → graph/map-graph.d.ts} +0 -0
- /package/dist/types/data-structures/{map-graph.js → graph/map-graph.js} +0 -0
- /package/dist/types/data-structures/{heap.d.ts → graph/undirected-graph.d.ts} +0 -0
- /package/dist/types/data-structures/{singly-linked-list.d.ts → hash/coordinate-map.d.ts} +0 -0
- /package/{lib/types/data-structures/abstract-graph.js → dist/types/data-structures/hash/coordinate-set.d.ts} +0 -0
- /package/{lib/types/data-structures/avl-tree.js → dist/types/data-structures/hash/hash-map.d.ts} +0 -0
- /package/{lib/types/data-structures/doubly-linked-list.d.ts → dist/types/data-structures/hash/hash-table.d.ts} +0 -0
- /package/dist/types/data-structures/{hash.d.ts → hash/index.d.ts} +0 -0
- /package/{lib/types/data-structures/doubly-linked-list.js → dist/types/data-structures/hash/tree-map.d.ts} +0 -0
- /package/{lib/types/data-structures/hash.js → dist/types/data-structures/hash/tree-set.d.ts} +0 -0
- /package/{lib/types/data-structures → dist/types/data-structures/heap}/heap.d.ts +0 -0
- /package/dist/types/data-structures/{heap.js → heap/heap.js} +0 -0
- /package/{lib/types/data-structures/heap.js → dist/types/data-structures/heap/max-heap.d.ts} +0 -0
- /package/{lib/types/data-structures/map-graph.js → dist/types/data-structures/heap/min-heap.d.ts} +0 -0
- /package/{lib/types/data-structures/navigator.js → dist/types/data-structures/linked-list/doubly-linked-list.d.ts} +0 -0
- /package/dist/types/data-structures/{doubly-linked-list.js → linked-list/doubly-linked-list.js} +0 -0
- /package/{lib/types/data-structures → dist/types/data-structures/linked-list}/singly-linked-list.d.ts +0 -0
- /package/dist/types/data-structures/{singly-linked-list.js → linked-list/singly-linked-list.js} +0 -0
- /package/{lib/types/data-structures/segment-tree.js → dist/types/data-structures/linked-list/skip-linked-list.d.ts} +0 -0
- /package/{lib/types/data-structures/singly-linked-list.js → dist/types/data-structures/matrix/matrix.d.ts} +0 -0
- /package/{lib/types/data-structures/tree-multiset.js → dist/types/data-structures/matrix/matrix2d.d.ts} +0 -0
- /package/dist/types/data-structures/{navigator.d.ts → matrix/navigator.d.ts} +0 -0
- /package/dist/types/data-structures/{navigator.js → matrix/navigator.js} +0 -0
- /package/{src/types/data-structures/doubly-linked-list.ts → dist/types/data-structures/matrix/vector2d.d.ts} +0 -0
- /package/{src/types/data-structures/heap.ts → dist/types/data-structures/priority-queue/max-priority-queue.d.ts} +0 -0
- /package/{src/types/data-structures/singly-linked-list.ts → dist/types/data-structures/priority-queue/min-priority-queue.d.ts} +0 -0
- /package/lib/types/data-structures/{binary-tree.js → binary-tree/binary-tree.js} +0 -0
- /package/lib/types/data-structures/{bst.js → binary-tree/bst.js} +0 -0
- /package/lib/types/data-structures/{rb-tree.js → binary-tree/rb-tree.js} +0 -0
- /package/lib/types/data-structures/{segment-tree.d.ts → binary-tree/segment-tree.d.ts} +0 -0
- /package/lib/types/data-structures/{abstract-graph.d.ts → graph/abstract-graph.d.ts} +0 -0
- /package/lib/types/data-structures/{directed-graph.d.ts → graph/directed-graph.d.ts} +0 -0
- /package/lib/types/data-structures/{directed-graph.js → graph/directed-graph.js} +0 -0
- /package/lib/types/data-structures/{map-graph.d.ts → graph/map-graph.d.ts} +0 -0
- /package/lib/types/data-structures/{hash.d.ts → hash/index.d.ts} +0 -0
- /package/lib/types/data-structures/{navigator.d.ts → matrix/navigator.d.ts} +0 -0
- /package/src/types/data-structures/{segment-tree.ts → binary-tree/segment-tree.ts} +0 -0
- /package/src/types/data-structures/{abstract-graph.ts → graph/abstract-graph.ts} +0 -0
- /package/src/types/data-structures/{directed-graph.ts → graph/directed-graph.ts} +0 -0
- /package/src/types/data-structures/{map-graph.ts → graph/map-graph.ts} +0 -0
- /package/src/types/data-structures/{hash.ts → hash/index.ts} +0 -0
- /package/src/types/data-structures/{navigator.ts → matrix/navigator.ts} +0 -0
|
@@ -20,29 +20,45 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
20
20
|
import {trampoline} from '../../utils';
|
|
21
21
|
import {Queue} from '../queue';
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Represents a node in a binary tree.
|
|
25
|
+
* @template V - The type of data stored in the node.
|
|
26
|
+
* @template FAMILY - The type of the family relationship in the binary tree.
|
|
27
|
+
*/
|
|
23
28
|
export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
|
|
24
29
|
/**
|
|
25
|
-
*
|
|
26
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
27
|
-
*
|
|
28
|
-
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
|
29
|
-
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
|
30
|
+
* Creates a new instance of BinaryTreeNode.
|
|
31
|
+
* @param {BinaryTreeNodeKey} key - The key associated with the node.
|
|
32
|
+
* @param {V} val - The value stored in the node.
|
|
30
33
|
*/
|
|
31
34
|
constructor(key: BinaryTreeNodeKey, val?: V) {
|
|
32
35
|
this.key = key;
|
|
33
36
|
this.val = val;
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
/**
|
|
40
|
+
* The key associated with the node.
|
|
41
|
+
*/
|
|
36
42
|
key: BinaryTreeNodeKey;
|
|
37
43
|
|
|
44
|
+
/**
|
|
45
|
+
* The value stored in the node.
|
|
46
|
+
*/
|
|
38
47
|
val: V | undefined;
|
|
39
48
|
|
|
40
49
|
private _left: FAMILY | null | undefined;
|
|
41
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Get the left child node.
|
|
53
|
+
*/
|
|
42
54
|
get left(): FAMILY | null | undefined {
|
|
43
55
|
return this._left;
|
|
44
56
|
}
|
|
45
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Set the left child node.
|
|
60
|
+
* @param {FAMILY | null | undefined} v - The left child node.
|
|
61
|
+
*/
|
|
46
62
|
set left(v: FAMILY | null | undefined) {
|
|
47
63
|
if (v) {
|
|
48
64
|
v.parent = this as unknown as FAMILY;
|
|
@@ -52,10 +68,17 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
52
68
|
|
|
53
69
|
private _right: FAMILY | null | undefined;
|
|
54
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Get the right child node.
|
|
73
|
+
*/
|
|
55
74
|
get right(): FAMILY | null | undefined {
|
|
56
75
|
return this._right;
|
|
57
76
|
}
|
|
58
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Set the right child node.
|
|
80
|
+
* @param {FAMILY | null | undefined} v - The right child node.
|
|
81
|
+
*/
|
|
59
82
|
set right(v: FAMILY | null | undefined) {
|
|
60
83
|
if (v) {
|
|
61
84
|
v.parent = this as unknown as FAMILY;
|
|
@@ -63,11 +86,14 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
63
86
|
this._right = v;
|
|
64
87
|
}
|
|
65
88
|
|
|
89
|
+
/**
|
|
90
|
+
* The parent node of the current node.
|
|
91
|
+
*/
|
|
66
92
|
parent: FAMILY | null | undefined;
|
|
67
93
|
|
|
68
94
|
/**
|
|
69
|
-
*
|
|
70
|
-
* @returns
|
|
95
|
+
* Get the position of the node within its family.
|
|
96
|
+
* @returns {FamilyPosition} - The family position of the node.
|
|
71
97
|
*/
|
|
72
98
|
get familyPosition(): FamilyPosition {
|
|
73
99
|
const that = this as unknown as FAMILY;
|
|
@@ -97,12 +123,14 @@ export class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> =
|
|
|
97
123
|
}
|
|
98
124
|
}
|
|
99
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Represents a binary tree data structure.
|
|
128
|
+
* @template N - The type of the binary tree's nodes.
|
|
129
|
+
*/
|
|
100
130
|
export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
|
|
101
131
|
/**
|
|
102
|
-
*
|
|
103
|
-
* @param {BinaryTreeOptions} [options] - The
|
|
104
|
-
* constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
|
|
105
|
-
* different configuration options.
|
|
132
|
+
* Creates a new instance of BinaryTree.
|
|
133
|
+
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
|
106
134
|
*/
|
|
107
135
|
constructor(options?: BinaryTreeOptions) {
|
|
108
136
|
if (options !== undefined) {
|
|
@@ -112,46 +140,55 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
112
140
|
}
|
|
113
141
|
|
|
114
142
|
/**
|
|
115
|
-
*
|
|
116
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
* stored in the node.
|
|
120
|
-
* @returns a new instance of a BinaryTreeNode with the specified key and value.
|
|
143
|
+
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
144
|
+
* @param {BinaryTreeNodeKey} key - The key for the new node.
|
|
145
|
+
* @param {N['val']} val - The value for the new node.
|
|
146
|
+
* @returns {N} - The newly created BinaryTreeNode.
|
|
121
147
|
*/
|
|
122
148
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
|
|
123
149
|
return new BinaryTreeNode<N['val'], N>(key, val) as N;
|
|
124
150
|
}
|
|
125
151
|
|
|
126
|
-
// TODO placeholder node may need redesigned
|
|
127
152
|
private _root: N | null = null;
|
|
128
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Get the root node of the binary tree.
|
|
156
|
+
*/
|
|
129
157
|
get root(): N | null {
|
|
130
158
|
return this._root;
|
|
131
159
|
}
|
|
132
160
|
|
|
133
161
|
private _size = 0;
|
|
134
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Get the number of nodes in the binary tree.
|
|
165
|
+
*/
|
|
135
166
|
get size(): number {
|
|
136
167
|
return this._size;
|
|
137
168
|
}
|
|
138
169
|
|
|
139
170
|
private _loopType: IterationType = IterationType.ITERATIVE;
|
|
140
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Get the iteration type used in the binary tree.
|
|
174
|
+
*/
|
|
141
175
|
get iterationType(): IterationType {
|
|
142
176
|
return this._loopType;
|
|
143
177
|
}
|
|
144
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Set the iteration type for the binary tree.
|
|
181
|
+
* @param {IterationType} v - The new iteration type to set.
|
|
182
|
+
*/
|
|
145
183
|
set iterationType(v: IterationType) {
|
|
146
184
|
this._loopType = v;
|
|
147
185
|
}
|
|
148
186
|
|
|
149
187
|
/**
|
|
150
|
-
*
|
|
151
|
-
* @param {N} srcNode - The source node
|
|
152
|
-
* @param {N} destNode - The
|
|
153
|
-
*
|
|
154
|
-
* @returns The `destNode` is being returned.
|
|
188
|
+
* Swap the data of two nodes in the binary tree.
|
|
189
|
+
* @param {N} srcNode - The source node to swap.
|
|
190
|
+
* @param {N} destNode - The destination node to swap.
|
|
191
|
+
* @returns {N} - The destination node after the swap.
|
|
155
192
|
*/
|
|
156
193
|
protected _swap(srcNode: N, destNode: N): N {
|
|
157
194
|
const {key, val} = destNode;
|
|
@@ -169,7 +206,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
169
206
|
}
|
|
170
207
|
|
|
171
208
|
/**
|
|
172
|
-
*
|
|
209
|
+
* Clear the binary tree, removing all nodes.
|
|
173
210
|
*/
|
|
174
211
|
clear() {
|
|
175
212
|
this._root = null;
|
|
@@ -177,26 +214,18 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
177
214
|
}
|
|
178
215
|
|
|
179
216
|
/**
|
|
180
|
-
*
|
|
181
|
-
* @returns
|
|
217
|
+
* Check if the binary tree is empty.
|
|
218
|
+
* @returns {boolean} - True if the binary tree is empty, false otherwise.
|
|
182
219
|
*/
|
|
183
220
|
isEmpty(): boolean {
|
|
184
221
|
return this.size === 0;
|
|
185
222
|
}
|
|
186
223
|
|
|
187
224
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* The `add` function adds a new node to a binary tree, either by ID or by creating a new node with a given value.
|
|
194
|
-
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey`, which
|
|
195
|
-
* is a number representing the ID of a binary tree node, or it can be a `N` object, which represents a binary tree
|
|
196
|
-
* node itself. It can also be `null` if no node is specified.
|
|
197
|
-
* @param [val] - The `val` parameter is an optional value that can be assigned to the `val` property of the new node
|
|
198
|
-
* being added to the binary tree.
|
|
199
|
-
* @returns The function `add` returns either the inserted node (`N`), `null`, or `undefined`.
|
|
225
|
+
* Add a node with the given key and value to the binary tree.
|
|
226
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
227
|
+
* @param {N['val']} val - The value for the new node (optional).
|
|
228
|
+
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
200
229
|
*/
|
|
201
230
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
202
231
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
@@ -252,12 +281,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
252
281
|
* values, and adds them to the binary tree.
|
|
253
282
|
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
|
|
254
283
|
* objects, or null values.
|
|
255
|
-
* @param {N['val'][]} [
|
|
256
|
-
* the nodes or node IDs being added. It is used to set the value of each node being added. If `
|
|
284
|
+
* @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
|
|
285
|
+
* the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
|
|
257
286
|
* the value of the nodes will be `undefined`.
|
|
258
287
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
259
288
|
*/
|
|
260
|
-
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
|
289
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[] {
|
|
261
290
|
// TODO not sure addMany not be run multi times
|
|
262
291
|
const inserted: (N | null | undefined)[] = [];
|
|
263
292
|
|
|
@@ -273,7 +302,7 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
273
302
|
continue;
|
|
274
303
|
}
|
|
275
304
|
|
|
276
|
-
const val =
|
|
305
|
+
const val = values?.[i];
|
|
277
306
|
inserted.push(this.add(keyOrNode, val));
|
|
278
307
|
}
|
|
279
308
|
return inserted;
|
|
@@ -294,11 +323,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
294
323
|
}
|
|
295
324
|
|
|
296
325
|
/**
|
|
297
|
-
* The `delete` function
|
|
298
|
-
*
|
|
299
|
-
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node
|
|
300
|
-
*
|
|
301
|
-
*
|
|
326
|
+
* The `delete` function removes a node from a binary search tree and returns the deleted node along
|
|
327
|
+
* with the parent node that needs to be balanced.
|
|
328
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node (`N`) or
|
|
329
|
+
* a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
|
|
330
|
+
* binary tree.
|
|
331
|
+
* @returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
302
332
|
*/
|
|
303
333
|
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
304
334
|
const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
|
|
@@ -343,10 +373,16 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
343
373
|
}
|
|
344
374
|
|
|
345
375
|
/**
|
|
346
|
-
* The function calculates the depth of a node in a binary tree
|
|
347
|
-
*
|
|
348
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
349
|
-
*
|
|
376
|
+
* The function `getDepth` calculates the depth of a given node in a binary tree relative to a
|
|
377
|
+
* specified root node.
|
|
378
|
+
* @param {N | BinaryTreeNodeKey | null} distNode - The `distNode` parameter represents the node
|
|
379
|
+
* whose depth we want to find in the binary tree. It can be either a node object (`N`), a key value
|
|
380
|
+
* of the node (`BinaryTreeNodeKey`), or `null`.
|
|
381
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
382
|
+
* starting node from which we want to calculate the depth. It can be either a node object or the key
|
|
383
|
+
* of a node in the binary tree. If no value is provided for `beginRoot`, it defaults to the root
|
|
384
|
+
* node of the binary tree.
|
|
385
|
+
* @returns the depth of the `distNode` relative to the `beginRoot`.
|
|
350
386
|
*/
|
|
351
387
|
getDepth(distNode: N | BinaryTreeNodeKey | null, beginRoot: N | BinaryTreeNodeKey | null = this.root): number {
|
|
352
388
|
if (typeof distNode === 'number') distNode = this.get(distNode);
|
|
@@ -363,11 +399,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
363
399
|
}
|
|
364
400
|
|
|
365
401
|
/**
|
|
366
|
-
* The `getHeight` function calculates the maximum height of a binary tree
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
* node
|
|
370
|
-
*
|
|
402
|
+
* The `getHeight` function calculates the maximum height of a binary tree using either recursive or
|
|
403
|
+
* iterative approach.
|
|
404
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter represents the
|
|
405
|
+
* starting node from which the height of the binary tree is calculated. It can be either a node
|
|
406
|
+
* object (`N`), a key value of a node in the tree (`BinaryTreeNodeKey`), or `null` if no starting
|
|
407
|
+
* node is specified. If `
|
|
408
|
+
* @param iterationType - The `iterationType` parameter is used to determine whether to calculate the
|
|
409
|
+
* height of the binary tree using a recursive approach or an iterative approach. It can have two
|
|
410
|
+
* possible values:
|
|
371
411
|
* @returns the height of the binary tree.
|
|
372
412
|
*/
|
|
373
413
|
getHeight(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): number {
|
|
@@ -412,13 +452,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
412
452
|
protected _defaultCallbackByKey: MapCallback<N> = node => node.key;
|
|
413
453
|
|
|
414
454
|
/**
|
|
415
|
-
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
416
|
-
* approach.
|
|
417
|
-
* @param {N | null}
|
|
418
|
-
*
|
|
419
|
-
*
|
|
420
|
-
* @param iterationType - The `iterationType` parameter is
|
|
421
|
-
*
|
|
455
|
+
* The `getMinHeight` function calculates the minimum height of a binary tree using either a
|
|
456
|
+
* recursive or iterative approach.
|
|
457
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
458
|
+
* calculate the minimum height of the tree. It is optional and defaults to the root of the tree if
|
|
459
|
+
* not provided.
|
|
460
|
+
* @param iterationType - The `iterationType` parameter is used to determine the method of iteration
|
|
461
|
+
* to calculate the minimum height of a binary tree. It can have two possible values:
|
|
462
|
+
* @returns The function `getMinHeight` returns the minimum height of a binary tree.
|
|
422
463
|
*/
|
|
423
464
|
getMinHeight(beginRoot: N | null = this.root, iterationType = this.iterationType): number {
|
|
424
465
|
if (!beginRoot) return -1;
|
|
@@ -463,10 +504,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
463
504
|
}
|
|
464
505
|
|
|
465
506
|
/**
|
|
466
|
-
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
467
|
-
* tree.
|
|
468
|
-
* @param {N | null}
|
|
469
|
-
* tree or null
|
|
507
|
+
* The function checks if a binary tree is perfectly balanced by comparing the minimum height and the
|
|
508
|
+
* height of the tree.
|
|
509
|
+
* @param {N | null} beginRoot - The parameter `beginRoot` is of type `N | null`, which means it can
|
|
510
|
+
* either be of type `N` (representing a node in a tree) or `null` (representing an empty tree).
|
|
470
511
|
* @returns The method is returning a boolean value.
|
|
471
512
|
*/
|
|
472
513
|
isPerfectlyBalanced(beginRoot: N | null = this.root): boolean {
|
|
@@ -474,17 +515,25 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
474
515
|
}
|
|
475
516
|
|
|
476
517
|
/**
|
|
477
|
-
* The function `getNodes` returns an array of nodes that match a given property
|
|
478
|
-
*
|
|
479
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter
|
|
480
|
-
* generic type `N`. It represents the property of the
|
|
481
|
-
*
|
|
482
|
-
* @param
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
486
|
-
* @param
|
|
487
|
-
*
|
|
518
|
+
* The function `getNodes` returns an array of nodes that match a given node property, using either
|
|
519
|
+
* recursive or iterative traversal.
|
|
520
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is either a
|
|
521
|
+
* `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
|
|
522
|
+
* searching for. It can be a specific key value or any other property of the node.
|
|
523
|
+
* @param callback - The `callback` parameter is a function that takes a node as input and returns a
|
|
524
|
+
* value. This value is compared with the `nodeProperty` parameter to determine if the node should be
|
|
525
|
+
* included in the result. The `callback` parameter has a default value of
|
|
526
|
+
* `this._defaultCallbackByKey`, which
|
|
527
|
+
* @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
|
|
528
|
+
* first node that matches the nodeProperty. If set to true, the function will return an array with
|
|
529
|
+
* only one element (or an empty array if no matching node is found). If set to false (default), the
|
|
530
|
+
* function will continue searching for all
|
|
531
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
|
|
532
|
+
* traversal of the binary tree will begin. It is optional and defaults to the root of the binary
|
|
533
|
+
* tree.
|
|
534
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration used to
|
|
535
|
+
* traverse the binary tree. It can have two possible values:
|
|
536
|
+
* @returns The function `getNodes` returns an array of nodes (`N[]`).
|
|
488
537
|
*/
|
|
489
538
|
getNodes(
|
|
490
539
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
@@ -528,13 +577,20 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
528
577
|
}
|
|
529
578
|
|
|
530
579
|
/**
|
|
531
|
-
* The function checks if a binary tree node
|
|
532
|
-
* @param
|
|
533
|
-
*
|
|
534
|
-
*
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
580
|
+
* The function checks if a binary tree has a node with a given property or key.
|
|
581
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
582
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
|
|
583
|
+
* generic type `N`.
|
|
584
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
585
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
586
|
+
* whether the node matches the criteria or not. The default callback function
|
|
587
|
+
* `this._defaultCallbackByKey` is used if no callback function is
|
|
588
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
589
|
+
* the node from which the search should begin. By default, it is set to `this.root`, which means the
|
|
590
|
+
* search will start from the root node of the binary tree. However, you can provide a different node
|
|
591
|
+
* as
|
|
592
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
593
|
+
* performed when searching for nodes in the binary tree. It can have one of the following values:
|
|
538
594
|
* @returns a boolean value.
|
|
539
595
|
*/
|
|
540
596
|
has(
|
|
@@ -548,17 +604,19 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
548
604
|
}
|
|
549
605
|
|
|
550
606
|
/**
|
|
551
|
-
* The function returns the first node that matches the given property
|
|
552
|
-
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
*
|
|
558
|
-
*
|
|
559
|
-
* @param
|
|
560
|
-
*
|
|
561
|
-
*
|
|
607
|
+
* The function `get` returns the first node in a binary tree that matches the given property or key.
|
|
608
|
+
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is the key or value of
|
|
609
|
+
* the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
|
|
610
|
+
* type.
|
|
611
|
+
* @param callback - The `callback` parameter is a function that is used to determine whether a node
|
|
612
|
+
* matches the desired criteria. It takes a node as input and returns a boolean value indicating
|
|
613
|
+
* whether the node matches the criteria or not. The default callback function
|
|
614
|
+
* (`this._defaultCallbackByKey`) is used if no callback function is
|
|
615
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
|
|
616
|
+
* the root node from which the search should begin.
|
|
617
|
+
* @param iterationType - The `iterationType` parameter specifies the type of iteration to be
|
|
618
|
+
* performed when searching for a node in the binary tree. It can have one of the following values:
|
|
619
|
+
* @returns either the found node (of type N) or null if no node is found.
|
|
562
620
|
*/
|
|
563
621
|
get(
|
|
564
622
|
nodeProperty: BinaryTreeNodeKey | N,
|
|
@@ -571,14 +629,14 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
571
629
|
}
|
|
572
630
|
|
|
573
631
|
/**
|
|
574
|
-
* The function `getPathToRoot` returns an array of nodes
|
|
575
|
-
*
|
|
576
|
-
*
|
|
577
|
-
*
|
|
578
|
-
* @param
|
|
579
|
-
* path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
580
|
-
* `isReverse` is set to `false` or not provided, the path will
|
|
581
|
-
* @returns The function `getPathToRoot` returns an array of
|
|
632
|
+
* The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
|
|
633
|
+
* up to the root node, with the option to reverse the order of the nodes.
|
|
634
|
+
* @param {N} beginRoot - The `beginRoot` parameter represents the starting node from which you want
|
|
635
|
+
* to find the path to the root node.
|
|
636
|
+
* @param [isReverse=true] - The `isReverse` parameter is a boolean flag that determines whether the
|
|
637
|
+
* resulting path should be reversed or not. If `isReverse` is set to `true`, the path will be
|
|
638
|
+
* reversed before returning it. If `isReverse` is set to `false` or not provided, the path will
|
|
639
|
+
* @returns The function `getPathToRoot` returns an array of type `N[]`.
|
|
582
640
|
*/
|
|
583
641
|
getPathToRoot(beginRoot: N, isReverse = true): N[] {
|
|
584
642
|
// TODO to support get path through passing key
|
|
@@ -594,16 +652,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
594
652
|
}
|
|
595
653
|
|
|
596
654
|
/**
|
|
597
|
-
* The function `getLeftMost` returns the leftmost node in a binary tree,
|
|
598
|
-
*
|
|
599
|
-
* @param {N | BinaryTreeNodeKey | null}
|
|
600
|
-
*
|
|
601
|
-
* node), or `null
|
|
602
|
-
* @param iterationType - The `iterationType` parameter is
|
|
603
|
-
*
|
|
604
|
-
*
|
|
605
|
-
*
|
|
606
|
-
* node is found (
|
|
655
|
+
* The function `getLeftMost` returns the leftmost node in a binary tree, either using recursive or
|
|
656
|
+
* iterative traversal.
|
|
657
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
658
|
+
* for finding the leftmost node in a binary tree. It can be either a node object (`N`), a key value
|
|
659
|
+
* of a node (`BinaryTreeNodeKey`), or `null` if the tree is empty.
|
|
660
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
661
|
+
* be performed when finding the leftmost node in a binary tree. It can have two possible values:
|
|
662
|
+
* @returns The function `getLeftMost` returns the leftmost node (`N`) in a binary tree. If there is
|
|
663
|
+
* no leftmost node, it returns `null`.
|
|
607
664
|
*/
|
|
608
665
|
getLeftMost(beginRoot: N | BinaryTreeNodeKey | null = this.root, iterationType = this.iterationType): N | null {
|
|
609
666
|
if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot);
|
|
@@ -629,15 +686,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
629
686
|
}
|
|
630
687
|
|
|
631
688
|
/**
|
|
632
|
-
* The `getRightMost`
|
|
633
|
-
*
|
|
634
|
-
* @param {N | null}
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
* @param iterationType - The `iterationType` parameter is
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
689
|
+
* The function `getRightMost` returns the rightmost node in a binary tree, either recursively or
|
|
690
|
+
* iteratively.
|
|
691
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which we want to
|
|
692
|
+
* find the rightmost node. It is of type `N | null`, which means it can either be a node of type `N`
|
|
693
|
+
* or `null`. If it is `null`, it means there is no starting node
|
|
694
|
+
* @param iterationType - The `iterationType` parameter is used to determine the type of iteration to
|
|
695
|
+
* be performed when finding the rightmost node in a binary tree. It can have two possible values:
|
|
696
|
+
* @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the
|
|
697
|
+
* `beginRoot` parameter is `null`, it returns `null`.
|
|
641
698
|
*/
|
|
642
699
|
getRightMost(beginRoot: N | null = this.root, iterationType = this.iterationType): N | null {
|
|
643
700
|
// TODO support get right most by passing key in
|
|
@@ -662,10 +719,13 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
662
719
|
}
|
|
663
720
|
|
|
664
721
|
/**
|
|
665
|
-
* The function checks if a binary
|
|
666
|
-
* @param {N
|
|
667
|
-
*
|
|
668
|
-
* @
|
|
722
|
+
* The function `isSubtreeBST` checks if a given binary tree is a valid binary search tree.
|
|
723
|
+
* @param {N} beginRoot - The `beginRoot` parameter is the root node of the binary tree that you want
|
|
724
|
+
* to check if it is a binary search tree (BST) subtree.
|
|
725
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
726
|
+
* type of iteration to use when checking if a subtree is a binary search tree (BST). It can have two
|
|
727
|
+
* possible values:
|
|
728
|
+
* @returns The function `isSubtreeBST` returns a boolean value.
|
|
669
729
|
*/
|
|
670
730
|
isSubtreeBST(beginRoot: N, iterationType = this.iterationType): boolean {
|
|
671
731
|
// TODO there is a bug
|
|
@@ -698,8 +758,12 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
698
758
|
}
|
|
699
759
|
|
|
700
760
|
/**
|
|
701
|
-
* The function
|
|
702
|
-
* @
|
|
761
|
+
* The function checks if a binary tree is a binary search tree.
|
|
762
|
+
* @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
|
|
763
|
+
* be used when checking if the binary tree is a binary search tree (BST). It is an optional
|
|
764
|
+
* parameter with a default value of "this.iterationType". The value of "this.iterationType" is not
|
|
765
|
+
* provided in
|
|
766
|
+
* @returns a boolean value.
|
|
703
767
|
*/
|
|
704
768
|
isBST(iterationType = this.iterationType): boolean {
|
|
705
769
|
if (this.root === null) return true;
|
|
@@ -707,13 +771,18 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
707
771
|
}
|
|
708
772
|
|
|
709
773
|
/**
|
|
710
|
-
* The function `subTreeTraverse`
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* @
|
|
774
|
+
* The function `subTreeTraverse` traverses a binary tree and applies a callback function to each
|
|
775
|
+
* node, either recursively or iteratively.
|
|
776
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
777
|
+
* subtree traversal. It takes a single argument, which is the current node being traversed, and
|
|
778
|
+
* returns a value. The return values from each callback invocation will be collected and returned as
|
|
779
|
+
* an array.
|
|
780
|
+
* @param {N | BinaryTreeNodeKey | null} beginRoot - The `beginRoot` parameter is the starting point
|
|
781
|
+
* for traversing the subtree. It can be either a node object, a key value of a node, or `null` to
|
|
782
|
+
* start from the root of the tree.
|
|
783
|
+
* @param iterationType - The `iterationType` parameter determines the type of traversal to be
|
|
784
|
+
* performed on the binary tree. It can have two possible values:
|
|
785
|
+
* @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
|
|
717
786
|
*/
|
|
718
787
|
subTreeTraverse(
|
|
719
788
|
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
@@ -748,13 +817,19 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
748
817
|
}
|
|
749
818
|
|
|
750
819
|
/**
|
|
751
|
-
* The dfs function performs a depth-first search traversal on a binary tree
|
|
752
|
-
* each node
|
|
753
|
-
* @param callback
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
* @param
|
|
757
|
-
*
|
|
820
|
+
* The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
|
|
821
|
+
* function on each node according to a specified order pattern.
|
|
822
|
+
* @param callback - The `callback` parameter is a function that will be called on each node during
|
|
823
|
+
* the depth-first search traversal. It takes a node as input and returns a value. The default value
|
|
824
|
+
* is `this._defaultCallbackByKey`, which is a callback function defined elsewhere in the code.
|
|
825
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter determines the order in which the
|
|
826
|
+
* nodes are visited during the depth-first search. There are three possible values for `pattern`:
|
|
827
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the depth-first
|
|
828
|
+
* search. It determines where the search will begin in the tree or graph structure. If `beginRoot`
|
|
829
|
+
* is `null`, an empty array will be returned.
|
|
830
|
+
* @param {IterationType} iterationType - The `iterationType` parameter determines the type of
|
|
831
|
+
* iteration used in the depth-first search algorithm. It can have two possible values:
|
|
832
|
+
* @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
|
|
758
833
|
*/
|
|
759
834
|
dfs(
|
|
760
835
|
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
@@ -830,11 +905,21 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
830
905
|
// --- start additional methods ---
|
|
831
906
|
|
|
832
907
|
/**
|
|
833
|
-
* The
|
|
834
|
-
*
|
|
835
|
-
* @param
|
|
836
|
-
*
|
|
837
|
-
*
|
|
908
|
+
* The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
|
|
909
|
+
* function on each node.
|
|
910
|
+
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
|
911
|
+
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
|
912
|
+
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
|
913
|
+
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
|
914
|
+
* whether or not to include the level of each node in the callback function. If `withLevel` is set
|
|
915
|
+
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
|
916
|
+
* `withLevel` is
|
|
917
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
|
918
|
+
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
|
919
|
+
* will not be performed and an empty array will be returned.
|
|
920
|
+
* @param iterationType - The `iterationType` parameter determines the type of iteration to be used
|
|
921
|
+
* in the breadth-first search (BFS) algorithm. It can have two possible values:
|
|
922
|
+
* @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
|
|
838
923
|
*/
|
|
839
924
|
bfs(
|
|
840
925
|
callback: BFSCallback<N> = this._defaultCallbackByKey,
|
|
@@ -870,9 +955,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
870
955
|
}
|
|
871
956
|
|
|
872
957
|
/**
|
|
873
|
-
* The function returns the predecessor of a given node in a binary tree.
|
|
874
|
-
* @param node - The parameter
|
|
875
|
-
* @returns the predecessor of the given node
|
|
958
|
+
* The function returns the predecessor node of a given node in a binary tree.
|
|
959
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree.
|
|
960
|
+
* @returns The function `getPredecessor` returns the predecessor node of the given node `node`.
|
|
876
961
|
*/
|
|
877
962
|
getPredecessor(node: N): N {
|
|
878
963
|
if (node.left) {
|
|
@@ -891,17 +976,24 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
891
976
|
/**
|
|
892
977
|
* Time complexity is O(n)
|
|
893
978
|
* Space complexity of Iterative dfs equals to recursive dfs which is O(n) because of the stack
|
|
894
|
-
*/
|
|
895
|
-
|
|
896
|
-
/**
|
|
897
|
-
* The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris traversal algorithm.
|
|
898
979
|
* The Morris algorithm only modifies the tree's structure during traversal; once the traversal is complete,
|
|
899
980
|
* the tree's structure should be restored to its original state to maintain the tree's integrity.
|
|
900
981
|
* This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
*
|
|
982
|
+
*/
|
|
983
|
+
|
|
984
|
+
/**
|
|
985
|
+
* The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
|
|
986
|
+
* algorithm and returns an array of values obtained by applying a callback function to each node.
|
|
987
|
+
* @param callback - The `callback` parameter is a function that will be called on each node in the
|
|
988
|
+
* tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
|
|
989
|
+
* default value for this parameter is `this._defaultCallbackByKey`.
|
|
990
|
+
* @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
|
|
991
|
+
* determines the order in which the nodes of a binary tree are traversed. It can have one of the
|
|
992
|
+
* following values:
|
|
993
|
+
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
|
|
994
|
+
* traversal. It specifies the root node of the tree from which the traversal should begin. If
|
|
995
|
+
* `beginRoot` is `null`, an empty array will be returned.
|
|
996
|
+
* @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
|
|
905
997
|
*/
|
|
906
998
|
morris(
|
|
907
999
|
callback: MapCallback<N> = this._defaultCallbackByKey,
|
|
@@ -989,14 +1081,15 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
989
1081
|
}
|
|
990
1082
|
|
|
991
1083
|
/**
|
|
992
|
-
* The function adds a new node to a binary tree if there is an available position.
|
|
993
|
-
* @param {N | null} newNode - The `newNode` parameter
|
|
994
|
-
*
|
|
995
|
-
* @param {N} parent - The parent parameter
|
|
996
|
-
*
|
|
997
|
-
*
|
|
998
|
-
*
|
|
999
|
-
* undefined.
|
|
1084
|
+
* The function `_addTo` adds a new node to a binary tree if there is an available position.
|
|
1085
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
|
|
1086
|
+
* the binary tree. It can be either a node object or `null`.
|
|
1087
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
1088
|
+
* be added as a child.
|
|
1089
|
+
* @returns either the left or right child node of the parent node, depending on which child is
|
|
1090
|
+
* available for adding the new node. If a new node is added, the function also updates the size of
|
|
1091
|
+
* the binary tree. If neither the left nor right child is available, the function returns undefined.
|
|
1092
|
+
* If the parent node is null, the function also returns undefined.
|
|
1000
1093
|
*/
|
|
1001
1094
|
protected _addTo(newNode: N | null, parent: N): N | null | undefined {
|
|
1002
1095
|
if (parent) {
|
|
@@ -1023,9 +1116,10 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1023
1116
|
}
|
|
1024
1117
|
|
|
1025
1118
|
/**
|
|
1026
|
-
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1027
|
-
* parent property of the value to undefined.
|
|
1028
|
-
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1119
|
+
* The function sets the root property of an object to a given value, and if the value is not null,
|
|
1120
|
+
* it also sets the parent property of the value to undefined.
|
|
1121
|
+
* @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of
|
|
1122
|
+
* type `N` or `null`.
|
|
1029
1123
|
*/
|
|
1030
1124
|
protected _setRoot(v: N | null) {
|
|
1031
1125
|
if (v) {
|
|
@@ -1035,8 +1129,9 @@ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
|
|
|
1035
1129
|
}
|
|
1036
1130
|
|
|
1037
1131
|
/**
|
|
1038
|
-
* The function sets the
|
|
1039
|
-
* @param {number} v - number
|
|
1132
|
+
* The function sets the value of the protected property "_size" to the given number.
|
|
1133
|
+
* @param {number} v - The parameter "v" is a number that represents the size value that we want to
|
|
1134
|
+
* set.
|
|
1040
1135
|
*/
|
|
1041
1136
|
protected _setSize(v: number) {
|
|
1042
1137
|
this._size = v;
|