data-structure-typed 2.4.0 → 2.4.2
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/CHANGELOG.md +1 -1
- package/README.md +2 -3
- package/README_CN.md +0 -1
- package/dist/cjs/index.cjs +85 -30
- package/dist/cjs-legacy/index.cjs +85 -30
- package/dist/esm/index.mjs +85 -30
- package/dist/esm-legacy/index.mjs +85 -30
- package/dist/types/data-structures/binary-tree/tree-map.d.ts +17 -6
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +13 -5
- package/dist/types/data-structures/binary-tree/tree-multi-set.d.ts +12 -5
- package/dist/types/data-structures/binary-tree/tree-set.d.ts +15 -4
- package/dist/types/types/data-structures/binary-tree/tree-map.d.ts +6 -1
- package/dist/types/types/data-structures/binary-tree/tree-multi-set.d.ts +6 -1
- package/dist/types/types/data-structures/binary-tree/tree-set.d.ts +6 -1
- package/dist/umd/data-structure-typed.js +85 -29
- package/dist/umd/data-structure-typed.min.js +4 -4
- package/package.json +11 -5
- package/src/data-structures/binary-tree/tree-map.ts +35 -13
- package/src/data-structures/binary-tree/tree-multi-map.ts +41 -20
- package/src/data-structures/binary-tree/tree-multi-set.ts +17 -6
- package/src/data-structures/binary-tree/tree-set.ts +19 -6
- package/src/data-structures/trie/trie.ts +6 -8
- package/src/types/data-structures/binary-tree/tree-map.ts +7 -1
- package/src/types/data-structures/binary-tree/tree-multi-set.ts +7 -1
- package/src/types/data-structures/binary-tree/tree-set.ts +7 -1
- package/CMakeLists.txt +0 -52
- package/benchmark/report.html +0 -143
- package/benchmark/report.json +0 -1972
- package/build/CMakeCache.txt +0 -603
- package/build/CMakeFiles/4.0.3/CMakeCXXCompiler.cmake +0 -104
- package/build/CMakeFiles/4.0.3/CMakeDetermineCompilerABI_CXX.bin +0 -0
- package/build/CMakeFiles/4.0.3/CMakeSystem.cmake +0 -15
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -920
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/a.out +0 -0
- package/build/CMakeFiles/4.0.3/CompilerIdCXX/apple-sdk.cpp +0 -1
- package/build/CMakeFiles/CMakeConfigureLog.yaml +0 -2146
- package/build/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/build/CMakeFiles/InstallScripts.json +0 -9
- package/build/CMakeFiles/Makefile.cmake +0 -100
- package/build/CMakeFiles/Makefile2 +0 -720
- package/build/CMakeFiles/TargetDirectories.txt +0 -23
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/test/performance/data-structures/binary-tree/avl-rb-range-search.test.cpp.o +0 -0
- package/build/CMakeFiles/avl-rb-range-search-benchmark.dir/test/performance/data-structures/binary-tree/avl-rb-range-search.test.cpp.o.d +0 -835
- package/build/CMakeFiles/avl-tree-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/avl-tree-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/avl-tree-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/avl-tree-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/avl-tree-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/avl-tree-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/avl-tree-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/avl-tree-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/avl-tree-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/avl-tree-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/avl-tree-benchmark.dir/test/performance/data-structures/binary-tree/avl-tree.test.cpp.o +0 -0
- package/build/CMakeFiles/avl-tree-benchmark.dir/test/performance/data-structures/binary-tree/avl-tree.test.cpp.o.d +0 -835
- package/build/CMakeFiles/cmake.check_cache +0 -1
- package/build/CMakeFiles/deque-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/deque-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/deque-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/deque-benchmark.dir/compiler_depend.internal +0 -791
- package/build/CMakeFiles/deque-benchmark.dir/compiler_depend.make +0 -2362
- package/build/CMakeFiles/deque-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/deque-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/deque-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/deque-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/deque-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/deque-benchmark.dir/test/performance/data-structures/queue/deque.test.cpp.o +0 -0
- package/build/CMakeFiles/deque-benchmark.dir/test/performance/data-structures/queue/deque.test.cpp.o.d +0 -787
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/compiler_depend.internal +0 -790
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/compiler_depend.make +0 -2359
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/test/performance/data-structures/linked-list/doubly-linked-list.test.cpp.o +0 -0
- package/build/CMakeFiles/doubly-linked-list-benchmark.dir/test/performance/data-structures/linked-list/doubly-linked-list.test.cpp.o.d +0 -786
- package/build/CMakeFiles/hash-map-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/hash-map-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/hash-map-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/hash-map-benchmark.dir/compiler_depend.internal +0 -790
- package/build/CMakeFiles/hash-map-benchmark.dir/compiler_depend.make +0 -2359
- package/build/CMakeFiles/hash-map-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/hash-map-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/hash-map-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/hash-map-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/hash-map-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/hash-map-benchmark.dir/test/performance/data-structures/hash/hash-map.test.cpp.o +0 -0
- package/build/CMakeFiles/hash-map-benchmark.dir/test/performance/data-structures/hash/hash-map.test.cpp.o.d +0 -786
- package/build/CMakeFiles/heap-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/heap-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/heap-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/heap-benchmark.dir/compiler_depend.internal +0 -843
- package/build/CMakeFiles/heap-benchmark.dir/compiler_depend.make +0 -2518
- package/build/CMakeFiles/heap-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/heap-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/heap-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/heap-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/heap-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/heap-benchmark.dir/test/performance/data-structures/heap/heap.test.cpp.o +0 -0
- package/build/CMakeFiles/heap-benchmark.dir/test/performance/data-structures/heap/heap.test.cpp.o.d +0 -839
- package/build/CMakeFiles/priority-queue-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/priority-queue-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/priority-queue-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/priority-queue-benchmark.dir/compiler_depend.internal +0 -793
- package/build/CMakeFiles/priority-queue-benchmark.dir/compiler_depend.make +0 -2368
- package/build/CMakeFiles/priority-queue-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/priority-queue-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/priority-queue-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/priority-queue-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/priority-queue-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/priority-queue-benchmark.dir/test/performance/data-structures/priority-queue/priority-queue.test.cpp.o +0 -0
- package/build/CMakeFiles/priority-queue-benchmark.dir/test/performance/data-structures/priority-queue/priority-queue.test.cpp.o.d +0 -789
- package/build/CMakeFiles/progress.marks +0 -1
- package/build/CMakeFiles/queue-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/queue-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/queue-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/queue-benchmark.dir/compiler_depend.internal +0 -793
- package/build/CMakeFiles/queue-benchmark.dir/compiler_depend.make +0 -2368
- package/build/CMakeFiles/queue-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/queue-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/queue-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/queue-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/queue-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/queue-benchmark.dir/test/performance/data-structures/queue/queue.test.cpp.o +0 -0
- package/build/CMakeFiles/queue-benchmark.dir/test/performance/data-structures/queue/queue.test.cpp.o.d +0 -789
- package/build/CMakeFiles/red-black-tree-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/red-black-tree-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/red-black-tree-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/red-black-tree-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/red-black-tree-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/red-black-tree-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/red-black-tree-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/red-black-tree-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/red-black-tree-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/red-black-tree-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/red-black-tree-benchmark.dir/test/performance/data-structures/binary-tree/red-black-tree.test.cpp.o +0 -0
- package/build/CMakeFiles/red-black-tree-benchmark.dir/test/performance/data-structures/binary-tree/red-black-tree.test.cpp.o.d +0 -835
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/compiler_depend.internal +0 -790
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/compiler_depend.make +0 -2359
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/test/performance/data-structures/linked-list/singly-linked-list.test.cpp.o +0 -0
- package/build/CMakeFiles/singly-linked-list-benchmark.dir/test/performance/data-structures/linked-list/singly-linked-list.test.cpp.o.d +0 -786
- package/build/CMakeFiles/stack-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/stack-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/stack-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/stack-benchmark.dir/compiler_depend.internal +0 -793
- package/build/CMakeFiles/stack-benchmark.dir/compiler_depend.make +0 -2368
- package/build/CMakeFiles/stack-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/stack-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/stack-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/stack-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/stack-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/stack-benchmark.dir/test/performance/data-structures/stack/stack.test.cpp.o +0 -0
- package/build/CMakeFiles/stack-benchmark.dir/test/performance/data-structures/stack/stack.test.cpp.o.d +0 -789
- package/build/CMakeFiles/tree-map-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/tree-map-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/tree-map-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/tree-map-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/tree-map-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/tree-map-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/tree-map-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/tree-map-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/tree-map-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/tree-map-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/tree-map-benchmark.dir/test/performance/data-structures/binary-tree/tree-map.test.cpp.o +0 -0
- package/build/CMakeFiles/tree-map-benchmark.dir/test/performance/data-structures/binary-tree/tree-map.test.cpp.o.d +0 -835
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/test/performance/data-structures/binary-tree/tree-multi-map.test.cpp.o +0 -0
- package/build/CMakeFiles/tree-multi-map-benchmark.dir/test/performance/data-structures/binary-tree/tree-multi-map.test.cpp.o.d +0 -835
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/test/performance/data-structures/binary-tree/tree-multi-set.test.cpp.o +0 -0
- package/build/CMakeFiles/tree-multi-set-benchmark.dir/test/performance/data-structures/binary-tree/tree-multi-set.test.cpp.o.d +0 -835
- package/build/CMakeFiles/tree-set-benchmark.dir/DependInfo.cmake +0 -23
- package/build/CMakeFiles/tree-set-benchmark.dir/build.make +0 -114
- package/build/CMakeFiles/tree-set-benchmark.dir/cmake_clean.cmake +0 -11
- package/build/CMakeFiles/tree-set-benchmark.dir/compiler_depend.internal +0 -839
- package/build/CMakeFiles/tree-set-benchmark.dir/compiler_depend.make +0 -2506
- package/build/CMakeFiles/tree-set-benchmark.dir/compiler_depend.ts +0 -2
- package/build/CMakeFiles/tree-set-benchmark.dir/depend.make +0 -2
- package/build/CMakeFiles/tree-set-benchmark.dir/flags.make +0 -10
- package/build/CMakeFiles/tree-set-benchmark.dir/link.txt +0 -1
- package/build/CMakeFiles/tree-set-benchmark.dir/progress.make +0 -3
- package/build/CMakeFiles/tree-set-benchmark.dir/test/performance/data-structures/binary-tree/tree-set.test.cpp.o +0 -0
- package/build/CMakeFiles/tree-set-benchmark.dir/test/performance/data-structures/binary-tree/tree-set.test.cpp.o.d +0 -835
- package/build/Makefile +0 -783
- package/build/_deps/benchmark-build/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/build/_deps/benchmark-build/CMakeFiles/progress.marks +0 -1
- package/build/_deps/benchmark-build/Makefile +0 -140
- package/build/_deps/benchmark-build/benchmark.pc +0 -12
- package/build/_deps/benchmark-build/benchmarkConfig.cmake +0 -14
- package/build/_deps/benchmark-build/benchmarkConfigVersion.cmake +0 -65
- package/build/_deps/benchmark-build/benchmarkTargets.cmake +0 -84
- package/build/_deps/benchmark-build/cmake_install.cmake +0 -50
- package/build/_deps/benchmark-build/src/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/DependInfo.cmake +0 -41
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark.cc.o.d +0 -875
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_api_internal.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_api_internal.cc.o.d +0 -802
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_name.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_name.cc.o.d +0 -772
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_register.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_register.cc.o.d +0 -841
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_runner.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/benchmark_runner.cc.o.d +0 -833
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/build.make +0 -402
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/check.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/check.cc.o.d +0 -784
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/cmake_clean.cmake +0 -47
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/cmake_clean_target.cmake +0 -3
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/colorprint.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/colorprint.cc.o.d +0 -791
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/commandlineflags.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/commandlineflags.cc.o.d +0 -795
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/compiler_depend.internal +0 -15519
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/compiler_depend.make +0 -17500
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/compiler_depend.ts +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/complexity.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/complexity.cc.o.d +0 -793
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/console_reporter.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/console_reporter.cc.o.d +0 -802
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/counter.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/counter.cc.o.d +0 -773
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/csv_reporter.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/csv_reporter.cc.o.d +0 -799
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/depend.make +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/flags.make +0 -10
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/json_reporter.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/json_reporter.cc.o.d +0 -800
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/link.txt +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/perf_counters.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/perf_counters.cc.o.d +0 -807
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/progress.make +0 -21
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/reporter.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/reporter.cc.o.d +0 -798
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/statistics.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/statistics.cc.o.d +0 -800
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/string_util.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/string_util.cc.o.d +0 -795
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/sysinfo.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/sysinfo.cc.o.d +0 -933
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/timers.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark.dir/timers.cc.o.d +0 -904
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/DependInfo.cmake +0 -23
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/benchmark_main.cc.o +0 -0
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/benchmark_main.cc.o.d +0 -772
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/build.make +0 -114
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/cmake_clean.cmake +0 -11
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/cmake_clean_target.cmake +0 -3
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/compiler_depend.internal +0 -776
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/compiler_depend.make +0 -2317
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/compiler_depend.ts +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/depend.make +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/flags.make +0 -10
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/link.txt +0 -2
- package/build/_deps/benchmark-build/src/CMakeFiles/benchmark_main.dir/progress.make +0 -3
- package/build/_deps/benchmark-build/src/CMakeFiles/progress.marks +0 -1
- package/build/_deps/benchmark-build/src/Makefile +0 -710
- package/build/_deps/benchmark-build/src/cmake_install.cmake +0 -45
- package/build/_deps/benchmark-build/src/libbenchmark.a +0 -0
- package/build/_deps/benchmark-build/src/libbenchmark_main.a +0 -0
- package/build/_deps/benchmark-src/.clang-format +0 -5
- package/build/_deps/benchmark-src/.clang-tidy +0 -7
- package/build/_deps/benchmark-src/.github/ISSUE_TEMPLATE/bug_report.md +0 -32
- package/build/_deps/benchmark-src/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/build/_deps/benchmark-src/.github/install_bazel.sh +0 -13
- package/build/_deps/benchmark-src/.github/libcxx-setup.sh +0 -26
- package/build/_deps/benchmark-src/.github/workflows/bazel.yml +0 -35
- package/build/_deps/benchmark-src/.github/workflows/build-and-test-min-cmake.yml +0 -46
- package/build/_deps/benchmark-src/.github/workflows/build-and-test-perfcounters.yml +0 -51
- package/build/_deps/benchmark-src/.github/workflows/build-and-test.yml +0 -114
- package/build/_deps/benchmark-src/.github/workflows/clang-format-lint.yml +0 -17
- package/build/_deps/benchmark-src/.github/workflows/clang-tidy.yml +0 -38
- package/build/_deps/benchmark-src/.github/workflows/doxygen.yml +0 -28
- package/build/_deps/benchmark-src/.github/workflows/pylint.yml +0 -28
- package/build/_deps/benchmark-src/.github/workflows/sanitizer.yml +0 -96
- package/build/_deps/benchmark-src/.github/workflows/test_bindings.yml +0 -29
- package/build/_deps/benchmark-src/.github/workflows/wheels.yml +0 -79
- package/build/_deps/benchmark-src/.travis.yml +0 -208
- package/build/_deps/benchmark-src/.ycm_extra_conf.py +0 -115
- package/build/_deps/benchmark-src/AUTHORS +0 -71
- package/build/_deps/benchmark-src/BUILD.bazel +0 -87
- package/build/_deps/benchmark-src/CMakeLists.txt +0 -340
- package/build/_deps/benchmark-src/CONTRIBUTING.md +0 -58
- package/build/_deps/benchmark-src/CONTRIBUTORS +0 -95
- package/build/_deps/benchmark-src/LICENSE +0 -202
- package/build/_deps/benchmark-src/MODULE.bazel +0 -24
- package/build/_deps/benchmark-src/README.md +0 -223
- package/build/_deps/benchmark-src/WORKSPACE +0 -22
- package/build/_deps/benchmark-src/WORKSPACE.bzlmod +0 -2
- package/build/_deps/benchmark-src/_config.yml +0 -2
- package/build/_deps/benchmark-src/appveyor.yml +0 -50
- package/build/_deps/benchmark-src/bazel/benchmark_deps.bzl +0 -65
- package/build/_deps/benchmark-src/bindings/python/BUILD +0 -3
- package/build/_deps/benchmark-src/bindings/python/build_defs.bzl +0 -25
- package/build/_deps/benchmark-src/bindings/python/google_benchmark/BUILD +0 -40
- package/build/_deps/benchmark-src/bindings/python/google_benchmark/__init__.py +0 -162
- package/build/_deps/benchmark-src/bindings/python/google_benchmark/benchmark.cc +0 -184
- package/build/_deps/benchmark-src/bindings/python/google_benchmark/example.py +0 -136
- package/build/_deps/benchmark-src/bindings/python/nanobind.BUILD +0 -17
- package/build/_deps/benchmark-src/bindings/python/python_headers.BUILD +0 -6
- package/build/_deps/benchmark-src/cmake/AddCXXCompilerFlag.cmake +0 -78
- package/build/_deps/benchmark-src/cmake/CXXFeatureCheck.cmake +0 -82
- package/build/_deps/benchmark-src/cmake/Config.cmake.in +0 -7
- package/build/_deps/benchmark-src/cmake/GetGitVersion.cmake +0 -58
- package/build/_deps/benchmark-src/cmake/GoogleTest.cmake +0 -58
- package/build/_deps/benchmark-src/cmake/GoogleTest.cmake.in +0 -59
- package/build/_deps/benchmark-src/cmake/benchmark.pc.in +0 -12
- package/build/_deps/benchmark-src/cmake/gnu_posix_regex.cpp +0 -12
- package/build/_deps/benchmark-src/cmake/llvm-toolchain.cmake +0 -8
- package/build/_deps/benchmark-src/cmake/posix_regex.cpp +0 -14
- package/build/_deps/benchmark-src/cmake/pthread_affinity.cpp +0 -16
- package/build/_deps/benchmark-src/cmake/split_list.cmake +0 -3
- package/build/_deps/benchmark-src/cmake/std_regex.cpp +0 -10
- package/build/_deps/benchmark-src/cmake/steady_clock.cpp +0 -7
- package/build/_deps/benchmark-src/cmake/thread_safety_attributes.cpp +0 -4
- package/build/_deps/benchmark-src/docs/AssemblyTests.md +0 -149
- package/build/_deps/benchmark-src/docs/_config.yml +0 -3
- package/build/_deps/benchmark-src/docs/assets/images/icon.png +0 -0
- package/build/_deps/benchmark-src/docs/assets/images/icon.xcf +0 -0
- package/build/_deps/benchmark-src/docs/assets/images/icon_black.png +0 -0
- package/build/_deps/benchmark-src/docs/assets/images/icon_black.xcf +0 -0
- package/build/_deps/benchmark-src/docs/dependencies.md +0 -13
- package/build/_deps/benchmark-src/docs/index.md +0 -12
- package/build/_deps/benchmark-src/docs/perf_counters.md +0 -35
- package/build/_deps/benchmark-src/docs/platform_specific_build_instructions.md +0 -48
- package/build/_deps/benchmark-src/docs/python_bindings.md +0 -34
- package/build/_deps/benchmark-src/docs/random_interleaving.md +0 -13
- package/build/_deps/benchmark-src/docs/reducing_variance.md +0 -100
- package/build/_deps/benchmark-src/docs/releasing.md +0 -41
- package/build/_deps/benchmark-src/docs/tools.md +0 -343
- package/build/_deps/benchmark-src/docs/user_guide.md +0 -1266
- package/build/_deps/benchmark-src/include/benchmark/benchmark.h +0 -1991
- package/build/_deps/benchmark-src/include/benchmark/export.h +0 -47
- package/build/_deps/benchmark-src/pyproject.toml +0 -50
- package/build/_deps/benchmark-src/setup.py +0 -113
- package/build/_deps/benchmark-src/src/CMakeLists.txt +0 -170
- package/build/_deps/benchmark-src/src/arraysize.h +0 -33
- package/build/_deps/benchmark-src/src/benchmark.cc +0 -779
- package/build/_deps/benchmark-src/src/benchmark_api_internal.cc +0 -118
- package/build/_deps/benchmark-src/src/benchmark_api_internal.h +0 -87
- package/build/_deps/benchmark-src/src/benchmark_main.cc +0 -18
- package/build/_deps/benchmark-src/src/benchmark_name.cc +0 -59
- package/build/_deps/benchmark-src/src/benchmark_register.cc +0 -520
- package/build/_deps/benchmark-src/src/benchmark_register.h +0 -109
- package/build/_deps/benchmark-src/src/benchmark_runner.cc +0 -497
- package/build/_deps/benchmark-src/src/benchmark_runner.h +0 -131
- package/build/_deps/benchmark-src/src/check.cc +0 -11
- package/build/_deps/benchmark-src/src/check.h +0 -106
- package/build/_deps/benchmark-src/src/colorprint.cc +0 -200
- package/build/_deps/benchmark-src/src/colorprint.h +0 -33
- package/build/_deps/benchmark-src/src/commandlineflags.cc +0 -298
- package/build/_deps/benchmark-src/src/commandlineflags.h +0 -133
- package/build/_deps/benchmark-src/src/complexity.cc +0 -244
- package/build/_deps/benchmark-src/src/complexity.h +0 -55
- package/build/_deps/benchmark-src/src/console_reporter.cc +0 -206
- package/build/_deps/benchmark-src/src/counter.cc +0 -80
- package/build/_deps/benchmark-src/src/counter.h +0 -32
- package/build/_deps/benchmark-src/src/csv_reporter.cc +0 -161
- package/build/_deps/benchmark-src/src/cycleclock.h +0 -230
- package/build/_deps/benchmark-src/src/internal_macros.h +0 -115
- package/build/_deps/benchmark-src/src/json_reporter.cc +0 -320
- package/build/_deps/benchmark-src/src/log.h +0 -88
- package/build/_deps/benchmark-src/src/mutex.h +0 -155
- package/build/_deps/benchmark-src/src/perf_counters.cc +0 -282
- package/build/_deps/benchmark-src/src/perf_counters.h +0 -200
- package/build/_deps/benchmark-src/src/re.h +0 -158
- package/build/_deps/benchmark-src/src/reporter.cc +0 -118
- package/build/_deps/benchmark-src/src/statistics.cc +0 -209
- package/build/_deps/benchmark-src/src/statistics.h +0 -44
- package/build/_deps/benchmark-src/src/string_util.cc +0 -254
- package/build/_deps/benchmark-src/src/string_util.h +0 -70
- package/build/_deps/benchmark-src/src/sysinfo.cc +0 -855
- package/build/_deps/benchmark-src/src/thread_manager.h +0 -63
- package/build/_deps/benchmark-src/src/thread_timer.h +0 -86
- package/build/_deps/benchmark-src/src/timers.cc +0 -272
- package/build/_deps/benchmark-src/src/timers.h +0 -48
- package/build/_deps/benchmark-src/test/AssemblyTests.cmake +0 -67
- package/build/_deps/benchmark-src/test/BUILD +0 -127
- package/build/_deps/benchmark-src/test/CMakeLists.txt +0 -309
- package/build/_deps/benchmark-src/test/args_product_test.cc +0 -77
- package/build/_deps/benchmark-src/test/basic_test.cc +0 -180
- package/build/_deps/benchmark-src/test/benchmark_gtest.cc +0 -169
- package/build/_deps/benchmark-src/test/benchmark_min_time_flag_iters_test.cc +0 -66
- package/build/_deps/benchmark-src/test/benchmark_min_time_flag_time_test.cc +0 -90
- package/build/_deps/benchmark-src/test/benchmark_name_gtest.cc +0 -82
- package/build/_deps/benchmark-src/test/benchmark_random_interleaving_gtest.cc +0 -126
- package/build/_deps/benchmark-src/test/benchmark_setup_teardown_test.cc +0 -157
- package/build/_deps/benchmark-src/test/benchmark_test.cc +0 -274
- package/build/_deps/benchmark-src/test/clobber_memory_assembly_test.cc +0 -64
- package/build/_deps/benchmark-src/test/commandlineflags_gtest.cc +0 -228
- package/build/_deps/benchmark-src/test/complexity_test.cc +0 -228
- package/build/_deps/benchmark-src/test/cxx03_test.cc +0 -62
- package/build/_deps/benchmark-src/test/diagnostics_test.cc +0 -91
- package/build/_deps/benchmark-src/test/display_aggregates_only_test.cc +0 -45
- package/build/_deps/benchmark-src/test/donotoptimize_assembly_test.cc +0 -201
- package/build/_deps/benchmark-src/test/donotoptimize_test.cc +0 -69
- package/build/_deps/benchmark-src/test/filter_test.cc +0 -117
- package/build/_deps/benchmark-src/test/fixture_test.cc +0 -51
- package/build/_deps/benchmark-src/test/internal_threading_test.cc +0 -185
- package/build/_deps/benchmark-src/test/link_main_test.cc +0 -9
- package/build/_deps/benchmark-src/test/map_test.cc +0 -59
- package/build/_deps/benchmark-src/test/memory_manager_test.cc +0 -47
- package/build/_deps/benchmark-src/test/min_time_parse_gtest.cc +0 -30
- package/build/_deps/benchmark-src/test/multiple_ranges_test.cc +0 -96
- package/build/_deps/benchmark-src/test/options_test.cc +0 -77
- package/build/_deps/benchmark-src/test/output_test.h +0 -211
- package/build/_deps/benchmark-src/test/output_test_helper.cc +0 -519
- package/build/_deps/benchmark-src/test/perf_counters_gtest.cc +0 -307
- package/build/_deps/benchmark-src/test/perf_counters_test.cc +0 -92
- package/build/_deps/benchmark-src/test/register_benchmark_test.cc +0 -196
- package/build/_deps/benchmark-src/test/repetitions_test.cc +0 -214
- package/build/_deps/benchmark-src/test/report_aggregates_only_test.cc +0 -41
- package/build/_deps/benchmark-src/test/reporter_output_test.cc +0 -1130
- package/build/_deps/benchmark-src/test/skip_with_error_test.cc +0 -199
- package/build/_deps/benchmark-src/test/spec_arg_test.cc +0 -105
- package/build/_deps/benchmark-src/test/spec_arg_verbosity_test.cc +0 -43
- package/build/_deps/benchmark-src/test/state_assembly_test.cc +0 -68
- package/build/_deps/benchmark-src/test/statistics_gtest.cc +0 -35
- package/build/_deps/benchmark-src/test/string_util_gtest.cc +0 -199
- package/build/_deps/benchmark-src/test/templated_fixture_test.cc +0 -28
- package/build/_deps/benchmark-src/test/time_unit_gtest.cc +0 -37
- package/build/_deps/benchmark-src/test/user_counters_tabular_test.cc +0 -558
- package/build/_deps/benchmark-src/test/user_counters_test.cc +0 -561
- package/build/_deps/benchmark-src/test/user_counters_thousands_test.cc +0 -186
- package/build/_deps/benchmark-src/tools/BUILD.bazel +0 -19
- package/build/_deps/benchmark-src/tools/compare.py +0 -432
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test1_run1.json +0 -127
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test1_run2.json +0 -127
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test2_run.json +0 -81
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test3_run0.json +0 -65
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test3_run1.json +0 -65
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test4_run.json +0 -96
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test4_run0.json +0 -21
- package/build/_deps/benchmark-src/tools/gbench/Inputs/test4_run1.json +0 -21
- package/build/_deps/benchmark-src/tools/gbench/__init__.py +0 -8
- package/build/_deps/benchmark-src/tools/gbench/report.py +0 -1201
- package/build/_deps/benchmark-src/tools/gbench/util.py +0 -203
- package/build/_deps/benchmark-src/tools/libpfm.BUILD.bazel +0 -22
- package/build/_deps/benchmark-src/tools/requirements.txt +0 -2
- package/build/_deps/benchmark-src/tools/strip_asm.py +0 -151
- package/build/_deps/benchmark-subbuild/CMakeCache.txt +0 -133
- package/build/_deps/benchmark-subbuild/CMakeFiles/4.0.3/CMakeSystem.cmake +0 -15
- package/build/_deps/benchmark-subbuild/CMakeFiles/CMakeConfigureLog.yaml +0 -11
- package/build/_deps/benchmark-subbuild/CMakeFiles/CMakeDirectoryInformation.cmake +0 -16
- package/build/_deps/benchmark-subbuild/CMakeFiles/CMakeRuleHashes.txt +0 -11
- package/build/_deps/benchmark-subbuild/CMakeFiles/InstallScripts.json +0 -7
- package/build/_deps/benchmark-subbuild/CMakeFiles/Makefile.cmake +0 -52
- package/build/_deps/benchmark-subbuild/CMakeFiles/Makefile2 +0 -122
- package/build/_deps/benchmark-subbuild/CMakeFiles/TargetDirectories.txt +0 -3
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate-complete +0 -0
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/DependInfo.cmake +0 -22
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/Labels.json +0 -46
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/Labels.txt +0 -14
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/build.make +0 -162
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/cmake_clean.cmake +0 -17
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/compiler_depend.make +0 -2
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/compiler_depend.ts +0 -2
- package/build/_deps/benchmark-subbuild/CMakeFiles/benchmark-populate.dir/progress.make +0 -10
- package/build/_deps/benchmark-subbuild/CMakeFiles/cmake.check_cache +0 -1
- package/build/_deps/benchmark-subbuild/CMakeFiles/progress.marks +0 -1
- package/build/_deps/benchmark-subbuild/CMakeLists.txt +0 -42
- package/build/_deps/benchmark-subbuild/Makefile +0 -162
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-build +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-configure +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-done +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-download +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-gitclone-lastrun.txt +0 -15
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-gitinfo.txt +0 -15
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-install +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-mkdir +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-patch +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-patch-info.txt +0 -6
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-test +0 -0
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/src/benchmark-populate-stamp/benchmark-populate-update-info.txt +0 -7
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/tmp/benchmark-populate-cfgcmd.txt +0 -1
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/tmp/benchmark-populate-gitclone.cmake +0 -87
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/tmp/benchmark-populate-gitupdate.cmake +0 -317
- package/build/_deps/benchmark-subbuild/benchmark-populate-prefix/tmp/benchmark-populate-mkdirs.cmake +0 -27
- package/build/_deps/benchmark-subbuild/cmake_install.cmake +0 -56
- package/build/bin/avl-rb-range-search-benchmark +0 -0
- package/build/bin/avl-tree-benchmark +0 -0
- package/build/bin/deque-benchmark +0 -0
- package/build/bin/doubly-linked-list-benchmark +0 -0
- package/build/bin/hash-map-benchmark +0 -0
- package/build/bin/heap-benchmark +0 -0
- package/build/bin/priority-queue-benchmark +0 -0
- package/build/bin/queue-benchmark +0 -0
- package/build/bin/red-black-tree-benchmark +0 -0
- package/build/bin/singly-linked-list-benchmark +0 -0
- package/build/bin/stack-benchmark +0 -0
- package/build/bin/tree-map-benchmark +0 -0
- package/build/bin/tree-multi-map-benchmark +0 -0
- package/build/bin/tree-multi-set-benchmark +0 -0
- package/build/bin/tree-set-benchmark +0 -0
- package/build/cmake_install.cmake +0 -66
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs-legacy/index.cjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm-legacy/index.mjs.map +0 -1
- package/dist/umd/data-structure-typed.js.map +0 -1
- package/dist/umd/data-structure-typed.min.js.map +0 -1
- package/eslint.config.mjs +0 -73
- package/test/config.ts +0 -4
- package/test/integration/all-in-one.test.ts +0 -110
- package/test/integration/avl-tree.test.ts +0 -127
- package/test/integration/bst.test.ts +0 -385
- package/test/integration/compile.test.mjs +0 -159
- package/test/integration/compile.test.ts +0 -176
- package/test/integration/heap.test.js +0 -21
- package/test/integration/index.html +0 -462
- package/test/performance/benchmark-runner-enhanced.mjs +0 -818
- package/test/performance/cmake-benchmark-runner.mjs +0 -234
- package/test/performance/data-structures/binary-tree/avl-rb-range-search.test.cpp +0 -112
- package/test/performance/data-structures/binary-tree/avl-rb-range-search.test.mjs +0 -51
- package/test/performance/data-structures/binary-tree/avl-tree.test.cpp +0 -181
- package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -138
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.mjs +0 -85
- package/test/performance/data-structures/binary-tree/binary-tree.test.mjs +0 -100
- package/test/performance/data-structures/binary-tree/bst.test.mjs +0 -61
- package/test/performance/data-structures/binary-tree/red-black-tree-cjs.test.mjs +0 -259
- package/test/performance/data-structures/binary-tree/red-black-tree.test.cpp +0 -213
- package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -275
- package/test/performance/data-structures/binary-tree/tree-map.test.cpp +0 -129
- package/test/performance/data-structures/binary-tree/tree-map.test.mjs +0 -171
- package/test/performance/data-structures/binary-tree/tree-multi-map.test.cpp +0 -205
- package/test/performance/data-structures/binary-tree/tree-multi-map.test.mjs +0 -106
- package/test/performance/data-structures/binary-tree/tree-multi-set.test.cpp +0 -217
- package/test/performance/data-structures/binary-tree/tree-multi-set.test.mjs +0 -91
- package/test/performance/data-structures/binary-tree/tree-set.test.cpp +0 -127
- package/test/performance/data-structures/binary-tree/tree-set.test.mjs +0 -174
- package/test/performance/data-structures/comparison/comparison.test.mjs +0 -99
- package/test/performance/data-structures/graph/directed-graph.test.mjs +0 -38
- package/test/performance/data-structures/hash/hash-map.test.cpp +0 -183
- package/test/performance/data-structures/hash/hash-map.test.mjs +0 -187
- package/test/performance/data-structures/heap/heap.test.cpp +0 -61
- package/test/performance/data-structures/heap/heap.test.mjs +0 -31
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.cpp +0 -84
- package/test/performance/data-structures/linked-list/doubly-linked-list.test.mjs +0 -125
- package/test/performance/data-structures/linked-list/singly-linked-list.test.cpp +0 -77
- package/test/performance/data-structures/linked-list/singly-linked-list.test.mjs +0 -84
- package/test/performance/data-structures/priority-queue/max-priority-queue.test.mjs +0 -40
- package/test/performance/data-structures/priority-queue/priority-queue.test.cpp +0 -59
- package/test/performance/data-structures/priority-queue/priority-queue.test.mjs +0 -64
- package/test/performance/data-structures/queue/deque.test.cpp +0 -119
- package/test/performance/data-structures/queue/deque.test.mjs +0 -144
- package/test/performance/data-structures/queue/queue.test.cpp +0 -59
- package/test/performance/data-structures/queue/queue.test.mjs +0 -77
- package/test/performance/data-structures/stack/stack.test.cpp +0 -40
- package/test/performance/data-structures/stack/stack.test.mjs +0 -69
- package/test/performance/data-structures/trie/trie.test.mjs +0 -43
- package/test/performance/reportor-enhanced.mjs +0 -871
- package/test/performance/runner-config.json +0 -51
- package/test/types/index.ts +0 -1
- package/test/types/utils/big-o.ts +0 -1
- package/test/types/utils/index.ts +0 -2
- package/test/types/utils/json2html.ts +0 -5
- package/test/unit/data-structures/base/iterable-element-base.coverage.test.ts +0 -106
- package/test/unit/data-structures/base/iterable-element-base.more-branches.coverage.test.ts +0 -61
- package/test/unit/data-structures/base/linear-base.array.coverage.test.ts +0 -168
- package/test/unit/data-structures/base/linear-base.concat-else.coverage.test.ts +0 -82
- package/test/unit/data-structures/base/linear-base.coverage.test.ts +0 -72
- package/test/unit/data-structures/base/linear-base.more-branches.coverage.test.ts +0 -414
- package/test/unit/data-structures/binary-tree/avl-tree-node.familyPosition-root-left.coverage.test.ts +0 -17
- package/test/unit/data-structures/binary-tree/avl-tree.more-branches-2.coverage.test.ts +0 -99
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -729
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +0 -345
- package/test/unit/data-structures/binary-tree/binary-indexed-tree.more-branches.coverage.test.ts +0 -18
- package/test/unit/data-structures/binary-tree/binary-tree.more-branches.coverage.test.ts +0 -56
- package/test/unit/data-structures/binary-tree/binary-tree.remaining-branches.coverage.test.ts +0 -232
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -1912
- package/test/unit/data-structures/binary-tree/bst.bound-by-predicate.coverage.test.ts +0 -33
- package/test/unit/data-structures/binary-tree/bst.coverage.test.ts +0 -94
- package/test/unit/data-structures/binary-tree/bst.deletebykey.coverage.test.ts +0 -70
- package/test/unit/data-structures/binary-tree/bst.deletewhere.coverage.test.ts +0 -37
- package/test/unit/data-structures/binary-tree/bst.floor-lower-predicate.coverage.test.ts +0 -29
- package/test/unit/data-structures/binary-tree/bst.floor-setmany.coverage.test.ts +0 -72
- package/test/unit/data-structures/binary-tree/bst.getnode.range-ensure.coverage.test.ts +0 -22
- package/test/unit/data-structures/binary-tree/bst.misc-branches.coverage.test.ts +0 -100
- package/test/unit/data-structures/binary-tree/bst.more-branches-2.coverage.test.ts +0 -133
- package/test/unit/data-structures/binary-tree/bst.more-branches-3.coverage.test.ts +0 -45
- package/test/unit/data-structures/binary-tree/bst.more-branches-4.coverage.test.ts +0 -36
- package/test/unit/data-structures/binary-tree/bst.more-branches-5.coverage.test.ts +0 -40
- package/test/unit/data-structures/binary-tree/bst.more.coverage.test.ts +0 -39
- package/test/unit/data-structures/binary-tree/bst.node-family.coverage.test.ts +0 -29
- package/test/unit/data-structures/binary-tree/bst.range-pruning.coverage.test.ts +0 -43
- package/test/unit/data-structures/binary-tree/bst.search-fastpath.coverage.test.ts +0 -30
- package/test/unit/data-structures/binary-tree/bst.test.ts +0 -3472
- package/test/unit/data-structures/binary-tree/data/cost-of-living-by-country.ts +0 -259
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -219
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-corruption-repair.coverage.test.ts +0 -66
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-max-update.coverage.test.ts +0 -18
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-null.coverage.test.ts +0 -53
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-stale-cache.coverage.test.ts +0 -25
- package/test/unit/data-structures/binary-tree/red-black-tree.boundary-update.coverage.test.ts +0 -23
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-delete.coverage.test.ts +0 -49
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-edge.coverage.test.ts +0 -37
- package/test/unit/data-structures/binary-tree/red-black-tree.cache-stale-insert.coverage.test.ts +0 -39
- package/test/unit/data-structures/binary-tree/red-black-tree.coverage.test.ts +0 -334
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-fixup.coverage.test.ts +0 -68
- package/test/unit/data-structures/binary-tree/red-black-tree.delete-successor.coverage.test.ts +0 -75
- package/test/unit/data-structures/binary-tree/red-black-tree.factories.coverage.test.ts +0 -26
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-compare-update.coverage.test.ts +0 -74
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-no-update.coverage.test.ts +0 -44
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-cache-nullish.coverage.test.ts +0 -61
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-defined.coverage.test.ts +0 -35
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-mapmode-undefined.coverage.test.ts +0 -43
- package/test/unit/data-structures/binary-tree/red-black-tree.hint-more.coverage.test.ts +0 -99
- package/test/unit/data-structures/binary-tree/red-black-tree.hint.coverage.test.ts +0 -60
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-cache-nullish.coverage.test.ts +0 -29
- package/test/unit/data-structures/binary-tree/red-black-tree.insert-header-parent-nullish.coverage.test.ts +0 -17
- package/test/unit/data-structures/binary-tree/red-black-tree.internal-walk.coverage.test.ts +0 -57
- package/test/unit/data-structures/binary-tree/red-black-tree.minmax-cache.test.ts +0 -65
- package/test/unit/data-structures/binary-tree/red-black-tree.misc-inputs.coverage.test.ts +0 -17
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-2.coverage.test.ts +0 -121
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-3.coverage.test.ts +0 -55
- package/test/unit/data-structures/binary-tree/red-black-tree.more-branches-4.coverage.test.ts +0 -44
- package/test/unit/data-structures/binary-tree/red-black-tree.predsucc.coverage.test.ts +0 -40
- package/test/unit/data-structures/binary-tree/red-black-tree.remaining-branches.coverage.test.ts +0 -123
- package/test/unit/data-structures/binary-tree/red-black-tree.set-inputs.coverage.test.ts +0 -64
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-parent-cache.coverage.test.ts +0 -79
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-remaining.coverage.test.ts +0 -44
- package/test/unit/data-structures/binary-tree/red-black-tree.setkvnode-uncovered.coverage.test.ts +0 -74
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +0 -936
- package/test/unit/data-structures/binary-tree/red-black-tree.update-branches.coverage.test.ts +0 -30
- package/test/unit/data-structures/binary-tree/segment-tree.more-branches.coverage.test.ts +0 -31
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -104
- package/test/unit/data-structures/binary-tree/tree-map.test.ts +0 -270
- package/test/unit/data-structures/binary-tree/tree-multi-map.coverage.test.ts +0 -245
- package/test/unit/data-structures/binary-tree/tree-multi-map.legacy.test.ts.skip +0 -1115
- package/test/unit/data-structures/binary-tree/tree-multi-map.more-branches-2.coverage.test.ts.skip +0 -59
- package/test/unit/data-structures/binary-tree/tree-multi-map.rfc.test.ts +0 -99
- package/test/unit/data-structures/binary-tree/tree-multi-map.simplified.test.ts +0 -482
- package/test/unit/data-structures/binary-tree/tree-multi-set.test.ts +0 -545
- package/test/unit/data-structures/binary-tree/tree-set.test.ts +0 -187
- package/test/unit/data-structures/graph/abstract-graph.more-branches-2.coverage.test.ts +0 -40
- package/test/unit/data-structures/graph/abstract-graph.more-branches-3.coverage.test.ts +0 -65
- package/test/unit/data-structures/graph/abstract-graph.more-branches-4.coverage.test.ts +0 -98
- package/test/unit/data-structures/graph/abstract-graph.more-branches-5.coverage.test.ts +0 -51
- package/test/unit/data-structures/graph/abstract-graph.more-branches.coverage.test.ts +0 -62
- package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -117
- package/test/unit/data-structures/graph/directed-graph.more-branches-2.coverage.test.ts +0 -38
- package/test/unit/data-structures/graph/directed-graph.more-branches-3.coverage.test.ts +0 -25
- package/test/unit/data-structures/graph/directed-graph.more-branches.coverage.test.ts +0 -82
- package/test/unit/data-structures/graph/directed-graph.test.ts +0 -1185
- package/test/unit/data-structures/graph/map-graph.more-branches.coverage.test.ts +0 -22
- package/test/unit/data-structures/graph/map-graph.test.ts +0 -148
- package/test/unit/data-structures/graph/overall.test.ts +0 -49
- package/test/unit/data-structures/graph/salty-edges.json +0 -875
- package/test/unit/data-structures/graph/salty-vertexes.json +0 -200
- package/test/unit/data-structures/graph/undirected-graph.more-branches-2.coverage.test.ts +0 -35
- package/test/unit/data-structures/graph/undirected-graph.more-branches.coverage.test.ts +0 -87
- package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -782
- package/test/unit/data-structures/hash/hash-map.more-branches.coverage.test.ts +0 -64
- package/test/unit/data-structures/hash/hash-map.test.ts +0 -1125
- package/test/unit/data-structures/hash/hash-map.toEntryFn-branch.coverage.test.ts +0 -9
- package/test/unit/data-structures/heap/heap.misc-branches.coverage.test.ts +0 -110
- package/test/unit/data-structures/heap/heap.remaining-branches.coverage.test.ts +0 -22
- package/test/unit/data-structures/heap/heap.test.ts +0 -760
- package/test/unit/data-structures/heap/max-heap.coverage.test.ts +0 -29
- package/test/unit/data-structures/heap/max-heap.test.ts +0 -98
- package/test/unit/data-structures/heap/min-heap.test.ts +0 -117
- package/test/unit/data-structures/linked-list/doubly-linked-list.more-branches.coverage.test.ts +0 -72
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -1150
- package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -8
- package/test/unit/data-structures/linked-list/linked-list.unshiftMany-else.coverage.test.ts +0 -15
- package/test/unit/data-structures/linked-list/singly-linked-list.coverage.test.ts +0 -221
- package/test/unit/data-structures/linked-list/singly-linked-list.more-branches.coverage.test.ts +0 -86
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -836
- package/test/unit/data-structures/linked-list/skip-linked-list.more-branches.coverage.test.ts +0 -31
- package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
- package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -86
- package/test/unit/data-structures/matrix/matrix.more-branches.coverage.test.ts +0 -81
- package/test/unit/data-structures/matrix/matrix.pivotElement-nullish.coverage.test.ts +0 -28
- package/test/unit/data-structures/matrix/matrix.test.ts +0 -377
- package/test/unit/data-structures/matrix/navigator.test.ts +0 -244
- package/test/unit/data-structures/priority-queue/max-priority-queue.more-branches.coverage.test.ts +0 -10
- package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -160
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -78
- package/test/unit/data-structures/priority-queue/priority-queue.coverage.test.ts +0 -21
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -101
- package/test/unit/data-structures/queue/deque.coverage.test.ts +0 -173
- package/test/unit/data-structures/queue/deque.more-branches-2.coverage.test.ts +0 -39
- package/test/unit/data-structures/queue/deque.more-branches-3.coverage.test.ts +0 -9
- package/test/unit/data-structures/queue/deque.more-branches.coverage.test.ts +0 -95
- package/test/unit/data-structures/queue/deque.test.ts +0 -936
- package/test/unit/data-structures/queue/queue.coverage.test.ts +0 -138
- package/test/unit/data-structures/queue/queue.more-branches-2.coverage.test.ts +0 -27
- package/test/unit/data-structures/queue/queue.test.ts +0 -703
- package/test/unit/data-structures/stack/stack.coverage.test.ts +0 -112
- package/test/unit/data-structures/stack/stack.test.ts +0 -438
- package/test/unit/data-structures/tree/tree.more-branches.coverage.test.ts +0 -9
- package/test/unit/data-structures/tree/tree.test.ts +0 -58
- package/test/unit/data-structures/trie/trie.more-branches-2.coverage.test.ts +0 -51
- package/test/unit/data-structures/trie/trie.test.ts +0 -1181
- package/test/unit/unrestricted-interconversion.test.ts +0 -299
- package/test/unit/utils/utils.test.ts +0 -174
- package/test/utils/array.ts +0 -5517
- package/test/utils/big-o.ts +0 -228
- package/test/utils/console.ts +0 -31
- package/test/utils/index.ts +0 -8
- package/test/utils/is.ts +0 -56
- package/test/utils/json2html.ts +0 -164
- package/test/utils/number.ts +0 -13
- package/test/utils/patch.ts +0 -33
- package/test/utils/perf.mjs +0 -34
- package/test/utils/performanc.ts +0 -7
- package/test/utils/string.ts +0 -49
- package/tsconfig.types.json +0 -15
- package/tsup.config.js +0 -63
- package/tsup.umd.config.js +0 -29
|
@@ -1,3472 +0,0 @@
|
|
|
1
|
-
import { BST, BSTNode, IBinaryTree, Range } from '../../../../src';
|
|
2
|
-
import { isDebugTest, isTestStackOverflow, SYSTEM_MAX_CALL_STACK } from '../../../config';
|
|
3
|
-
import { withMutedConsole } from '../../../utils/patch';
|
|
4
|
-
|
|
5
|
-
const isDebug = isDebugTest;
|
|
6
|
-
|
|
7
|
-
function expectSingleDeleteResult<K, _V>(removed: any, expectedKey: K) {
|
|
8
|
-
// Most tree delete APIs here return an array of { deleted, ... }.
|
|
9
|
-
expect(removed).toBeInstanceOf(Array);
|
|
10
|
-
expect(removed[0]).toBeDefined();
|
|
11
|
-
expect(removed[0].deleted).toBeDefined();
|
|
12
|
-
if (removed[0].deleted) expect(removed[0].deleted.key).toBe(expectedKey);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
describe('BST operations test', () => {
|
|
16
|
-
it('should add undefined and null', () => {
|
|
17
|
-
const bst = new BST<number, string>();
|
|
18
|
-
const isAddUndefined = bst.set(undefined);
|
|
19
|
-
expect(isAddUndefined).toBe(false);
|
|
20
|
-
expect(bst.get(undefined)).toBe(undefined);
|
|
21
|
-
const isAddNull = bst.set(null);
|
|
22
|
-
expect(isAddNull).toBe(false);
|
|
23
|
-
expect(bst.get(null)).toBe(undefined);
|
|
24
|
-
const isAdd0 = bst.set(0, '0');
|
|
25
|
-
expect(isAdd0).toBe(true);
|
|
26
|
-
expect(bst.get(0)).toBe('0');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('should addMany undefined and null', () => {
|
|
30
|
-
const bst = new BST<number, string>();
|
|
31
|
-
const addManyWithUndefined = bst.setMany([1, undefined, 3]);
|
|
32
|
-
expect(addManyWithUndefined).toEqual([true, false, true]);
|
|
33
|
-
expect(bst.get(undefined)).toBe(undefined);
|
|
34
|
-
const addManyWithNull = bst.setMany([1, null, 3, 4]);
|
|
35
|
-
expect(addManyWithNull).toEqual([true, false, true, true]);
|
|
36
|
-
const addManyEntriesWithNull = bst.setMany([
|
|
37
|
-
[1, '1'],
|
|
38
|
-
[null, 'null'],
|
|
39
|
-
[3, '3'],
|
|
40
|
-
[4, '4']
|
|
41
|
-
]);
|
|
42
|
-
expect(addManyEntriesWithNull).toEqual([true, false, true, true]);
|
|
43
|
-
expect(bst.get(null)).toBe(undefined);
|
|
44
|
-
const node0 = bst.set(0, '0');
|
|
45
|
-
expect(node0).toBe(true);
|
|
46
|
-
expect(bst.get(0)).toBe('0');
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
50
|
-
const bst = new BST<number, number>();
|
|
51
|
-
expect(bst).toBeInstanceOf(BST);
|
|
52
|
-
bst.set([11, 11]);
|
|
53
|
-
bst.set([3, 3]);
|
|
54
|
-
const idsAndValues: [number, number][] = [
|
|
55
|
-
[15, 15],
|
|
56
|
-
[1, 1],
|
|
57
|
-
[8, 8],
|
|
58
|
-
[13, 13],
|
|
59
|
-
[16, 16],
|
|
60
|
-
[2, 2],
|
|
61
|
-
[6, 6],
|
|
62
|
-
[9, 9],
|
|
63
|
-
[12, 12],
|
|
64
|
-
[14, 14],
|
|
65
|
-
[4, 4],
|
|
66
|
-
[7, 7],
|
|
67
|
-
[10, 10],
|
|
68
|
-
[5, 5]
|
|
69
|
-
];
|
|
70
|
-
bst.setMany(idsAndValues, [], false);
|
|
71
|
-
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
72
|
-
|
|
73
|
-
if (bst.root) expect(bst.root.key).toBe(11);
|
|
74
|
-
|
|
75
|
-
expect(bst.size).toBe(16);
|
|
76
|
-
|
|
77
|
-
expect(bst.has(6)).toBe(true);
|
|
78
|
-
|
|
79
|
-
const node6 = bst.getNode(6);
|
|
80
|
-
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
81
|
-
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
82
|
-
|
|
83
|
-
const nodeId10 = bst.getNode(10);
|
|
84
|
-
expect(nodeId10?.key).toBe(10);
|
|
85
|
-
|
|
86
|
-
const nodeVal9 = bst.getNode(node => node.key === 9);
|
|
87
|
-
expect(nodeVal9?.key).toBe(9);
|
|
88
|
-
|
|
89
|
-
const leftMost = bst.getLeftMost();
|
|
90
|
-
expect(leftMost).toBe(1);
|
|
91
|
-
|
|
92
|
-
expect(bst.isBST()).toBe(true);
|
|
93
|
-
|
|
94
|
-
const node15 = bst.getNode(15);
|
|
95
|
-
const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15);
|
|
96
|
-
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
97
|
-
|
|
98
|
-
const nodes = bst.getNodes(node => node.key === 15);
|
|
99
|
-
expect(nodes.map(node => node.key)).toEqual([15]);
|
|
100
|
-
|
|
101
|
-
let subTreeSum = 0;
|
|
102
|
-
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
103
|
-
expect(subTreeSum).toBe(70);
|
|
104
|
-
|
|
105
|
-
let lesserSum = 0;
|
|
106
|
-
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
107
|
-
expect(lesserSum).toBe(45);
|
|
108
|
-
|
|
109
|
-
expect(node15).toBeInstanceOf(BSTNode);
|
|
110
|
-
|
|
111
|
-
const node11 = bst.getNode(11);
|
|
112
|
-
expect(node11).toBeInstanceOf(BSTNode);
|
|
113
|
-
|
|
114
|
-
const dfsInorderNodes = bst.dfs(node => node, 'IN');
|
|
115
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
116
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
117
|
-
|
|
118
|
-
bst.perfectlyBalance();
|
|
119
|
-
expect(bst.isPerfectlyBalanced()).toBe(true);
|
|
120
|
-
|
|
121
|
-
const bfsNodesAfterBalanced: BSTNode<number>[] = [];
|
|
122
|
-
bst.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
123
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
124
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
125
|
-
|
|
126
|
-
const removed11 = bst.delete(11);
|
|
127
|
-
expectSingleDeleteResult(removed11, 11);
|
|
128
|
-
|
|
129
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
130
|
-
|
|
131
|
-
expect(bst.getHeight(15)).toBe(1);
|
|
132
|
-
|
|
133
|
-
const removed1 = bst.delete(1);
|
|
134
|
-
expectSingleDeleteResult(removed1, 1);
|
|
135
|
-
|
|
136
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
137
|
-
|
|
138
|
-
expect(bst.getHeight()).toBe(4);
|
|
139
|
-
|
|
140
|
-
const removed4 = bst.delete(4);
|
|
141
|
-
expectSingleDeleteResult(removed4, 4);
|
|
142
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
143
|
-
expect(bst.getHeight()).toBe(4);
|
|
144
|
-
|
|
145
|
-
const removed10 = bst.delete(10);
|
|
146
|
-
expectSingleDeleteResult(removed10, 10);
|
|
147
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
148
|
-
expect(bst.getHeight()).toBe(4);
|
|
149
|
-
|
|
150
|
-
const removed15 = bst.delete(15);
|
|
151
|
-
expectSingleDeleteResult(removed15, 15);
|
|
152
|
-
|
|
153
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
154
|
-
expect(bst.getHeight()).toBe(3);
|
|
155
|
-
|
|
156
|
-
const removed5 = bst.delete(5);
|
|
157
|
-
expectSingleDeleteResult(removed5, 5);
|
|
158
|
-
|
|
159
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
160
|
-
expect(bst.getHeight()).toBe(3);
|
|
161
|
-
|
|
162
|
-
const removed13 = bst.delete(13);
|
|
163
|
-
expectSingleDeleteResult(removed13, 13);
|
|
164
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
165
|
-
expect(bst.getHeight()).toBe(3);
|
|
166
|
-
|
|
167
|
-
const removed3 = bst.delete(3);
|
|
168
|
-
expectSingleDeleteResult(removed3, 3);
|
|
169
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
170
|
-
expect(bst.getHeight()).toBe(3);
|
|
171
|
-
|
|
172
|
-
const removed8 = bst.delete(8);
|
|
173
|
-
expectSingleDeleteResult(removed8, 8);
|
|
174
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
175
|
-
expect(bst.getHeight()).toBe(3);
|
|
176
|
-
|
|
177
|
-
const removed6 = bst.delete(6);
|
|
178
|
-
expectSingleDeleteResult(removed6, 6);
|
|
179
|
-
expect(bst.delete(6).length).toBe(0);
|
|
180
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
181
|
-
expect(bst.getHeight()).toBe(3);
|
|
182
|
-
|
|
183
|
-
const removed7 = bst.delete(7);
|
|
184
|
-
expectSingleDeleteResult(removed7, 7);
|
|
185
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
186
|
-
expect(bst.getHeight()).toBe(3);
|
|
187
|
-
|
|
188
|
-
const removed9 = bst.delete(9);
|
|
189
|
-
expectSingleDeleteResult(removed9, 9);
|
|
190
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
191
|
-
expect(bst.getHeight()).toBe(3);
|
|
192
|
-
|
|
193
|
-
const removed14 = bst.delete(14);
|
|
194
|
-
expectSingleDeleteResult(removed14, 14);
|
|
195
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
196
|
-
expect(bst.getHeight()).toBe(2);
|
|
197
|
-
|
|
198
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
199
|
-
|
|
200
|
-
const bfsIDs: number[] = [];
|
|
201
|
-
bst.bfs(node => bfsIDs.push(node.key));
|
|
202
|
-
expect(bfsIDs[0]).toBe(2);
|
|
203
|
-
expect(bfsIDs[1]).toBe(12);
|
|
204
|
-
expect(bfsIDs[2]).toBe(16);
|
|
205
|
-
|
|
206
|
-
const bfsNodes: BSTNode<number>[] = [];
|
|
207
|
-
bst.bfs(node => bfsNodes.push(node));
|
|
208
|
-
expect(bfsNodes[0].key).toBe(2);
|
|
209
|
-
expect(bfsNodes[1].key).toBe(12);
|
|
210
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
211
|
-
bst.clear();
|
|
212
|
-
expect(bst.perfectlyBalance()).toBe(false);
|
|
213
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
217
|
-
const objBST = new BST<number, { name: string; age: number }>();
|
|
218
|
-
expect(objBST).toBeInstanceOf(BST);
|
|
219
|
-
objBST.set([11, { name: '11', age: 11 }]);
|
|
220
|
-
objBST.set([3, { name: '3', age: 3 }]);
|
|
221
|
-
|
|
222
|
-
objBST.setMany(
|
|
223
|
-
[15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5],
|
|
224
|
-
[
|
|
225
|
-
{ name: 'Alice', age: 15 },
|
|
226
|
-
{ name: 'Bob', age: 1 },
|
|
227
|
-
{ name: 'Charlie', age: 8 },
|
|
228
|
-
{ name: 'David', age: 13 },
|
|
229
|
-
{ name: 'Emma', age: 16 },
|
|
230
|
-
{ name: 'Frank', age: 2 },
|
|
231
|
-
{ name: 'Grace', age: 6 },
|
|
232
|
-
{ name: 'Hannah', age: 9 },
|
|
233
|
-
{ name: 'Isaac', age: 12 },
|
|
234
|
-
{ name: 'Jack', age: 14 },
|
|
235
|
-
{ name: 'Katie', age: 4 },
|
|
236
|
-
{ name: 'Liam', age: 7 },
|
|
237
|
-
{ name: 'Mia', age: 10 },
|
|
238
|
-
{ name: 'Noah', age: 5 }
|
|
239
|
-
],
|
|
240
|
-
false
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
244
|
-
|
|
245
|
-
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
246
|
-
|
|
247
|
-
expect(objBST.has(6)).toBe(true);
|
|
248
|
-
|
|
249
|
-
const node6 = objBST.getNode(6);
|
|
250
|
-
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
251
|
-
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
252
|
-
|
|
253
|
-
const nodeId10 = objBST.getNode(10);
|
|
254
|
-
expect(nodeId10?.key).toBe(10);
|
|
255
|
-
|
|
256
|
-
const nodeVal9 = objBST.getNode(9);
|
|
257
|
-
expect(nodeVal9?.key).toBe(9);
|
|
258
|
-
|
|
259
|
-
const leftMost = objBST.getLeftMost();
|
|
260
|
-
expect(leftMost).toBe(1);
|
|
261
|
-
|
|
262
|
-
const node15 = objBST.getNode(15);
|
|
263
|
-
expect(objBST.get(node15)).toEqual({
|
|
264
|
-
name: 'Alice',
|
|
265
|
-
age: 15
|
|
266
|
-
});
|
|
267
|
-
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node => node, node15);
|
|
268
|
-
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
269
|
-
|
|
270
|
-
let subTreeSum = 0;
|
|
271
|
-
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', false, node15);
|
|
272
|
-
expect(subTreeSum).toBe(70);
|
|
273
|
-
|
|
274
|
-
let lesserSum = 0;
|
|
275
|
-
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
276
|
-
expect(lesserSum).toBe(45);
|
|
277
|
-
|
|
278
|
-
expect(node15).toBeInstanceOf(BSTNode);
|
|
279
|
-
|
|
280
|
-
const node11 = objBST.getNode(11);
|
|
281
|
-
expect(node11).toBeInstanceOf(BSTNode);
|
|
282
|
-
|
|
283
|
-
const dfsInorderNodes = objBST.dfs(node => node, 'IN');
|
|
284
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
285
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
286
|
-
|
|
287
|
-
objBST.perfectlyBalance();
|
|
288
|
-
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
289
|
-
|
|
290
|
-
const bfsNodesAfterBalanced: BSTNode<number, { name: string; age: number }>[] = [];
|
|
291
|
-
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
292
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
293
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
294
|
-
|
|
295
|
-
const removed11 = objBST.delete(11);
|
|
296
|
-
expect(removed11).toBeInstanceOf(Array);
|
|
297
|
-
expect(removed11[0]).toBeDefined();
|
|
298
|
-
expect(removed11[0].deleted).toBeDefined();
|
|
299
|
-
|
|
300
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
|
301
|
-
|
|
302
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
303
|
-
expect(node15 && objBST.getHeight(node15)).toBe(1);
|
|
304
|
-
|
|
305
|
-
const removed1 = objBST.delete(1);
|
|
306
|
-
expect(removed1).toBeInstanceOf(Array);
|
|
307
|
-
expect(removed1[0]).toBeDefined();
|
|
308
|
-
expect(removed1[0].deleted).toBeDefined();
|
|
309
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
|
310
|
-
|
|
311
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
312
|
-
|
|
313
|
-
expect(objBST.getHeight()).toBe(4);
|
|
314
|
-
|
|
315
|
-
const removed4 = objBST.delete(4);
|
|
316
|
-
expect(removed4).toBeInstanceOf(Array);
|
|
317
|
-
expect(removed4[0]).toBeDefined();
|
|
318
|
-
expect(removed4[0].deleted).toBeDefined();
|
|
319
|
-
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
|
320
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
321
|
-
expect(objBST.getHeight()).toBe(4);
|
|
322
|
-
|
|
323
|
-
const removed10 = objBST.delete(10);
|
|
324
|
-
expect(removed10).toBeInstanceOf(Array);
|
|
325
|
-
expect(removed10[0]).toBeDefined();
|
|
326
|
-
expect(removed10[0].deleted).toBeDefined();
|
|
327
|
-
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
|
328
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
329
|
-
expect(objBST.getHeight()).toBe(4);
|
|
330
|
-
|
|
331
|
-
const removed15 = objBST.delete(15);
|
|
332
|
-
expect(removed15).toBeInstanceOf(Array);
|
|
333
|
-
expect(removed15[0]).toBeDefined();
|
|
334
|
-
expect(removed15[0].deleted).toBeDefined();
|
|
335
|
-
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
|
336
|
-
|
|
337
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
338
|
-
expect(objBST.getHeight()).toBe(3);
|
|
339
|
-
|
|
340
|
-
const removed5 = objBST.delete(5);
|
|
341
|
-
expect(removed5).toBeInstanceOf(Array);
|
|
342
|
-
expect(removed5[0]).toBeDefined();
|
|
343
|
-
expect(removed5[0].deleted).toBeDefined();
|
|
344
|
-
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
|
345
|
-
|
|
346
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
347
|
-
expect(objBST.getHeight()).toBe(3);
|
|
348
|
-
|
|
349
|
-
const removed13 = objBST.delete(13);
|
|
350
|
-
expect(removed13).toBeInstanceOf(Array);
|
|
351
|
-
expect(removed13[0]).toBeDefined();
|
|
352
|
-
expect(removed13[0].deleted).toBeDefined();
|
|
353
|
-
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
|
354
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
355
|
-
expect(objBST.getHeight()).toBe(3);
|
|
356
|
-
|
|
357
|
-
const removed3 = objBST.delete(3);
|
|
358
|
-
expect(removed3).toBeInstanceOf(Array);
|
|
359
|
-
expect(removed3[0]).toBeDefined();
|
|
360
|
-
expect(removed3[0].deleted).toBeDefined();
|
|
361
|
-
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
|
362
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
363
|
-
expect(objBST.getHeight()).toBe(3);
|
|
364
|
-
|
|
365
|
-
const removed8 = objBST.delete(8);
|
|
366
|
-
expect(removed8).toBeInstanceOf(Array);
|
|
367
|
-
expect(removed8[0]).toBeDefined();
|
|
368
|
-
expect(removed8[0].deleted).toBeDefined();
|
|
369
|
-
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
|
370
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
371
|
-
expect(objBST.getHeight()).toBe(3);
|
|
372
|
-
|
|
373
|
-
const removed6 = objBST.delete(6);
|
|
374
|
-
expect(removed6).toBeInstanceOf(Array);
|
|
375
|
-
expect(removed6[0]).toBeDefined();
|
|
376
|
-
expect(removed6[0].deleted).toBeDefined();
|
|
377
|
-
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
|
378
|
-
expect(objBST.delete(6).length).toBe(0);
|
|
379
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
380
|
-
expect(objBST.getHeight()).toBe(3);
|
|
381
|
-
|
|
382
|
-
const removed7 = objBST.delete(7);
|
|
383
|
-
expect(removed7).toBeInstanceOf(Array);
|
|
384
|
-
expect(removed7[0]).toBeDefined();
|
|
385
|
-
expect(removed7[0].deleted).toBeDefined();
|
|
386
|
-
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
|
387
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
388
|
-
expect(objBST.getHeight()).toBe(3);
|
|
389
|
-
|
|
390
|
-
const removed9 = objBST.delete(9);
|
|
391
|
-
expect(removed9).toBeInstanceOf(Array);
|
|
392
|
-
expect(removed9[0]).toBeDefined();
|
|
393
|
-
expect(removed9[0].deleted).toBeDefined();
|
|
394
|
-
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
|
395
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
396
|
-
expect(objBST.getHeight()).toBe(3);
|
|
397
|
-
|
|
398
|
-
const removed14 = objBST.delete(14);
|
|
399
|
-
expect(removed14).toBeInstanceOf(Array);
|
|
400
|
-
expect(removed14[0]).toBeDefined();
|
|
401
|
-
expect(removed14[0].deleted).toBeDefined();
|
|
402
|
-
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
|
403
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
404
|
-
expect(objBST.getHeight()).toBe(2);
|
|
405
|
-
|
|
406
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
407
|
-
|
|
408
|
-
const bfsIDs: number[] = [];
|
|
409
|
-
objBST.bfs(node => bfsIDs.push(node.key));
|
|
410
|
-
expect(bfsIDs[0]).toBe(2);
|
|
411
|
-
expect(bfsIDs[1]).toBe(12);
|
|
412
|
-
expect(bfsIDs[2]).toBe(16);
|
|
413
|
-
|
|
414
|
-
const bfsNodes: BSTNode<number, { name: string; age: number }>[] = [];
|
|
415
|
-
objBST.bfs(node => bfsNodes.push(node));
|
|
416
|
-
expect(bfsNodes[0].key).toBe(2);
|
|
417
|
-
expect(bfsNodes[1].key).toBe(12);
|
|
418
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
// it('should keyValueNodeEntryRawToNodeAndValue', () => {
|
|
422
|
-
// const bst = new BST<number>();
|
|
423
|
-
// const node0 = bst.keyValueNodeEntryRawToNodeAndValue(0);
|
|
424
|
-
// expect(node0).toEqual([
|
|
425
|
-
// {
|
|
426
|
-
// _left: undefined,
|
|
427
|
-
// _right: undefined,
|
|
428
|
-
// key: 0,
|
|
429
|
-
// parent: undefined,
|
|
430
|
-
// value: undefined
|
|
431
|
-
// },
|
|
432
|
-
// undefined
|
|
433
|
-
// ]);
|
|
434
|
-
//
|
|
435
|
-
// const nodeUndefined = bst.keyValueNodeEntryRawToNodeAndValue(undefined);
|
|
436
|
-
// expect(nodeUndefined).toEqual([undefined, undefined]);
|
|
437
|
-
//
|
|
438
|
-
// const nodeNull = bst.keyValueNodeEntryRawToNodeAndValue(null);
|
|
439
|
-
// expect(nodeNull).toEqual([undefined, undefined]);
|
|
440
|
-
// });
|
|
441
|
-
|
|
442
|
-
it('should replace value', () => {
|
|
443
|
-
const tree = new BST<number, string>([4, 5, [1, '1'], 2, 3], { isMapMode: false });
|
|
444
|
-
expect(tree.get(1)).toBe('1');
|
|
445
|
-
expect(tree.getNode(1)?.value).toBe('1');
|
|
446
|
-
tree.set(1, 'a');
|
|
447
|
-
expect(tree.get(1)).toBe('a');
|
|
448
|
-
tree.set([1, 'b']);
|
|
449
|
-
expect(tree.getNode(1)?.value).toBe('b');
|
|
450
|
-
expect(tree.get(1)).toBe('b');
|
|
451
|
-
const treeMap = new BST<number>([4, 5, [1, '1'], 2, 3]);
|
|
452
|
-
expect(treeMap.get(1)).toBe('1');
|
|
453
|
-
expect(treeMap.getNode(1)?.value).toBe('1');
|
|
454
|
-
treeMap.set(1, 'a');
|
|
455
|
-
expect(treeMap.get(1)).toBe('a');
|
|
456
|
-
treeMap.set([1, 'b']);
|
|
457
|
-
expect(treeMap.getNode(1)?.value).toBe('b');
|
|
458
|
-
expect(treeMap.get(1)).toBe('b');
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
it('should search in range', () => {
|
|
462
|
-
const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
463
|
-
expect(bst.rangeSearch([4, 12])).toEqual([5, 7, 10, 12]);
|
|
464
|
-
expect(bst.rangeSearch([12, 12])).toEqual([12]);
|
|
465
|
-
});
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
describe('BST operations test recursively', () => {
|
|
469
|
-
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
470
|
-
const bst = new BST<number>([], {
|
|
471
|
-
iterationType: 'RECURSIVE'
|
|
472
|
-
});
|
|
473
|
-
expect(bst).toBeInstanceOf(BST);
|
|
474
|
-
bst.set([11, 11]);
|
|
475
|
-
bst.set([3, 3]);
|
|
476
|
-
const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
477
|
-
bst.setMany(idsAndValues, undefined, false);
|
|
478
|
-
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
479
|
-
|
|
480
|
-
if (bst.root) expect(bst.root.key).toBe(11);
|
|
481
|
-
|
|
482
|
-
expect(bst.size).toBe(16);
|
|
483
|
-
|
|
484
|
-
expect(bst.has(6)).toBe(true);
|
|
485
|
-
|
|
486
|
-
const node6 = bst.getNode(6);
|
|
487
|
-
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
488
|
-
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
489
|
-
|
|
490
|
-
const nodeId10 = bst.getNode(10);
|
|
491
|
-
expect(bst.get(10)).toBe(undefined);
|
|
492
|
-
expect(nodeId10?.key).toBe(10);
|
|
493
|
-
|
|
494
|
-
const nodeVal9 = bst.getNode(node => node.value === 9);
|
|
495
|
-
expect(bst.get(nodeVal9?.key)).toBe(undefined);
|
|
496
|
-
|
|
497
|
-
const leftMost = bst.getLeftMost();
|
|
498
|
-
expect(leftMost).toBe(1);
|
|
499
|
-
|
|
500
|
-
const node15 = bst.getNode(15);
|
|
501
|
-
const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15);
|
|
502
|
-
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
503
|
-
|
|
504
|
-
let subTreeSum = 0;
|
|
505
|
-
if (node15) bst.dfs(node => (subTreeSum += node.key), 'PRE', false, 15);
|
|
506
|
-
expect(subTreeSum).toBe(70);
|
|
507
|
-
|
|
508
|
-
let lesserSum = 0;
|
|
509
|
-
bst.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
510
|
-
expect(lesserSum).toBe(45);
|
|
511
|
-
|
|
512
|
-
expect(node15).toBeInstanceOf(BSTNode);
|
|
513
|
-
|
|
514
|
-
const node11 = bst.getNode(11);
|
|
515
|
-
expect(node11).toBeInstanceOf(BSTNode);
|
|
516
|
-
|
|
517
|
-
const dfsInorderNodes = bst.dfs(node => node, 'IN');
|
|
518
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
519
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
520
|
-
|
|
521
|
-
bst.perfectlyBalance();
|
|
522
|
-
expect(bst.isPerfectlyBalanced()).toBe(true);
|
|
523
|
-
|
|
524
|
-
const bfsNodesAfterBalanced: BSTNode<number>[] = [];
|
|
525
|
-
bst.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
526
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
527
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
528
|
-
|
|
529
|
-
const removed11 = bst.delete(11);
|
|
530
|
-
expect(removed11).toBeInstanceOf(Array);
|
|
531
|
-
expect(removed11[0]).toBeDefined();
|
|
532
|
-
expect(removed11[0].deleted).toBeDefined();
|
|
533
|
-
|
|
534
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
|
535
|
-
|
|
536
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
537
|
-
|
|
538
|
-
expect(bst.getHeight(15)).toBe(1);
|
|
539
|
-
|
|
540
|
-
const removed1 = bst.delete(1);
|
|
541
|
-
expect(removed1).toBeInstanceOf(Array);
|
|
542
|
-
expect(removed1[0]).toBeDefined();
|
|
543
|
-
expect(removed1[0].deleted).toBeDefined();
|
|
544
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
|
545
|
-
|
|
546
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
547
|
-
|
|
548
|
-
expect(bst.getHeight()).toBe(4);
|
|
549
|
-
|
|
550
|
-
const removed4 = bst.delete(4);
|
|
551
|
-
expect(removed4).toBeInstanceOf(Array);
|
|
552
|
-
expect(removed4[0]).toBeDefined();
|
|
553
|
-
expect(removed4[0].deleted).toBeDefined();
|
|
554
|
-
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
|
555
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
556
|
-
expect(bst.getHeight()).toBe(4);
|
|
557
|
-
|
|
558
|
-
const removed10 = bst.delete(10);
|
|
559
|
-
expect(removed10).toBeInstanceOf(Array);
|
|
560
|
-
expect(removed10[0]).toBeDefined();
|
|
561
|
-
expect(removed10[0].deleted).toBeDefined();
|
|
562
|
-
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
|
563
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
564
|
-
expect(bst.getHeight()).toBe(4);
|
|
565
|
-
|
|
566
|
-
const removed15 = bst.delete(15);
|
|
567
|
-
expect(removed15).toBeInstanceOf(Array);
|
|
568
|
-
expect(removed15[0]).toBeDefined();
|
|
569
|
-
expect(removed15[0].deleted).toBeDefined();
|
|
570
|
-
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
|
571
|
-
|
|
572
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
573
|
-
expect(bst.getHeight()).toBe(3);
|
|
574
|
-
|
|
575
|
-
const removed5 = bst.delete(5);
|
|
576
|
-
expect(removed5).toBeInstanceOf(Array);
|
|
577
|
-
expect(removed5[0]).toBeDefined();
|
|
578
|
-
expect(removed5[0].deleted).toBeDefined();
|
|
579
|
-
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
|
580
|
-
|
|
581
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
582
|
-
expect(bst.getHeight()).toBe(3);
|
|
583
|
-
|
|
584
|
-
const removed13 = bst.delete(13);
|
|
585
|
-
expect(removed13).toBeInstanceOf(Array);
|
|
586
|
-
expect(removed13[0]).toBeDefined();
|
|
587
|
-
expect(removed13[0].deleted).toBeDefined();
|
|
588
|
-
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
|
589
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
590
|
-
expect(bst.getHeight()).toBe(3);
|
|
591
|
-
|
|
592
|
-
const removed3 = bst.delete(3);
|
|
593
|
-
expect(removed3).toBeInstanceOf(Array);
|
|
594
|
-
expect(removed3[0]).toBeDefined();
|
|
595
|
-
expect(removed3[0].deleted).toBeDefined();
|
|
596
|
-
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
|
597
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
598
|
-
expect(bst.getHeight()).toBe(3);
|
|
599
|
-
|
|
600
|
-
const removed8 = bst.delete(8);
|
|
601
|
-
expect(removed8).toBeInstanceOf(Array);
|
|
602
|
-
expect(removed8[0]).toBeDefined();
|
|
603
|
-
expect(removed8[0].deleted).toBeDefined();
|
|
604
|
-
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
|
605
|
-
expect(bst.isAVLBalanced()).toBe(true);
|
|
606
|
-
expect(bst.getHeight()).toBe(3);
|
|
607
|
-
|
|
608
|
-
const removed6 = bst.delete(6);
|
|
609
|
-
expect(removed6).toBeInstanceOf(Array);
|
|
610
|
-
expect(removed6[0]).toBeDefined();
|
|
611
|
-
expect(removed6[0].deleted).toBeDefined();
|
|
612
|
-
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
|
613
|
-
expect(bst.delete(6).length).toBe(0);
|
|
614
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
615
|
-
expect(bst.getHeight()).toBe(3);
|
|
616
|
-
|
|
617
|
-
const removed7 = bst.delete(7);
|
|
618
|
-
expect(removed7).toBeInstanceOf(Array);
|
|
619
|
-
expect(removed7[0]).toBeDefined();
|
|
620
|
-
expect(removed7[0].deleted).toBeDefined();
|
|
621
|
-
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
|
622
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
623
|
-
expect(bst.getHeight()).toBe(3);
|
|
624
|
-
|
|
625
|
-
const removed9 = bst.delete(9);
|
|
626
|
-
expect(removed9).toBeInstanceOf(Array);
|
|
627
|
-
expect(removed9[0]).toBeDefined();
|
|
628
|
-
expect(removed9[0].deleted).toBeDefined();
|
|
629
|
-
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
|
630
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
631
|
-
expect(bst.getHeight()).toBe(3);
|
|
632
|
-
|
|
633
|
-
const removed14 = bst.delete(14);
|
|
634
|
-
expect(removed14).toBeInstanceOf(Array);
|
|
635
|
-
expect(removed14[0]).toBeDefined();
|
|
636
|
-
expect(removed14[0].deleted).toBeDefined();
|
|
637
|
-
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
|
638
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
639
|
-
expect(bst.getHeight()).toBe(2);
|
|
640
|
-
|
|
641
|
-
expect(bst.isAVLBalanced()).toBe(false);
|
|
642
|
-
|
|
643
|
-
const bfsIDs: number[] = [];
|
|
644
|
-
bst.bfs(node => bfsIDs.push(node.key));
|
|
645
|
-
expect(bfsIDs[0]).toBe(2);
|
|
646
|
-
expect(bfsIDs[1]).toBe(12);
|
|
647
|
-
expect(bfsIDs[2]).toBe(16);
|
|
648
|
-
|
|
649
|
-
const bfsNodes: BSTNode<number>[] = [];
|
|
650
|
-
bst.bfs(node => bfsNodes.push(node));
|
|
651
|
-
expect(bfsNodes[0].key).toBe(2);
|
|
652
|
-
expect(bfsNodes[1].key).toBe(12);
|
|
653
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
657
|
-
const objBST = new BST<number, { key: number; keyA: number }>();
|
|
658
|
-
expect(objBST).toBeInstanceOf(BST);
|
|
659
|
-
objBST.set([11, { key: 11, keyA: 11 }]);
|
|
660
|
-
objBST.set([3, { key: 3, keyA: 3 }]);
|
|
661
|
-
const entries: [number, { key: number; keyA: number }][] = [
|
|
662
|
-
[15, { key: 15, keyA: 15 }],
|
|
663
|
-
[1, { key: 1, keyA: 1 }],
|
|
664
|
-
[8, { key: 8, keyA: 8 }],
|
|
665
|
-
[13, { key: 13, keyA: 13 }],
|
|
666
|
-
[16, { key: 16, keyA: 16 }],
|
|
667
|
-
[2, { key: 2, keyA: 2 }],
|
|
668
|
-
[6, { key: 6, keyA: 6 }],
|
|
669
|
-
[9, { key: 9, keyA: 9 }],
|
|
670
|
-
[12, { key: 12, keyA: 12 }],
|
|
671
|
-
[14, { key: 14, keyA: 14 }],
|
|
672
|
-
[4, { key: 4, keyA: 4 }],
|
|
673
|
-
[7, { key: 7, keyA: 7 }],
|
|
674
|
-
[10, { key: 10, keyA: 10 }],
|
|
675
|
-
[5, { key: 5, keyA: 5 }]
|
|
676
|
-
];
|
|
677
|
-
|
|
678
|
-
objBST.setMany(entries, undefined, false);
|
|
679
|
-
|
|
680
|
-
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
681
|
-
|
|
682
|
-
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
683
|
-
|
|
684
|
-
expect(objBST.has(6)).toBe(true);
|
|
685
|
-
|
|
686
|
-
const node6 = objBST.getNode(6);
|
|
687
|
-
expect(objBST.get(6)).toEqual({
|
|
688
|
-
key: 6,
|
|
689
|
-
keyA: 6
|
|
690
|
-
});
|
|
691
|
-
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
692
|
-
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
693
|
-
|
|
694
|
-
const nodeId10 = objBST.getNode(10);
|
|
695
|
-
expect(nodeId10?.key).toBe(10);
|
|
696
|
-
|
|
697
|
-
const nodeVal9 = objBST.getNode(9);
|
|
698
|
-
expect(nodeVal9?.key).toBe(9);
|
|
699
|
-
|
|
700
|
-
const leftMost = objBST.getLeftMost();
|
|
701
|
-
expect(leftMost).toBe(1);
|
|
702
|
-
|
|
703
|
-
const node15 = objBST.getNode(15);
|
|
704
|
-
expect(objBST.get(node15)).toEqual({
|
|
705
|
-
key: 15,
|
|
706
|
-
keyA: 15
|
|
707
|
-
});
|
|
708
|
-
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node => node, node15);
|
|
709
|
-
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
710
|
-
|
|
711
|
-
let subTreeSum = 0;
|
|
712
|
-
if (node15) objBST.dfs(node => (subTreeSum += node.key), 'PRE', false, node15);
|
|
713
|
-
expect(subTreeSum).toBe(70);
|
|
714
|
-
|
|
715
|
-
let lesserSum = 0;
|
|
716
|
-
objBST.lesserOrGreaterTraverse(node => (lesserSum += node.key), -1, 10);
|
|
717
|
-
expect(lesserSum).toBe(45);
|
|
718
|
-
|
|
719
|
-
expect(node15).toBeInstanceOf(BSTNode);
|
|
720
|
-
|
|
721
|
-
const node11 = objBST.getNode(11);
|
|
722
|
-
expect(node11).toBeInstanceOf(BSTNode);
|
|
723
|
-
|
|
724
|
-
const dfsInorderNodes = objBST.dfs(node => node, 'IN');
|
|
725
|
-
expect(dfsInorderNodes[0].key).toBe(1);
|
|
726
|
-
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
|
727
|
-
|
|
728
|
-
objBST.perfectlyBalance();
|
|
729
|
-
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
|
730
|
-
|
|
731
|
-
const bfsNodesAfterBalanced: BSTNode<number, { key: number; keyA: number }>[] = [];
|
|
732
|
-
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
|
733
|
-
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
|
734
|
-
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
|
735
|
-
|
|
736
|
-
const removed11 = objBST.delete(11);
|
|
737
|
-
expect(removed11).toBeInstanceOf(Array);
|
|
738
|
-
expect(removed11[0]).toBeDefined();
|
|
739
|
-
expect(removed11[0].deleted).toBeDefined();
|
|
740
|
-
|
|
741
|
-
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
|
742
|
-
|
|
743
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
744
|
-
|
|
745
|
-
expect(node15 && objBST.getHeight(node15)).toBe(1);
|
|
746
|
-
|
|
747
|
-
const removed1 = objBST.delete(1);
|
|
748
|
-
expect(removed1).toBeInstanceOf(Array);
|
|
749
|
-
expect(removed1[0]).toBeDefined();
|
|
750
|
-
expect(removed1[0].deleted).toBeDefined();
|
|
751
|
-
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
|
752
|
-
|
|
753
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
754
|
-
|
|
755
|
-
expect(objBST.getHeight()).toBe(4);
|
|
756
|
-
|
|
757
|
-
const removed4 = objBST.delete(4);
|
|
758
|
-
expect(removed4).toBeInstanceOf(Array);
|
|
759
|
-
expect(removed4[0]).toBeDefined();
|
|
760
|
-
expect(removed4[0].deleted).toBeDefined();
|
|
761
|
-
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
|
762
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
763
|
-
expect(objBST.getHeight()).toBe(4);
|
|
764
|
-
|
|
765
|
-
const removed10 = objBST.delete(10);
|
|
766
|
-
expect(removed10).toBeInstanceOf(Array);
|
|
767
|
-
expect(removed10[0]).toBeDefined();
|
|
768
|
-
expect(removed10[0].deleted).toBeDefined();
|
|
769
|
-
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
|
770
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
771
|
-
expect(objBST.getHeight()).toBe(4);
|
|
772
|
-
|
|
773
|
-
const removed15 = objBST.delete(15);
|
|
774
|
-
expect(removed15).toBeInstanceOf(Array);
|
|
775
|
-
expect(removed15[0]).toBeDefined();
|
|
776
|
-
expect(removed15[0].deleted).toBeDefined();
|
|
777
|
-
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
|
778
|
-
|
|
779
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
780
|
-
expect(objBST.getHeight()).toBe(3);
|
|
781
|
-
|
|
782
|
-
const removed5 = objBST.delete(5);
|
|
783
|
-
expect(removed5).toBeInstanceOf(Array);
|
|
784
|
-
expect(removed5[0]).toBeDefined();
|
|
785
|
-
expect(removed5[0].deleted).toBeDefined();
|
|
786
|
-
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
|
787
|
-
|
|
788
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
789
|
-
expect(objBST.getHeight()).toBe(3);
|
|
790
|
-
|
|
791
|
-
const removed13 = objBST.delete(13);
|
|
792
|
-
expect(removed13).toBeInstanceOf(Array);
|
|
793
|
-
expect(removed13[0]).toBeDefined();
|
|
794
|
-
expect(removed13[0].deleted).toBeDefined();
|
|
795
|
-
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
|
796
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
797
|
-
expect(objBST.getHeight()).toBe(3);
|
|
798
|
-
|
|
799
|
-
const removed3 = objBST.delete(3);
|
|
800
|
-
expect(removed3).toBeInstanceOf(Array);
|
|
801
|
-
expect(removed3[0]).toBeDefined();
|
|
802
|
-
expect(removed3[0].deleted).toBeDefined();
|
|
803
|
-
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
|
804
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
805
|
-
expect(objBST.getHeight()).toBe(3);
|
|
806
|
-
|
|
807
|
-
const removed8 = objBST.delete(8);
|
|
808
|
-
expect(removed8).toBeInstanceOf(Array);
|
|
809
|
-
expect(removed8[0]).toBeDefined();
|
|
810
|
-
expect(removed8[0].deleted).toBeDefined();
|
|
811
|
-
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
|
812
|
-
expect(objBST.isAVLBalanced()).toBe(true);
|
|
813
|
-
expect(objBST.getHeight()).toBe(3);
|
|
814
|
-
|
|
815
|
-
const removed6 = objBST.delete(6);
|
|
816
|
-
expect(removed6).toBeInstanceOf(Array);
|
|
817
|
-
expect(removed6[0]).toBeDefined();
|
|
818
|
-
expect(removed6[0].deleted).toBeDefined();
|
|
819
|
-
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
|
820
|
-
expect(objBST.delete(6).length).toBe(0);
|
|
821
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
822
|
-
expect(objBST.getHeight()).toBe(3);
|
|
823
|
-
|
|
824
|
-
const removed7 = objBST.delete(7);
|
|
825
|
-
expect(removed7).toBeInstanceOf(Array);
|
|
826
|
-
expect(removed7[0]).toBeDefined();
|
|
827
|
-
expect(removed7[0].deleted).toBeDefined();
|
|
828
|
-
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
|
829
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
830
|
-
expect(objBST.getHeight()).toBe(3);
|
|
831
|
-
|
|
832
|
-
const removed9 = objBST.delete(9);
|
|
833
|
-
expect(removed9).toBeInstanceOf(Array);
|
|
834
|
-
expect(removed9[0]).toBeDefined();
|
|
835
|
-
expect(removed9[0].deleted).toBeDefined();
|
|
836
|
-
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
|
837
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
838
|
-
expect(objBST.getHeight()).toBe(3);
|
|
839
|
-
|
|
840
|
-
const removed14 = objBST.delete(14);
|
|
841
|
-
expect(removed14).toBeInstanceOf(Array);
|
|
842
|
-
expect(removed14[0]).toBeDefined();
|
|
843
|
-
expect(removed14[0].deleted).toBeDefined();
|
|
844
|
-
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
|
845
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
846
|
-
expect(objBST.getHeight()).toBe(2);
|
|
847
|
-
|
|
848
|
-
expect(objBST.isAVLBalanced()).toBe(false);
|
|
849
|
-
|
|
850
|
-
const bfsIDs: number[] = [];
|
|
851
|
-
objBST.bfs(node => bfsIDs.push(node.key));
|
|
852
|
-
expect(bfsIDs[0]).toBe(2);
|
|
853
|
-
expect(bfsIDs[1]).toBe(12);
|
|
854
|
-
expect(bfsIDs[2]).toBe(16);
|
|
855
|
-
|
|
856
|
-
const bfsNodes: BSTNode<number, { key: number; keyA: number }>[] = [];
|
|
857
|
-
objBST.bfs(node => bfsNodes.push(node));
|
|
858
|
-
expect(bfsNodes[0].key).toBe(2);
|
|
859
|
-
expect(bfsNodes[1].key).toBe(12);
|
|
860
|
-
expect(bfsNodes[2].key).toBe(16);
|
|
861
|
-
});
|
|
862
|
-
|
|
863
|
-
it('should delete', () => {
|
|
864
|
-
const numBST = new BST<number>();
|
|
865
|
-
numBST.setMany([2, 4, 5, 3, 1]);
|
|
866
|
-
expect(numBST.size).toBe(5);
|
|
867
|
-
numBST.delete(1);
|
|
868
|
-
expect(numBST.size).toBe(4);
|
|
869
|
-
|
|
870
|
-
numBST.delete(2);
|
|
871
|
-
numBST.delete(3);
|
|
872
|
-
numBST.delete(4);
|
|
873
|
-
numBST.delete(5);
|
|
874
|
-
expect(numBST.size).toBe(0);
|
|
875
|
-
numBST.delete(5);
|
|
876
|
-
expect(numBST.size).toBe(0);
|
|
877
|
-
});
|
|
878
|
-
|
|
879
|
-
it('should listLevels', () => {
|
|
880
|
-
const bst = new BST<number>();
|
|
881
|
-
bst.setMany([2, 4, 5, 3, 1]);
|
|
882
|
-
expect(bst.size).toBe(5);
|
|
883
|
-
bst.delete(1);
|
|
884
|
-
bst.delete(5);
|
|
885
|
-
const levelKeys = bst.listLevels();
|
|
886
|
-
expect(levelKeys).toEqual([[3], [2, 4]]);
|
|
887
|
-
});
|
|
888
|
-
|
|
889
|
-
it('should lesserOrGreaterTraverse', () => {
|
|
890
|
-
const bst = new BST<number>();
|
|
891
|
-
const levelKeys = bst.lesserOrGreaterTraverse();
|
|
892
|
-
expect(levelKeys).toEqual([]);
|
|
893
|
-
bst.setMany([2, 4, 5, 3, 1]);
|
|
894
|
-
expect(bst.size).toBe(5);
|
|
895
|
-
bst.delete(1);
|
|
896
|
-
bst.delete(5);
|
|
897
|
-
const levelKeys1 = bst.lesserOrGreaterTraverse();
|
|
898
|
-
expect(levelKeys1).toEqual([2]);
|
|
899
|
-
});
|
|
900
|
-
|
|
901
|
-
it('should the clone method', () => {
|
|
902
|
-
function checkTreeStructure(bst: IBinaryTree<string, number>) {
|
|
903
|
-
expect(bst.size).toBe(4);
|
|
904
|
-
expect(bst.root?.key).toBe('2');
|
|
905
|
-
expect(bst.root?.left?.key).toBe('1');
|
|
906
|
-
expect(bst.root?.left?.left?.key).toBe(undefined);
|
|
907
|
-
expect(bst.root?.left?.right?.key).toBe(undefined);
|
|
908
|
-
expect(bst.root?.right?.key).toBe('4');
|
|
909
|
-
expect(bst.root?.right?.left?.key).toBe(undefined);
|
|
910
|
-
expect(bst.root?.right?.right?.key).toBe('5');
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
const bst = new BST<string, number>();
|
|
914
|
-
bst.setMany([
|
|
915
|
-
['2', 2],
|
|
916
|
-
['4', 4],
|
|
917
|
-
['5', 5],
|
|
918
|
-
['3', 3],
|
|
919
|
-
['1', 1]
|
|
920
|
-
]);
|
|
921
|
-
expect(bst.size).toBe(5);
|
|
922
|
-
expect(bst.root?.key).toBe('3');
|
|
923
|
-
expect(bst.root?.left?.key).toBe('1');
|
|
924
|
-
expect(bst.root?.left?.left?.key).toBe(undefined);
|
|
925
|
-
expect(bst.root?.left?.right?.key).toBe('2');
|
|
926
|
-
expect(bst.root?.right?.key).toBe('4');
|
|
927
|
-
expect(bst.root?.right?.left?.key).toBe(undefined);
|
|
928
|
-
expect(bst.root?.right?.right?.key).toBe('5');
|
|
929
|
-
bst.delete('3');
|
|
930
|
-
checkTreeStructure(bst);
|
|
931
|
-
const cloned = bst.clone();
|
|
932
|
-
checkTreeStructure(cloned);
|
|
933
|
-
bst.delete('2');
|
|
934
|
-
bst.delete('1');
|
|
935
|
-
bst.delete('4');
|
|
936
|
-
bst.delete('5');
|
|
937
|
-
expect(bst.size).toBe(0);
|
|
938
|
-
|
|
939
|
-
cloned.delete('1');
|
|
940
|
-
expect(bst.size).toBe(0);
|
|
941
|
-
expect(cloned.size).toBe(3);
|
|
942
|
-
cloned.delete('2');
|
|
943
|
-
cloned.delete('3');
|
|
944
|
-
cloned.delete('4');
|
|
945
|
-
cloned.delete('5');
|
|
946
|
-
expect(cloned.size).toBe(0);
|
|
947
|
-
});
|
|
948
|
-
|
|
949
|
-
if (isTestStackOverflow) {
|
|
950
|
-
it('should getLeftMost', () => {
|
|
951
|
-
const bst = new BST<number>([]);
|
|
952
|
-
for (let i = 1; i <= SYSTEM_MAX_CALL_STACK; i++) bst.set(i);
|
|
953
|
-
|
|
954
|
-
expect(() => {
|
|
955
|
-
const leftMost = bst.getLeftMost(node => node, bst.root, 'RECURSIVE');
|
|
956
|
-
expect(leftMost?.key).toEqual(SYSTEM_MAX_CALL_STACK);
|
|
957
|
-
}).toThrow('Maximum call stack size exceeded');
|
|
958
|
-
|
|
959
|
-
const leftMost = bst.getLeftMost(node => node, bst.root, 'ITERATIVE');
|
|
960
|
-
expect(leftMost?.key).toEqual(SYSTEM_MAX_CALL_STACK);
|
|
961
|
-
});
|
|
962
|
-
|
|
963
|
-
it('should getRightMost', () => {
|
|
964
|
-
const bst = new BST<number>();
|
|
965
|
-
for (let i = 1; i <= SYSTEM_MAX_CALL_STACK; i++) bst.set(i);
|
|
966
|
-
|
|
967
|
-
expect(() => {
|
|
968
|
-
const rightMost = bst.getRightMost(node => node, bst.root, 'RECURSIVE');
|
|
969
|
-
expect(rightMost?.key).toEqual(SYSTEM_MAX_CALL_STACK);
|
|
970
|
-
}).toThrow('Maximum call stack size exceeded');
|
|
971
|
-
const rightMost = bst.getRightMost(node => node, bst.root, 'ITERATIVE');
|
|
972
|
-
expect(rightMost?.key).toEqual(SYSTEM_MAX_CALL_STACK);
|
|
973
|
-
});
|
|
974
|
-
}
|
|
975
|
-
});
|
|
976
|
-
|
|
977
|
-
describe('BST isBST', function () {
|
|
978
|
-
it('isBST', () => {
|
|
979
|
-
const bst = new BST<number, number>();
|
|
980
|
-
bst.setMany([1, 2, 3, 9, 8, 5, 6, 7, 4]);
|
|
981
|
-
expect(bst.isBST()).toBe(true);
|
|
982
|
-
});
|
|
983
|
-
|
|
984
|
-
it('isBST when variant is Max', () => {
|
|
985
|
-
const bst = new BST<number, number>([1, 2, 3, 9, 8, 5, 6, 7, 4], {
|
|
986
|
-
comparator: (a, b) => b - a
|
|
987
|
-
});
|
|
988
|
-
bst.setMany([1, 2, 3, 9, 8, 5, 6, 7, 4]);
|
|
989
|
-
expect(bst.isBST()).toBe(true);
|
|
990
|
-
});
|
|
991
|
-
});
|
|
992
|
-
|
|
993
|
-
describe('BST Performance test', function () {
|
|
994
|
-
const bst = new BST<number, number>();
|
|
995
|
-
const inputSize = 10000; // Adjust input sizes as needed
|
|
996
|
-
|
|
997
|
-
beforeEach(() => {
|
|
998
|
-
bst.clear();
|
|
999
|
-
});
|
|
1000
|
-
|
|
1001
|
-
it(`Observe the time consumption of BST.dfs be good`, function () {
|
|
1002
|
-
const startDFS = performance.now();
|
|
1003
|
-
const dfs = bst.dfs(node => node);
|
|
1004
|
-
if (isDebug) console.log('---bfs', performance.now() - startDFS, dfs.length);
|
|
1005
|
-
});
|
|
1006
|
-
|
|
1007
|
-
it('Should the time consumption of lesserOrGreaterTraverse fitting O(n log n)', function () {
|
|
1008
|
-
const nodes: number[] = [];
|
|
1009
|
-
for (let i = 0; i < inputSize; i++) {
|
|
1010
|
-
nodes.push(i);
|
|
1011
|
-
}
|
|
1012
|
-
const start = performance.now();
|
|
1013
|
-
bst.setMany(nodes);
|
|
1014
|
-
if (isDebug) console.log('---add', performance.now() - start);
|
|
1015
|
-
const startL = performance.now();
|
|
1016
|
-
bst.lesserOrGreaterTraverse(
|
|
1017
|
-
node => {
|
|
1018
|
-
node.key -= 1;
|
|
1019
|
-
},
|
|
1020
|
-
-1,
|
|
1021
|
-
inputSize / 2
|
|
1022
|
-
);
|
|
1023
|
-
if (isDebug) console.log('---lesserOrGreaterTraverse', performance.now() - startL);
|
|
1024
|
-
});
|
|
1025
|
-
|
|
1026
|
-
it('Should the time consumption of listLevels fitting well', function () {
|
|
1027
|
-
const nodes: number[] = [];
|
|
1028
|
-
for (let i = 0; i < inputSize; i++) {
|
|
1029
|
-
nodes.push(i);
|
|
1030
|
-
}
|
|
1031
|
-
const start = performance.now();
|
|
1032
|
-
bst.setMany(nodes);
|
|
1033
|
-
if (isDebug) console.log('---add', performance.now() - start);
|
|
1034
|
-
const startL = performance.now();
|
|
1035
|
-
const arr: number[][] = bst.listLevels(node => node.key);
|
|
1036
|
-
if (isDebug) console.log('---listLevels', arr);
|
|
1037
|
-
if (isDebug) console.log('---listLevels', performance.now() - startL);
|
|
1038
|
-
});
|
|
1039
|
-
|
|
1040
|
-
it('should the lastKey of a BST to be the largest key', function () {
|
|
1041
|
-
const bst = new BST();
|
|
1042
|
-
bst.setMany([9, 8, 7, 3, 1, 2, 5, 4, 6], undefined, false);
|
|
1043
|
-
// TODO
|
|
1044
|
-
// expect(bst.lastKey()).toBe(9);
|
|
1045
|
-
});
|
|
1046
|
-
|
|
1047
|
-
it('should dfs as sub tree traversal, null should be ignored', () => {
|
|
1048
|
-
const bst = new BST();
|
|
1049
|
-
bst.setMany([4, 2, 6, 1, 3, 5, 7]);
|
|
1050
|
-
expect(bst.dfs(node => node.key, 'PRE', false, bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1051
|
-
expect(bst.dfs(node => node.key, 'PRE', false, bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1052
|
-
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', false, bst.getNode(6), 'ITERATIVE')).toEqual([6, 5, 7]);
|
|
1053
|
-
expect(bst.dfs(node => node?.key ?? undefined, 'PRE', false, bst.getNode(6), 'RECURSIVE')).toEqual([6, 5, 7]);
|
|
1054
|
-
});
|
|
1055
|
-
});
|
|
1056
|
-
|
|
1057
|
-
describe('BST iterative methods test', () => {
|
|
1058
|
-
let bst: BST<number, string>;
|
|
1059
|
-
beforeEach(() => {
|
|
1060
|
-
bst = new BST();
|
|
1061
|
-
bst.setMany(
|
|
1062
|
-
[
|
|
1063
|
-
[1, 'a'],
|
|
1064
|
-
[2, 'b'],
|
|
1065
|
-
[3, 'c']
|
|
1066
|
-
],
|
|
1067
|
-
[],
|
|
1068
|
-
false
|
|
1069
|
-
);
|
|
1070
|
-
});
|
|
1071
|
-
|
|
1072
|
-
it('The node obtained by get Node should match the node type', () => {
|
|
1073
|
-
const node3 = bst.getNode(3);
|
|
1074
|
-
// expect(node3).toBeInstanceOf(BinaryTreeNode);
|
|
1075
|
-
expect(node3).toBeInstanceOf(BSTNode);
|
|
1076
|
-
});
|
|
1077
|
-
|
|
1078
|
-
it('forEach should iterate over all elements', () => {
|
|
1079
|
-
const mockCallback = jest.fn();
|
|
1080
|
-
bst.forEach((value, key) => {
|
|
1081
|
-
mockCallback(key, value);
|
|
1082
|
-
});
|
|
1083
|
-
|
|
1084
|
-
expect(mockCallback.mock.calls.length).toBe(3);
|
|
1085
|
-
expect(mockCallback.mock.calls[0]).toEqual([1, 'a']);
|
|
1086
|
-
expect(mockCallback.mock.calls[1]).toEqual([2, 'b']);
|
|
1087
|
-
expect(mockCallback.mock.calls[2]).toEqual([3, 'c']);
|
|
1088
|
-
});
|
|
1089
|
-
|
|
1090
|
-
it('filter should return a new tree with filtered elements', () => {
|
|
1091
|
-
const filteredTree = bst.filter((_value, key) => key > 1);
|
|
1092
|
-
expect(filteredTree.size).toBe(2);
|
|
1093
|
-
expect([...filteredTree]).toEqual([
|
|
1094
|
-
[2, 'b'],
|
|
1095
|
-
[3, 'c']
|
|
1096
|
-
]);
|
|
1097
|
-
});
|
|
1098
|
-
|
|
1099
|
-
it('map should return a new tree with modified elements', () => {
|
|
1100
|
-
const mappedTree = bst.map((value, key) => [(key * 2).toString(), value]);
|
|
1101
|
-
expect(mappedTree.size).toBe(3);
|
|
1102
|
-
expect([...mappedTree]).toEqual([
|
|
1103
|
-
['2', 'a'],
|
|
1104
|
-
['4', 'b'],
|
|
1105
|
-
['6', 'c']
|
|
1106
|
-
]);
|
|
1107
|
-
});
|
|
1108
|
-
|
|
1109
|
-
it('reduce should accumulate values', () => {
|
|
1110
|
-
const sum = bst.reduce((acc, value, key) => acc + key, 0);
|
|
1111
|
-
expect(sum).toBe(6);
|
|
1112
|
-
});
|
|
1113
|
-
|
|
1114
|
-
it('[Symbol.iterator] should provide an iterator', () => {
|
|
1115
|
-
const entries = [];
|
|
1116
|
-
for (const entry of bst) {
|
|
1117
|
-
entries.push(entry);
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
expect(entries.length).toBe(3);
|
|
1121
|
-
expect(entries).toEqual([
|
|
1122
|
-
[1, 'a'],
|
|
1123
|
-
[2, 'b'],
|
|
1124
|
-
[3, 'c']
|
|
1125
|
-
]);
|
|
1126
|
-
});
|
|
1127
|
-
|
|
1128
|
-
it('should clone work well', () => {
|
|
1129
|
-
const cloned = bst.clone();
|
|
1130
|
-
expect(cloned.root?.left).toBe(undefined);
|
|
1131
|
-
expect(cloned.root?.right?.value).toBe('b');
|
|
1132
|
-
});
|
|
1133
|
-
|
|
1134
|
-
it('should keys', () => {
|
|
1135
|
-
const keys = bst.keys();
|
|
1136
|
-
expect([...keys]).toEqual([1, 2, 3]);
|
|
1137
|
-
});
|
|
1138
|
-
|
|
1139
|
-
it('should values', () => {
|
|
1140
|
-
const values = bst.values();
|
|
1141
|
-
expect([...values]).toEqual(['a', 'b', 'c']);
|
|
1142
|
-
});
|
|
1143
|
-
|
|
1144
|
-
it('should leaves', () => {
|
|
1145
|
-
const leaves = bst.leaves();
|
|
1146
|
-
expect(leaves).toEqual([3]);
|
|
1147
|
-
});
|
|
1148
|
-
|
|
1149
|
-
it('should collapsed, unbalanced, balanced bst leaves', () => {
|
|
1150
|
-
const collapsedToLinkedList = new BST();
|
|
1151
|
-
collapsedToLinkedList.setMany(
|
|
1152
|
-
[
|
|
1153
|
-
[1, 'a'],
|
|
1154
|
-
[2, 'b'],
|
|
1155
|
-
[3, 'c'],
|
|
1156
|
-
[4, 'd'],
|
|
1157
|
-
[5, 'e'],
|
|
1158
|
-
[6, 'f'],
|
|
1159
|
-
[7, 'g'],
|
|
1160
|
-
[8, 'h'],
|
|
1161
|
-
[9, 'i']
|
|
1162
|
-
],
|
|
1163
|
-
[],
|
|
1164
|
-
false
|
|
1165
|
-
);
|
|
1166
|
-
|
|
1167
|
-
expect(collapsedToLinkedList.leaves()).toEqual([9]);
|
|
1168
|
-
|
|
1169
|
-
const unbalanced = new BST();
|
|
1170
|
-
unbalanced.setMany(
|
|
1171
|
-
[
|
|
1172
|
-
[2, 'b'],
|
|
1173
|
-
[1, 'a'],
|
|
1174
|
-
[3, 'c'],
|
|
1175
|
-
[4, 'd'],
|
|
1176
|
-
[5, 'e'],
|
|
1177
|
-
[6, 'f'],
|
|
1178
|
-
[7, 'g'],
|
|
1179
|
-
[8, 'h'],
|
|
1180
|
-
[9, 'i']
|
|
1181
|
-
],
|
|
1182
|
-
[],
|
|
1183
|
-
false
|
|
1184
|
-
);
|
|
1185
|
-
|
|
1186
|
-
expect(unbalanced.leaves()).toEqual([1, 9]);
|
|
1187
|
-
|
|
1188
|
-
const balanced = new BST();
|
|
1189
|
-
balanced.setMany(
|
|
1190
|
-
[
|
|
1191
|
-
[2, 'b'],
|
|
1192
|
-
[1, 'a'],
|
|
1193
|
-
[3, 'c'],
|
|
1194
|
-
[4, 'd'],
|
|
1195
|
-
[5, 'e'],
|
|
1196
|
-
[6, 'f'],
|
|
1197
|
-
[7, 'g'],
|
|
1198
|
-
[8, 'h'],
|
|
1199
|
-
[9, 'i']
|
|
1200
|
-
],
|
|
1201
|
-
[],
|
|
1202
|
-
true
|
|
1203
|
-
);
|
|
1204
|
-
|
|
1205
|
-
expect(balanced.leaves()).toEqual([1, 6, 4, 9]);
|
|
1206
|
-
expect(balanced.leaves(node => balanced.get(node))).toEqual(['a', 'f', 'd', 'i']);
|
|
1207
|
-
});
|
|
1208
|
-
});
|
|
1209
|
-
|
|
1210
|
-
describe('BST operations not map mode test', () => {
|
|
1211
|
-
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
1212
|
-
const bst = new BST<number, number>([], { isMapMode: false });
|
|
1213
|
-
expect(bst).toBeInstanceOf(BST);
|
|
1214
|
-
bst.set([11, 11]);
|
|
1215
|
-
bst.set([3, 3]);
|
|
1216
|
-
const idsAndValues: [number, number][] = [
|
|
1217
|
-
[15, 15],
|
|
1218
|
-
[1, 1],
|
|
1219
|
-
[8, 8],
|
|
1220
|
-
[13, 13],
|
|
1221
|
-
[16, 16],
|
|
1222
|
-
[2, 2],
|
|
1223
|
-
[6, 6],
|
|
1224
|
-
[9, 9],
|
|
1225
|
-
[12, 12],
|
|
1226
|
-
[14, 14],
|
|
1227
|
-
[4, 4],
|
|
1228
|
-
[7, 7],
|
|
1229
|
-
[10, 10],
|
|
1230
|
-
[5, 5]
|
|
1231
|
-
];
|
|
1232
|
-
bst.setMany(idsAndValues, undefined, false);
|
|
1233
|
-
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
1234
|
-
|
|
1235
|
-
if (bst.root) expect(bst.root.key).toBe(11);
|
|
1236
|
-
|
|
1237
|
-
expect(bst.size).toBe(16);
|
|
1238
|
-
|
|
1239
|
-
expect(bst.has(6)).toBe(true);
|
|
1240
|
-
|
|
1241
|
-
const node6 = bst.getNode(6);
|
|
1242
|
-
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
1243
|
-
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
1244
|
-
|
|
1245
|
-
const nodeId10 = bst.getNode(10);
|
|
1246
|
-
expect(nodeId10?.key).toBe(10);
|
|
1247
|
-
|
|
1248
|
-
const nodeVal9 = bst.getNode(node => node.key === 9);
|
|
1249
|
-
expect(nodeVal9?.key).toBe(9);
|
|
1250
|
-
|
|
1251
|
-
const leftMost = bst.getLeftMost();
|
|
1252
|
-
expect(leftMost).toBe(1);
|
|
1253
|
-
|
|
1254
|
-
expect(bst.isBST()).toBe(true);
|
|
1255
|
-
|
|
1256
|
-
const node15 = bst.getNode(15);
|
|
1257
|
-
const minNodeBySpecificNode = node15 && bst.getLeftMost(node => node, node15);
|
|
1258
|
-
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
1259
|
-
|
|
1260
|
-
const nodes = bst.getNodes(node => node.key === 15);
|
|
1261
|
-
expect(nodes.map(node => node.key)).toEqual([15]);
|
|
1262
|
-
});
|
|
1263
|
-
|
|
1264
|
-
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
1265
|
-
const objBST = new BST<number, { name: string; age: number }>([], { isMapMode: false });
|
|
1266
|
-
expect(objBST).toBeInstanceOf(BST);
|
|
1267
|
-
objBST.set([11, { name: '11', age: 11 }]);
|
|
1268
|
-
objBST.set([3, { name: '3', age: 3 }]);
|
|
1269
|
-
|
|
1270
|
-
objBST.setMany(
|
|
1271
|
-
[15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5],
|
|
1272
|
-
[
|
|
1273
|
-
{ name: 'Alice', age: 15 },
|
|
1274
|
-
{ name: 'Bob', age: 1 },
|
|
1275
|
-
{ name: 'Charlie', age: 8 },
|
|
1276
|
-
{ name: 'David', age: 13 },
|
|
1277
|
-
{ name: 'Emma', age: 16 },
|
|
1278
|
-
{ name: 'Frank', age: 2 },
|
|
1279
|
-
{ name: 'Grace', age: 6 },
|
|
1280
|
-
{ name: 'Hannah', age: 9 },
|
|
1281
|
-
{ name: 'Isaac', age: 12 },
|
|
1282
|
-
{ name: 'Jack', age: 14 },
|
|
1283
|
-
{ name: 'Katie', age: 4 },
|
|
1284
|
-
{ name: 'Liam', age: 7 },
|
|
1285
|
-
{ name: 'Mia', age: 10 },
|
|
1286
|
-
{ name: 'Noah', age: 5 }
|
|
1287
|
-
],
|
|
1288
|
-
false
|
|
1289
|
-
);
|
|
1290
|
-
|
|
1291
|
-
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
1292
|
-
|
|
1293
|
-
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
1294
|
-
|
|
1295
|
-
expect(objBST.has(6)).toBe(true);
|
|
1296
|
-
|
|
1297
|
-
const node6 = objBST.getNode(6);
|
|
1298
|
-
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
1299
|
-
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
1300
|
-
|
|
1301
|
-
const nodeId10 = objBST.getNode(10);
|
|
1302
|
-
expect(nodeId10?.key).toBe(10);
|
|
1303
|
-
|
|
1304
|
-
const nodeVal9 = objBST.getNode(9);
|
|
1305
|
-
expect(nodeVal9?.key).toBe(9);
|
|
1306
|
-
|
|
1307
|
-
const leftMost = objBST.getLeftMost();
|
|
1308
|
-
expect(leftMost).toBe(1);
|
|
1309
|
-
|
|
1310
|
-
const node15 = objBST.getNode(15);
|
|
1311
|
-
expect(objBST.get(node15)).toEqual({
|
|
1312
|
-
name: 'Alice',
|
|
1313
|
-
age: 15
|
|
1314
|
-
});
|
|
1315
|
-
});
|
|
1316
|
-
|
|
1317
|
-
// it('should keyValueNodeEntryRawToNodeAndValue', () => {
|
|
1318
|
-
// const bst = new BST<number>([], { isMapMode: false });
|
|
1319
|
-
// const node0 = bst.keyValueNodeEntryRawToNodeAndValue(0);
|
|
1320
|
-
// expect(node0).toEqual([
|
|
1321
|
-
// {
|
|
1322
|
-
// _left: undefined,
|
|
1323
|
-
// _right: undefined,
|
|
1324
|
-
// key: 0,
|
|
1325
|
-
// parent: undefined,
|
|
1326
|
-
// value: undefined
|
|
1327
|
-
// },
|
|
1328
|
-
// undefined
|
|
1329
|
-
// ]);
|
|
1330
|
-
//
|
|
1331
|
-
// const nodeUndefined = bst.keyValueNodeEntryRawToNodeAndValue(undefined);
|
|
1332
|
-
// expect(nodeUndefined).toEqual([undefined, undefined]);
|
|
1333
|
-
//
|
|
1334
|
-
// const nodeNull = bst.keyValueNodeEntryRawToNodeAndValue(null);
|
|
1335
|
-
// expect(nodeNull).toEqual([undefined, undefined]);
|
|
1336
|
-
// });
|
|
1337
|
-
});
|
|
1338
|
-
|
|
1339
|
-
describe('BST operations not map mode test recursively', () => {
|
|
1340
|
-
it('should perform various operations on a Binary Search Tree with numeric values', () => {
|
|
1341
|
-
const bst = new BST<number>([], {
|
|
1342
|
-
iterationType: 'RECURSIVE',
|
|
1343
|
-
isMapMode: false
|
|
1344
|
-
});
|
|
1345
|
-
expect(bst).toBeInstanceOf(BST);
|
|
1346
|
-
bst.set([11, 11]);
|
|
1347
|
-
bst.set([3, 3]);
|
|
1348
|
-
const idsAndValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
1349
|
-
bst.setMany(idsAndValues, undefined, false);
|
|
1350
|
-
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
1351
|
-
|
|
1352
|
-
if (bst.root) expect(bst.root.key).toBe(11);
|
|
1353
|
-
|
|
1354
|
-
expect(bst.size).toBe(16);
|
|
1355
|
-
|
|
1356
|
-
expect(bst.has(6)).toBe(true);
|
|
1357
|
-
|
|
1358
|
-
const node6 = bst.getNode(6);
|
|
1359
|
-
expect(node6 && bst.getHeight(6)).toBe(2);
|
|
1360
|
-
expect(node6 && bst.getDepth(6)).toBe(3);
|
|
1361
|
-
|
|
1362
|
-
const nodeId10 = bst.getNode(10);
|
|
1363
|
-
expect(bst.get(10)).toBe(undefined);
|
|
1364
|
-
expect(nodeId10?.key).toBe(10);
|
|
1365
|
-
|
|
1366
|
-
const nodeVal9 = bst.getNode(node => node.key === 9);
|
|
1367
|
-
expect(bst.get(nodeVal9?.key)).toBe(undefined);
|
|
1368
|
-
});
|
|
1369
|
-
|
|
1370
|
-
it('should perform various operations on a Binary Search Tree with object values', () => {
|
|
1371
|
-
const objBST = new BST<number, { key: number; keyA: number }>([], { isMapMode: false });
|
|
1372
|
-
expect(objBST).toBeInstanceOf(BST);
|
|
1373
|
-
objBST.set([11, { key: 11, keyA: 11 }]);
|
|
1374
|
-
objBST.set([3, { key: 3, keyA: 3 }]);
|
|
1375
|
-
const entries: [number, { key: number; keyA: number }][] = [
|
|
1376
|
-
[15, { key: 15, keyA: 15 }],
|
|
1377
|
-
[1, { key: 1, keyA: 1 }],
|
|
1378
|
-
[8, { key: 8, keyA: 8 }],
|
|
1379
|
-
[13, { key: 13, keyA: 13 }],
|
|
1380
|
-
[16, { key: 16, keyA: 16 }],
|
|
1381
|
-
[2, { key: 2, keyA: 2 }],
|
|
1382
|
-
[6, { key: 6, keyA: 6 }],
|
|
1383
|
-
[9, { key: 9, keyA: 9 }],
|
|
1384
|
-
[12, { key: 12, keyA: 12 }],
|
|
1385
|
-
[14, { key: 14, keyA: 14 }],
|
|
1386
|
-
[4, { key: 4, keyA: 4 }],
|
|
1387
|
-
[7, { key: 7, keyA: 7 }],
|
|
1388
|
-
[10, { key: 10, keyA: 10 }],
|
|
1389
|
-
[5, { key: 5, keyA: 5 }]
|
|
1390
|
-
];
|
|
1391
|
-
|
|
1392
|
-
objBST.setMany(entries, undefined, false);
|
|
1393
|
-
|
|
1394
|
-
expect(objBST.root).toBeInstanceOf(BSTNode);
|
|
1395
|
-
|
|
1396
|
-
if (objBST.root) expect(objBST.root.key).toBe(11);
|
|
1397
|
-
|
|
1398
|
-
expect(objBST.has(6)).toBe(true);
|
|
1399
|
-
|
|
1400
|
-
const node6 = objBST.getNode(6);
|
|
1401
|
-
expect(objBST.get(6)).toEqual({
|
|
1402
|
-
key: 6,
|
|
1403
|
-
keyA: 6
|
|
1404
|
-
});
|
|
1405
|
-
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
|
1406
|
-
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
|
1407
|
-
|
|
1408
|
-
const nodeId10 = objBST.getNode(10);
|
|
1409
|
-
expect(nodeId10?.key).toBe(10);
|
|
1410
|
-
|
|
1411
|
-
const nodeVal9 = objBST.getNode(9);
|
|
1412
|
-
expect(nodeVal9?.key).toBe(9);
|
|
1413
|
-
|
|
1414
|
-
const leftMost = objBST.getLeftMost();
|
|
1415
|
-
expect(leftMost).toBe(1);
|
|
1416
|
-
|
|
1417
|
-
const node15 = objBST.getNode(15);
|
|
1418
|
-
expect(objBST.get(node15)).toEqual({
|
|
1419
|
-
key: 15,
|
|
1420
|
-
keyA: 15
|
|
1421
|
-
});
|
|
1422
|
-
});
|
|
1423
|
-
});
|
|
1424
|
-
|
|
1425
|
-
describe('BST iterative methods not map mode test', () => {
|
|
1426
|
-
let bst: BST<number, string>;
|
|
1427
|
-
beforeEach(() => {
|
|
1428
|
-
bst = new BST();
|
|
1429
|
-
bst.setMany(
|
|
1430
|
-
[
|
|
1431
|
-
[1, 'a'],
|
|
1432
|
-
[2, 'b'],
|
|
1433
|
-
[3, 'c']
|
|
1434
|
-
],
|
|
1435
|
-
[],
|
|
1436
|
-
false
|
|
1437
|
-
);
|
|
1438
|
-
});
|
|
1439
|
-
|
|
1440
|
-
it('should clone work well', () => {
|
|
1441
|
-
const cloned = bst.clone();
|
|
1442
|
-
expect(cloned.root?.left).toBe(undefined);
|
|
1443
|
-
expect(cloned.get(cloned.root?.right)).toBe('b');
|
|
1444
|
-
});
|
|
1445
|
-
|
|
1446
|
-
it('should collapsed, unbalanced, balanced bst leaves', () => {
|
|
1447
|
-
const collapsedToLinkedList = new BST();
|
|
1448
|
-
collapsedToLinkedList.setMany(
|
|
1449
|
-
[
|
|
1450
|
-
[1, 'a'],
|
|
1451
|
-
[2, 'b'],
|
|
1452
|
-
[3, 'c'],
|
|
1453
|
-
[4, 'd'],
|
|
1454
|
-
[5, 'e'],
|
|
1455
|
-
[6, 'f'],
|
|
1456
|
-
[7, 'g'],
|
|
1457
|
-
[8, 'h'],
|
|
1458
|
-
[9, 'i']
|
|
1459
|
-
],
|
|
1460
|
-
[],
|
|
1461
|
-
false
|
|
1462
|
-
);
|
|
1463
|
-
|
|
1464
|
-
expect(collapsedToLinkedList.leaves()).toEqual([9]);
|
|
1465
|
-
|
|
1466
|
-
const unbalanced = new BST();
|
|
1467
|
-
unbalanced.setMany(
|
|
1468
|
-
[
|
|
1469
|
-
[2, 'b'],
|
|
1470
|
-
[1, 'a'],
|
|
1471
|
-
[3, 'c'],
|
|
1472
|
-
[4, 'd'],
|
|
1473
|
-
[5, 'e'],
|
|
1474
|
-
[6, 'f'],
|
|
1475
|
-
[7, 'g'],
|
|
1476
|
-
[8, 'h'],
|
|
1477
|
-
[9, 'i']
|
|
1478
|
-
],
|
|
1479
|
-
[],
|
|
1480
|
-
false
|
|
1481
|
-
);
|
|
1482
|
-
|
|
1483
|
-
expect(unbalanced.leaves()).toEqual([1, 9]);
|
|
1484
|
-
|
|
1485
|
-
const balanced = new BST();
|
|
1486
|
-
balanced.setMany(
|
|
1487
|
-
[
|
|
1488
|
-
[2, 'b'],
|
|
1489
|
-
[1, 'a'],
|
|
1490
|
-
[3, 'c'],
|
|
1491
|
-
[4, 'd'],
|
|
1492
|
-
[5, 'e'],
|
|
1493
|
-
[6, 'f'],
|
|
1494
|
-
[7, 'g'],
|
|
1495
|
-
[8, 'h'],
|
|
1496
|
-
[9, 'i']
|
|
1497
|
-
],
|
|
1498
|
-
[],
|
|
1499
|
-
true
|
|
1500
|
-
);
|
|
1501
|
-
|
|
1502
|
-
expect(balanced.leaves()).toEqual([1, 6, 4, 9]);
|
|
1503
|
-
expect(balanced.leaves(node => balanced.get(node?.key))).toEqual(['a', 'f', 'd', 'i']);
|
|
1504
|
-
});
|
|
1505
|
-
});
|
|
1506
|
-
|
|
1507
|
-
describe('BST constructor and comparator edge cases', () => {
|
|
1508
|
-
it('should support comparator', () => {
|
|
1509
|
-
const bst = new BST<number>([], {
|
|
1510
|
-
comparator: (a, b) => b - a
|
|
1511
|
-
});
|
|
1512
|
-
bst.set(1);
|
|
1513
|
-
bst.set(2);
|
|
1514
|
-
expect([...bst.keys()]).toEqual([2, 1]);
|
|
1515
|
-
});
|
|
1516
|
-
|
|
1517
|
-
it('should throw if compare object key without comparator', () => {
|
|
1518
|
-
const bst = new BST<any>();
|
|
1519
|
-
expect(() => bst.comparator({ a: 1 }, { a: 2 })).toThrow();
|
|
1520
|
-
});
|
|
1521
|
-
});
|
|
1522
|
-
|
|
1523
|
-
describe('BST addMany edge cases', () => {
|
|
1524
|
-
it('should addMany with values iterable', () => {
|
|
1525
|
-
const bst = new BST<number, string>();
|
|
1526
|
-
const keys = [1, 2, 3];
|
|
1527
|
-
const values = ['a', 'b', 'c'];
|
|
1528
|
-
const result = bst.setMany(keys, values);
|
|
1529
|
-
expect(result).toEqual([true, true, true]);
|
|
1530
|
-
expect([...bst]).toEqual([
|
|
1531
|
-
[1, 'a'],
|
|
1532
|
-
[2, 'b'],
|
|
1533
|
-
[3, 'c']
|
|
1534
|
-
]);
|
|
1535
|
-
});
|
|
1536
|
-
|
|
1537
|
-
it('should addMany with isBalanceAdd=false', () => {
|
|
1538
|
-
const bst = new BST<number>();
|
|
1539
|
-
const result = bst.setMany([3, 1, 2], undefined, false);
|
|
1540
|
-
expect(result).toEqual([true, true, true]);
|
|
1541
|
-
expect([...bst.keys()]).toEqual([1, 2, 3]);
|
|
1542
|
-
});
|
|
1543
|
-
|
|
1544
|
-
it('should addMany with raw/entry/node', () => {
|
|
1545
|
-
const bst = new BST<number, string>([], { isMapMode: false });
|
|
1546
|
-
const node = new BSTNode(5, 'x');
|
|
1547
|
-
const result = bst.setMany([1, [2, 'b'], node]);
|
|
1548
|
-
expect(result).toEqual([true, true, true]);
|
|
1549
|
-
expect(bst.get(5)).toBe('x');
|
|
1550
|
-
});
|
|
1551
|
-
});
|
|
1552
|
-
|
|
1553
|
-
describe('BST perfectlyBalance and isAVLBalanced edge cases', () => {
|
|
1554
|
-
it('should perfectlyBalance with <1 node and both iterationType', () => {
|
|
1555
|
-
const bst = new BST<number>();
|
|
1556
|
-
expect(bst.perfectlyBalance('RECURSIVE')).toBe(false);
|
|
1557
|
-
expect(bst.perfectlyBalance('ITERATIVE')).toBe(false);
|
|
1558
|
-
bst.setMany([1, 2, 3]);
|
|
1559
|
-
expect(bst.perfectlyBalance('RECURSIVE')).toBe(true);
|
|
1560
|
-
bst.clear();
|
|
1561
|
-
bst.setMany([1, 2, 3]);
|
|
1562
|
-
expect(bst.perfectlyBalance('ITERATIVE')).toBe(true);
|
|
1563
|
-
});
|
|
1564
|
-
|
|
1565
|
-
it('should isAVLBalanced with both iterationType', () => {
|
|
1566
|
-
const bst = new BST<number>();
|
|
1567
|
-
expect(bst.isAVLBalanced('RECURSIVE')).toBe(true);
|
|
1568
|
-
expect(bst.isAVLBalanced('ITERATIVE')).toBe(true);
|
|
1569
|
-
bst.setMany([1, 2, 3, 4, 5]);
|
|
1570
|
-
expect(typeof bst.isAVLBalanced('RECURSIVE')).toBe('boolean');
|
|
1571
|
-
expect(typeof bst.isAVLBalanced('ITERATIVE')).toBe('boolean');
|
|
1572
|
-
});
|
|
1573
|
-
});
|
|
1574
|
-
|
|
1575
|
-
describe('BST _keyValueNodeOrEntryToNodeAndValue edge', () => {
|
|
1576
|
-
it('should return [undefined, undefined] for null', () => {
|
|
1577
|
-
const bst = new BST<number>();
|
|
1578
|
-
// @ts-ignore
|
|
1579
|
-
const result = bst['_keyValueNodeOrEntryToNodeAndValue'](null);
|
|
1580
|
-
expect(result).toEqual([undefined, undefined]);
|
|
1581
|
-
});
|
|
1582
|
-
});
|
|
1583
|
-
|
|
1584
|
-
describe('BST ceiling and higher', () => {
|
|
1585
|
-
let bst: BST<number, string>;
|
|
1586
|
-
|
|
1587
|
-
beforeEach(() => {
|
|
1588
|
-
// Create a BST with keys: [10, 5, 15, 3, 7, 12, 20, 1, 4, 6, 8, 11, 13, 18, 25]
|
|
1589
|
-
bst = new BST<number, string>([10, 5, 15, 3, 7, 12, 20, 1, 4, 6, 8, 11, 13, 18, 25]);
|
|
1590
|
-
});
|
|
1591
|
-
|
|
1592
|
-
describe('ceiling', () => {
|
|
1593
|
-
it('should return the key with exact key match', () => {
|
|
1594
|
-
// Test for key that exists in tree
|
|
1595
|
-
const key = bst.ceiling(10);
|
|
1596
|
-
expect(key).toBeDefined();
|
|
1597
|
-
expect(key).toBe(10);
|
|
1598
|
-
});
|
|
1599
|
-
|
|
1600
|
-
it('should return the smallest key >= key when exact match not found', () => {
|
|
1601
|
-
// Test for key 9 (doesn't exist, should return 10)
|
|
1602
|
-
const key = bst.ceiling(9);
|
|
1603
|
-
expect(key).toBeDefined();
|
|
1604
|
-
expect(key).toBe(10);
|
|
1605
|
-
});
|
|
1606
|
-
|
|
1607
|
-
it('should return key with key >= search key from multiple candidates', () => {
|
|
1608
|
-
// Test for key 6 (exists, should return 6)
|
|
1609
|
-
const key = bst.ceiling(6);
|
|
1610
|
-
expect(key).toBeDefined();
|
|
1611
|
-
expect(key).toBe(6);
|
|
1612
|
-
|
|
1613
|
-
// Test for key 7 (exists, should return 7)
|
|
1614
|
-
const key2 = bst.ceiling(7);
|
|
1615
|
-
expect(key2).toBeDefined();
|
|
1616
|
-
expect(key2).toBe(7);
|
|
1617
|
-
});
|
|
1618
|
-
|
|
1619
|
-
it('should return smallest key >= search key for non-existent key in middle range', () => {
|
|
1620
|
-
// Test for key 14 (doesn't exist, should return 15)
|
|
1621
|
-
const key = bst.ceiling(14);
|
|
1622
|
-
expect(key).toBeDefined();
|
|
1623
|
-
expect(key).toBe(15);
|
|
1624
|
-
|
|
1625
|
-
// Test for key 11.5 (doesn't exist, should return 12)
|
|
1626
|
-
const key2 = bst.ceiling(11.5);
|
|
1627
|
-
expect(key2).toBeDefined();
|
|
1628
|
-
expect(key2).toBe(12);
|
|
1629
|
-
});
|
|
1630
|
-
|
|
1631
|
-
it('should return smallest key when search key is less than minimum', () => {
|
|
1632
|
-
// Test for key 0 (should return 1, the minimum)
|
|
1633
|
-
const key = bst.ceiling(0);
|
|
1634
|
-
expect(key).toBeDefined();
|
|
1635
|
-
expect(key).toBe(1);
|
|
1636
|
-
|
|
1637
|
-
// Test for key -100 (should return 1, the minimum)
|
|
1638
|
-
const key2 = bst.ceiling(-100);
|
|
1639
|
-
expect(key2).toBeDefined();
|
|
1640
|
-
expect(key2).toBe(1);
|
|
1641
|
-
});
|
|
1642
|
-
|
|
1643
|
-
it('should return undefined when search key is greater than maximum', () => {
|
|
1644
|
-
// Test for key 100 (no key >= 100, should return undefined)
|
|
1645
|
-
const key = bst.ceiling(100);
|
|
1646
|
-
expect(key).toBeUndefined();
|
|
1647
|
-
|
|
1648
|
-
// Test for key 26 (no key >= 26, should return undefined)
|
|
1649
|
-
const key2 = bst.ceiling(26);
|
|
1650
|
-
expect(key2).toBeUndefined();
|
|
1651
|
-
});
|
|
1652
|
-
|
|
1653
|
-
it('should return correct key for edge cases', () => {
|
|
1654
|
-
// Test for key 25 (maximum, should return 25)
|
|
1655
|
-
const key = bst.ceiling(25);
|
|
1656
|
-
expect(key).toBeDefined();
|
|
1657
|
-
expect(key).toBe(25);
|
|
1658
|
-
|
|
1659
|
-
// Test for key 1 (minimum, should return 1)
|
|
1660
|
-
const key2 = bst.ceiling(1);
|
|
1661
|
-
expect(key2).toBeDefined();
|
|
1662
|
-
expect(key2).toBe(1);
|
|
1663
|
-
});
|
|
1664
|
-
});
|
|
1665
|
-
|
|
1666
|
-
describe('higher', () => {
|
|
1667
|
-
it('should return the smallest key > search key', () => {
|
|
1668
|
-
// Test for key 10 (exists, should return next key > 10, which is 11)
|
|
1669
|
-
const key = bst.higher(10);
|
|
1670
|
-
expect(key).toBeDefined();
|
|
1671
|
-
expect(key).toBe(11);
|
|
1672
|
-
});
|
|
1673
|
-
|
|
1674
|
-
it('should return the smallest key > search key for non-existent key', () => {
|
|
1675
|
-
// Test for key 9 (doesn't exist, should return 10, the smallest key > 9)
|
|
1676
|
-
const key = bst.higher(9);
|
|
1677
|
-
expect(key).toBeDefined();
|
|
1678
|
-
expect(key).toBe(10);
|
|
1679
|
-
|
|
1680
|
-
// Test for key 14 (doesn't exist, should return 15)
|
|
1681
|
-
const key2 = bst.higher(14);
|
|
1682
|
-
expect(key2).toBeDefined();
|
|
1683
|
-
expect(key2).toBe(15);
|
|
1684
|
-
});
|
|
1685
|
-
|
|
1686
|
-
it('should skip equal keys and return the next larger key', () => {
|
|
1687
|
-
// Test for key 5 (exists, should return 6, the smallest key > 5)
|
|
1688
|
-
const key = bst.higher(5);
|
|
1689
|
-
expect(key).toBeDefined();
|
|
1690
|
-
expect(key).toBe(6);
|
|
1691
|
-
|
|
1692
|
-
// Test for key 20 (exists, should return 25)
|
|
1693
|
-
const key2 = bst.higher(20);
|
|
1694
|
-
expect(key2).toBeDefined();
|
|
1695
|
-
expect(key2).toBe(25);
|
|
1696
|
-
});
|
|
1697
|
-
|
|
1698
|
-
it('should return smallest key > search key for non-existent key in middle range', () => {
|
|
1699
|
-
// Test for key 11.5 (doesn't exist, should return 12)
|
|
1700
|
-
const key = bst.higher(11.5);
|
|
1701
|
-
expect(key).toBeDefined();
|
|
1702
|
-
expect(key).toBe(12);
|
|
1703
|
-
|
|
1704
|
-
// Test for key 19 (doesn't exist, should return 20)
|
|
1705
|
-
const key2 = bst.higher(19);
|
|
1706
|
-
expect(key2).toBeDefined();
|
|
1707
|
-
expect(key2).toBe(20);
|
|
1708
|
-
});
|
|
1709
|
-
|
|
1710
|
-
it('should return smallest key when search key is less than minimum', () => {
|
|
1711
|
-
// Test for key 0 (should return 1, the minimum)
|
|
1712
|
-
const key = bst.higher(0);
|
|
1713
|
-
expect(key).toBeDefined();
|
|
1714
|
-
expect(key).toBe(1);
|
|
1715
|
-
|
|
1716
|
-
// Test for key -100 (should return 1, the minimum)
|
|
1717
|
-
const key2 = bst.higher(-100);
|
|
1718
|
-
expect(key2).toBeDefined();
|
|
1719
|
-
expect(key2).toBe(1);
|
|
1720
|
-
});
|
|
1721
|
-
|
|
1722
|
-
it('should return undefined when search key is >= maximum', () => {
|
|
1723
|
-
// Test for key 100 (no key > 100, should return undefined)
|
|
1724
|
-
const key = bst.higher(100);
|
|
1725
|
-
expect(key).toBeUndefined();
|
|
1726
|
-
|
|
1727
|
-
// Test for key 25 (maximum, no key > 25, should return undefined)
|
|
1728
|
-
const key2 = bst.higher(25);
|
|
1729
|
-
expect(key2).toBeUndefined();
|
|
1730
|
-
|
|
1731
|
-
// Test for key 26 (no key > 26, should return undefined)
|
|
1732
|
-
const key3 = bst.higher(26);
|
|
1733
|
-
expect(key3).toBeUndefined();
|
|
1734
|
-
});
|
|
1735
|
-
|
|
1736
|
-
it('should return correct key for edge cases', () => {
|
|
1737
|
-
// Test for key 1 (minimum, should return next key 3)
|
|
1738
|
-
const key = bst.higher(1);
|
|
1739
|
-
expect(key).toBeDefined();
|
|
1740
|
-
expect(key).toBe(3);
|
|
1741
|
-
|
|
1742
|
-
// Test for key 24 (doesn't exist, should return 25)
|
|
1743
|
-
const key2 = bst.higher(24);
|
|
1744
|
-
expect(key2).toBeDefined();
|
|
1745
|
-
expect(key2).toBe(25);
|
|
1746
|
-
});
|
|
1747
|
-
});
|
|
1748
|
-
|
|
1749
|
-
describe('ceiling vs higher comparison', () => {
|
|
1750
|
-
it('should demonstrate difference between ceiling and higher', () => {
|
|
1751
|
-
const searchKey = 12;
|
|
1752
|
-
|
|
1753
|
-
// ceiling(12) should return 12 (key >= 12)
|
|
1754
|
-
const lower = bst.ceiling(searchKey);
|
|
1755
|
-
expect(lower).toBe(12);
|
|
1756
|
-
|
|
1757
|
-
// higher(12) should return 13 (key > 12)
|
|
1758
|
-
const upper = bst.higher(searchKey);
|
|
1759
|
-
expect(upper).toBe(13);
|
|
1760
|
-
});
|
|
1761
|
-
|
|
1762
|
-
it('should return same result for non-existent key in both methods', () => {
|
|
1763
|
-
const searchKey = 11.5;
|
|
1764
|
-
|
|
1765
|
-
// Both should return 12 (smallest key >= 11.5 for ceiling, smallest key > 11.5 for higher)
|
|
1766
|
-
const lower = bst.ceiling(searchKey);
|
|
1767
|
-
const upper = bst.higher(searchKey);
|
|
1768
|
-
expect(lower).toBe(12);
|
|
1769
|
-
expect(upper).toBe(12);
|
|
1770
|
-
});
|
|
1771
|
-
});
|
|
1772
|
-
|
|
1773
|
-
describe('BST Bound Methods Comprehensive Tests', () => {
|
|
1774
|
-
let bst: BST<number, string>;
|
|
1775
|
-
|
|
1776
|
-
// Helper: Generate random number array
|
|
1777
|
-
const generateRandomArray = (size: number, min: number, max: number) => {
|
|
1778
|
-
return Array.from({ length: size }, () => Math.floor(Math.random() * (max - min + 1)) + min);
|
|
1779
|
-
};
|
|
1780
|
-
|
|
1781
|
-
beforeEach(() => {
|
|
1782
|
-
// Construct a standard test tree
|
|
1783
|
-
// Structure:
|
|
1784
|
-
// 10
|
|
1785
|
-
// / \
|
|
1786
|
-
// 5 15
|
|
1787
|
-
// / \ / \
|
|
1788
|
-
// 2 8 12 20
|
|
1789
|
-
// / \
|
|
1790
|
-
// 6 9
|
|
1791
|
-
bst = new BST<number, string>();
|
|
1792
|
-
const keys = [10, 5, 15, 2, 8, 12, 20, 6, 9];
|
|
1793
|
-
keys.forEach(k => bst.set(k, `val-${k}`));
|
|
1794
|
-
});
|
|
1795
|
-
|
|
1796
|
-
describe('ceiling (First key >= key)', () => {
|
|
1797
|
-
test('should return strict match if key exists', () => {
|
|
1798
|
-
// Case: Key exists
|
|
1799
|
-
expect(bst.ceiling(10)).toBe(10);
|
|
1800
|
-
expect(bst.ceiling(6)).toBe(6);
|
|
1801
|
-
expect(bst.ceiling(20)).toBe(20);
|
|
1802
|
-
});
|
|
1803
|
-
|
|
1804
|
-
test('should return next larger key if key does not exist', () => {
|
|
1805
|
-
// Case: Key doesn't exist, but falls within range
|
|
1806
|
-
expect(bst.ceiling(7)).toBe(8); // 7 -> 8
|
|
1807
|
-
expect(bst.ceiling(11)).toBe(12); // 11 -> 12
|
|
1808
|
-
expect(bst.ceiling(3)).toBe(5); // 3 -> 5
|
|
1809
|
-
});
|
|
1810
|
-
|
|
1811
|
-
test('should return smallest key if key is smaller than min', () => {
|
|
1812
|
-
// Case: Key is smaller than the minimum value in the tree
|
|
1813
|
-
expect(bst.ceiling(0)).toBe(2);
|
|
1814
|
-
expect(bst.ceiling(-100)).toBe(2);
|
|
1815
|
-
});
|
|
1816
|
-
|
|
1817
|
-
test('should return undefined if key is larger than max', () => {
|
|
1818
|
-
// Case: Key is larger than the maximum value in the tree
|
|
1819
|
-
expect(bst.ceiling(21)).toBeUndefined();
|
|
1820
|
-
expect(bst.ceiling(100)).toBeUndefined();
|
|
1821
|
-
});
|
|
1822
|
-
|
|
1823
|
-
test('should work with IterationType.RECURSIVE explicitly', () => {
|
|
1824
|
-
expect(bst.ceiling(7)).toBe(8);
|
|
1825
|
-
expect(bst.ceiling(10)).toBe(10);
|
|
1826
|
-
expect(bst.ceiling(21)).toBeUndefined();
|
|
1827
|
-
});
|
|
1828
|
-
|
|
1829
|
-
test('should work with IterationType.ITERATIVE explicitly', () => {
|
|
1830
|
-
expect(bst.ceiling(7)).toBe(8);
|
|
1831
|
-
expect(bst.ceiling(10)).toBe(10);
|
|
1832
|
-
expect(bst.ceiling(21)).toBeUndefined();
|
|
1833
|
-
});
|
|
1834
|
-
});
|
|
1835
|
-
|
|
1836
|
-
describe('higher (First key > key)', () => {
|
|
1837
|
-
test('should return next larger key even if key exists', () => {
|
|
1838
|
-
// Case: Key exists, but we want strictly greater
|
|
1839
|
-
expect(bst.higher(10)).toBe(12); // > 10 is 12
|
|
1840
|
-
expect(bst.higher(6)).toBe(8); // > 6 is 8
|
|
1841
|
-
expect(bst.higher(20)).toBeUndefined(); // > 20 is undefined
|
|
1842
|
-
});
|
|
1843
|
-
|
|
1844
|
-
test('should return next larger key if key does not exist', () => {
|
|
1845
|
-
// Case: Key doesn't exist
|
|
1846
|
-
expect(bst.higher(7)).toBe(8);
|
|
1847
|
-
expect(bst.higher(11)).toBe(12);
|
|
1848
|
-
});
|
|
1849
|
-
|
|
1850
|
-
test('should return undefined if key is larger than or equal to max', () => {
|
|
1851
|
-
expect(bst.higher(20)).toBeUndefined();
|
|
1852
|
-
expect(bst.higher(21)).toBeUndefined();
|
|
1853
|
-
});
|
|
1854
|
-
|
|
1855
|
-
test('should work with IterationType.RECURSIVE explicitly', () => {
|
|
1856
|
-
expect(bst.higher(10)).toBe(12);
|
|
1857
|
-
expect(bst.higher(20)).toBeUndefined();
|
|
1858
|
-
});
|
|
1859
|
-
});
|
|
1860
|
-
|
|
1861
|
-
describe('Edge Cases', () => {
|
|
1862
|
-
test('should handle empty tree', () => {
|
|
1863
|
-
const emptyTree = new BST<number, string>();
|
|
1864
|
-
expect(emptyTree.ceiling(10)).toBeUndefined();
|
|
1865
|
-
expect(emptyTree.higher(10)).toBeUndefined();
|
|
1866
|
-
});
|
|
1867
|
-
|
|
1868
|
-
test('should handle single node tree', () => {
|
|
1869
|
-
const singleNodeTree = new BST<number, string>();
|
|
1870
|
-
singleNodeTree.set(10);
|
|
1871
|
-
|
|
1872
|
-
// ceiling
|
|
1873
|
-
expect(singleNodeTree.ceiling(5)).toBe(10);
|
|
1874
|
-
expect(singleNodeTree.ceiling(10)).toBe(10);
|
|
1875
|
-
expect(singleNodeTree.ceiling(15)).toBeUndefined();
|
|
1876
|
-
|
|
1877
|
-
// higher
|
|
1878
|
-
expect(singleNodeTree.higher(5)).toBe(10);
|
|
1879
|
-
expect(singleNodeTree.higher(10)).toBeUndefined();
|
|
1880
|
-
});
|
|
1881
|
-
});
|
|
1882
|
-
|
|
1883
|
-
describe('Consistency & Fuzz Testing', () => {
|
|
1884
|
-
test('Recursive and Iterative implementations should match on random data', () => {
|
|
1885
|
-
const fuzzTree = new BST<number, number>();
|
|
1886
|
-
// Generate 500 random numbers between 0-1000
|
|
1887
|
-
const randomKeys = generateRandomArray(500, 0, 1000);
|
|
1888
|
-
|
|
1889
|
-
// Insert unique keys
|
|
1890
|
-
[...new Set(randomKeys)].forEach(k => fuzzTree.set(k));
|
|
1891
|
-
|
|
1892
|
-
// Sort for verification (Ground Truth)
|
|
1893
|
-
const sortedKeys = Array.from(fuzzTree.keys()).sort((a, b) => a - b);
|
|
1894
|
-
|
|
1895
|
-
// Test with 200 random queries (including existing and non-existing keys)
|
|
1896
|
-
const testQueries = generateRandomArray(200, 0, 1000);
|
|
1897
|
-
|
|
1898
|
-
testQueries.forEach(queryKey => {
|
|
1899
|
-
// 1. Verify ceiling
|
|
1900
|
-
const recLower = fuzzTree.ceiling(queryKey);
|
|
1901
|
-
const iterLower = fuzzTree.ceiling(queryKey);
|
|
1902
|
-
|
|
1903
|
-
// Verify consistency between recursive and iterative
|
|
1904
|
-
expect(recLower).toBe(iterLower);
|
|
1905
|
-
|
|
1906
|
-
// Verify logic correctness (compare with Array.find)
|
|
1907
|
-
const expectedLower = sortedKeys.find(k => k >= queryKey);
|
|
1908
|
-
expect(recLower).toBe(expectedLower);
|
|
1909
|
-
|
|
1910
|
-
// 2. Verify higher
|
|
1911
|
-
const recUpper = fuzzTree.higher(queryKey);
|
|
1912
|
-
const iterUpper = fuzzTree.higher(queryKey);
|
|
1913
|
-
|
|
1914
|
-
// Verify consistency
|
|
1915
|
-
expect(recUpper).toBe(iterUpper);
|
|
1916
|
-
|
|
1917
|
-
// Verify logic correctness
|
|
1918
|
-
const expectedUpper = sortedKeys.find(k => k > queryKey);
|
|
1919
|
-
expect(recUpper).toBe(expectedUpper);
|
|
1920
|
-
});
|
|
1921
|
-
});
|
|
1922
|
-
});
|
|
1923
|
-
});
|
|
1924
|
-
|
|
1925
|
-
describe('empty tree', () => {
|
|
1926
|
-
beforeEach(() => {
|
|
1927
|
-
bst = new BST<number, string>();
|
|
1928
|
-
});
|
|
1929
|
-
|
|
1930
|
-
it('ceiling should return undefined on empty tree', () => {
|
|
1931
|
-
const key = bst.ceiling(10);
|
|
1932
|
-
expect(key).toBeUndefined();
|
|
1933
|
-
});
|
|
1934
|
-
|
|
1935
|
-
it('higher should return undefined on empty tree', () => {
|
|
1936
|
-
const key = bst.higher(10);
|
|
1937
|
-
expect(key).toBeUndefined();
|
|
1938
|
-
});
|
|
1939
|
-
});
|
|
1940
|
-
|
|
1941
|
-
describe('single node tree', () => {
|
|
1942
|
-
beforeEach(() => {
|
|
1943
|
-
bst = new BST<number, string>();
|
|
1944
|
-
bst.set(10, 'ten');
|
|
1945
|
-
});
|
|
1946
|
-
|
|
1947
|
-
it('ceiling should return the key if key matches', () => {
|
|
1948
|
-
const key = bst.ceiling(10);
|
|
1949
|
-
expect(key).toBeDefined();
|
|
1950
|
-
expect(key).toBe(10);
|
|
1951
|
-
});
|
|
1952
|
-
|
|
1953
|
-
it('ceiling should return the key if search key is less', () => {
|
|
1954
|
-
const key = bst.ceiling(5);
|
|
1955
|
-
expect(key).toBeDefined();
|
|
1956
|
-
expect(key).toBe(10);
|
|
1957
|
-
});
|
|
1958
|
-
|
|
1959
|
-
it('ceiling should return undefined if search key is greater', () => {
|
|
1960
|
-
const key = bst.ceiling(15);
|
|
1961
|
-
expect(key).toBeUndefined();
|
|
1962
|
-
});
|
|
1963
|
-
|
|
1964
|
-
it('higher should return undefined if key matches', () => {
|
|
1965
|
-
const key = bst.higher(10);
|
|
1966
|
-
expect(key).toBeUndefined();
|
|
1967
|
-
});
|
|
1968
|
-
|
|
1969
|
-
it('higher should return the key if search key is less', () => {
|
|
1970
|
-
const key = bst.higher(5);
|
|
1971
|
-
expect(key).toBeDefined();
|
|
1972
|
-
expect(key).toBe(10);
|
|
1973
|
-
});
|
|
1974
|
-
|
|
1975
|
-
it('higher should return undefined if search key is greater', () => {
|
|
1976
|
-
const key = bst.higher(15);
|
|
1977
|
-
expect(key).toBeUndefined();
|
|
1978
|
-
});
|
|
1979
|
-
});
|
|
1980
|
-
|
|
1981
|
-
describe('practical LeetCode use case: Range search', () => {
|
|
1982
|
-
it('should find all keys in range [12, 20]', () => {
|
|
1983
|
-
// Use ceiling to find start of range and higher to find end
|
|
1984
|
-
const start = bst.ceiling(12);
|
|
1985
|
-
|
|
1986
|
-
expect(start).toBe(12);
|
|
1987
|
-
|
|
1988
|
-
// Let's verify we found the correct bounds
|
|
1989
|
-
expect(start).toBeGreaterThanOrEqual(12);
|
|
1990
|
-
|
|
1991
|
-
// Check overlap
|
|
1992
|
-
const rangeStart = bst.ceiling(12);
|
|
1993
|
-
const rangeEndInclusive = bst.ceiling(20); // Should be 20
|
|
1994
|
-
|
|
1995
|
-
expect(rangeStart).toBe(12);
|
|
1996
|
-
expect(rangeEndInclusive).toBe(20);
|
|
1997
|
-
});
|
|
1998
|
-
|
|
1999
|
-
it('should help identify interval overlaps', () => {
|
|
2000
|
-
// Check if interval [14, 19] overlaps with any existing interval
|
|
2001
|
-
// Use higher to find the first key > 19
|
|
2002
|
-
const nextKey = bst.higher(19);
|
|
2003
|
-
// Use ceiling to find the first key >= 14
|
|
2004
|
-
const firstInRange = bst.ceiling(14);
|
|
2005
|
-
|
|
2006
|
-
expect(firstInRange).toBe(15);
|
|
2007
|
-
expect(nextKey).toBe(20);
|
|
2008
|
-
});
|
|
2009
|
-
});
|
|
2010
|
-
});
|
|
2011
|
-
|
|
2012
|
-
describe('BST Range Query Methods', () => {
|
|
2013
|
-
let bst: BST<number, string>;
|
|
2014
|
-
|
|
2015
|
-
beforeEach(() => {
|
|
2016
|
-
// Create a balanced BST: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20]
|
|
2017
|
-
bst = new BST([10, 5, 15, 3, 7, 13, 17, 1, 4, 6, 9, 11, 14, 16, 19, 20]);
|
|
2018
|
-
});
|
|
2019
|
-
|
|
2020
|
-
describe('ceiling - finds >= key (minimum value >= target)', () => {
|
|
2021
|
-
test('should find ceiling when key exists', () => {
|
|
2022
|
-
const result = bst.ceiling(10);
|
|
2023
|
-
expect(result).toBeDefined();
|
|
2024
|
-
expect(result).toBe(10);
|
|
2025
|
-
});
|
|
2026
|
-
|
|
2027
|
-
test('should find ceiling when key does not exist but higher value exists', () => {
|
|
2028
|
-
const result = bst.ceiling(8);
|
|
2029
|
-
expect(result).toBeDefined();
|
|
2030
|
-
expect(result).toBe(9);
|
|
2031
|
-
});
|
|
2032
|
-
|
|
2033
|
-
test('should return undefined when no ceiling exists (key greater than all)', () => {
|
|
2034
|
-
const result = bst.ceiling(100);
|
|
2035
|
-
expect(result).toBeUndefined();
|
|
2036
|
-
});
|
|
2037
|
-
|
|
2038
|
-
test('should find minimum element as ceiling for key smaller than all', () => {
|
|
2039
|
-
const result = bst.ceiling(-10);
|
|
2040
|
-
expect(result).toBeDefined();
|
|
2041
|
-
expect(result).toBe(1);
|
|
2042
|
-
});
|
|
2043
|
-
|
|
2044
|
-
test('should handle ceiling with node input', () => {
|
|
2045
|
-
const targetNode = bst.getNode(7);
|
|
2046
|
-
expect(targetNode).toBeDefined();
|
|
2047
|
-
const result = bst.ceiling(targetNode!);
|
|
2048
|
-
expect(result).toBe(7);
|
|
2049
|
-
});
|
|
2050
|
-
|
|
2051
|
-
test('should handle ceiling with entry input', () => {
|
|
2052
|
-
const result = bst.ceiling([11, 'test']);
|
|
2053
|
-
expect(result).toBeDefined();
|
|
2054
|
-
expect(result).toBe(11);
|
|
2055
|
-
});
|
|
2056
|
-
|
|
2057
|
-
test('should handle null/undefined inputs', () => {
|
|
2058
|
-
expect(bst.ceiling(null)).toBeUndefined();
|
|
2059
|
-
expect(bst.ceiling(undefined)).toBeUndefined();
|
|
2060
|
-
});
|
|
2061
|
-
|
|
2062
|
-
test('should work with ITERATIVE mode', () => {
|
|
2063
|
-
const result = bst.ceiling(12);
|
|
2064
|
-
expect(result).toBeDefined();
|
|
2065
|
-
expect(result).toBe(13);
|
|
2066
|
-
});
|
|
2067
|
-
|
|
2068
|
-
test('should work with RECURSIVE mode', () => {
|
|
2069
|
-
const result = bst.ceiling(12);
|
|
2070
|
-
expect(result).toBeDefined();
|
|
2071
|
-
expect(result).toBe(13);
|
|
2072
|
-
});
|
|
2073
|
-
|
|
2074
|
-
test('should find exact match as ceiling', () => {
|
|
2075
|
-
const result = bst.ceiling(15);
|
|
2076
|
-
expect(result).toBeDefined();
|
|
2077
|
-
expect(result).toBe(15);
|
|
2078
|
-
});
|
|
2079
|
-
});
|
|
2080
|
-
|
|
2081
|
-
describe('higher - finds > key (minimum value > target)', () => {
|
|
2082
|
-
test('should find higher when key exists (exclude exact match)', () => {
|
|
2083
|
-
const result = bst.higher(10);
|
|
2084
|
-
expect(result).toBeDefined();
|
|
2085
|
-
expect(result).toBe(11);
|
|
2086
|
-
expect(result).not.toBe(10);
|
|
2087
|
-
});
|
|
2088
|
-
|
|
2089
|
-
test('should find higher when key does not exist', () => {
|
|
2090
|
-
const result = bst.higher(8);
|
|
2091
|
-
expect(result).toBeDefined();
|
|
2092
|
-
expect(result).toBe(9);
|
|
2093
|
-
});
|
|
2094
|
-
|
|
2095
|
-
test('should return undefined when no higher exists (key >= all)', () => {
|
|
2096
|
-
const result = bst.higher(20);
|
|
2097
|
-
expect(result).toBeUndefined();
|
|
2098
|
-
});
|
|
2099
|
-
|
|
2100
|
-
test('should find minimum element as higher for key < all', () => {
|
|
2101
|
-
const result = bst.higher(-10);
|
|
2102
|
-
expect(result).toBeDefined();
|
|
2103
|
-
expect(result).toBe(1);
|
|
2104
|
-
});
|
|
2105
|
-
|
|
2106
|
-
test('should not return the key itself', () => {
|
|
2107
|
-
const result = bst.higher(7);
|
|
2108
|
-
expect(result).not.toBe(7);
|
|
2109
|
-
expect(result).toBe(9);
|
|
2110
|
-
});
|
|
2111
|
-
|
|
2112
|
-
test('should handle higher with node input', () => {
|
|
2113
|
-
const targetNode = bst.getNode(5);
|
|
2114
|
-
expect(targetNode).toBeDefined();
|
|
2115
|
-
const result = bst.higher(targetNode!);
|
|
2116
|
-
expect(result).toBeGreaterThan(5);
|
|
2117
|
-
expect(result).toBe(6);
|
|
2118
|
-
});
|
|
2119
|
-
|
|
2120
|
-
test('should work with ITERATIVE mode', () => {
|
|
2121
|
-
const result = bst.higher(13);
|
|
2122
|
-
expect(result).toBeDefined();
|
|
2123
|
-
expect(result).toBe(14);
|
|
2124
|
-
});
|
|
2125
|
-
|
|
2126
|
-
test('should work with RECURSIVE mode', () => {
|
|
2127
|
-
const result = bst.higher(13);
|
|
2128
|
-
expect(result).toBeDefined();
|
|
2129
|
-
expect(result).toBe(14);
|
|
2130
|
-
});
|
|
2131
|
-
});
|
|
2132
|
-
|
|
2133
|
-
describe('floor - finds <= key (maximum value <= target)', () => {
|
|
2134
|
-
test('should find floor when key exists', () => {
|
|
2135
|
-
const result = bst.floor(10);
|
|
2136
|
-
expect(result).toBeDefined();
|
|
2137
|
-
expect(result).toBe(10);
|
|
2138
|
-
});
|
|
2139
|
-
|
|
2140
|
-
test('should find floor when key does not exist but lower value exists', () => {
|
|
2141
|
-
const result = bst.floor(12);
|
|
2142
|
-
expect(result).toBeDefined();
|
|
2143
|
-
expect(result).toBe(11);
|
|
2144
|
-
});
|
|
2145
|
-
|
|
2146
|
-
test('should return undefined when no floor exists (key less than all)', () => {
|
|
2147
|
-
const result = bst.floor(-10);
|
|
2148
|
-
expect(result).toBeUndefined();
|
|
2149
|
-
});
|
|
2150
|
-
|
|
2151
|
-
test('should find maximum element as floor for key greater than all', () => {
|
|
2152
|
-
const result = bst.floor(100);
|
|
2153
|
-
expect(result).toBeDefined();
|
|
2154
|
-
expect(result).toBe(20);
|
|
2155
|
-
});
|
|
2156
|
-
|
|
2157
|
-
test('should handle floor with node input', () => {
|
|
2158
|
-
const targetNode = bst.getNode(13);
|
|
2159
|
-
expect(targetNode).toBeDefined();
|
|
2160
|
-
const result = bst.floor(targetNode!);
|
|
2161
|
-
expect(result).toBe(13);
|
|
2162
|
-
});
|
|
2163
|
-
|
|
2164
|
-
test('should handle floor with entry input', () => {
|
|
2165
|
-
const result = bst.floor([16, 'test']);
|
|
2166
|
-
expect(result).toBeDefined();
|
|
2167
|
-
expect(result).toBe(16);
|
|
2168
|
-
});
|
|
2169
|
-
|
|
2170
|
-
test('should handle null/undefined inputs', () => {
|
|
2171
|
-
expect(bst.floor(null)).toBeUndefined();
|
|
2172
|
-
expect(bst.floor(undefined)).toBeUndefined();
|
|
2173
|
-
});
|
|
2174
|
-
|
|
2175
|
-
test('should work with ITERATIVE mode', () => {
|
|
2176
|
-
const result = bst.floor(12);
|
|
2177
|
-
expect(result).toBeDefined();
|
|
2178
|
-
expect(result).toBe(11);
|
|
2179
|
-
});
|
|
2180
|
-
|
|
2181
|
-
test('should work with RECURSIVE mode', () => {
|
|
2182
|
-
const result = bst.floor(12);
|
|
2183
|
-
expect(result).toBeDefined();
|
|
2184
|
-
expect(result).toBe(11);
|
|
2185
|
-
});
|
|
2186
|
-
|
|
2187
|
-
test('should find exact match as floor', () => {
|
|
2188
|
-
const result = bst.floor(15);
|
|
2189
|
-
expect(result).toBeDefined();
|
|
2190
|
-
expect(result).toBe(15);
|
|
2191
|
-
});
|
|
2192
|
-
|
|
2193
|
-
test('should correctly find floor between two keys', () => {
|
|
2194
|
-
const result = bst.floor(8);
|
|
2195
|
-
expect(result).toBeDefined();
|
|
2196
|
-
expect(result).toBe(7);
|
|
2197
|
-
expect(result).toBeLessThan(8);
|
|
2198
|
-
});
|
|
2199
|
-
});
|
|
2200
|
-
|
|
2201
|
-
describe('lower - finds < key (maximum value < target)', () => {
|
|
2202
|
-
test('should find lower when key exists (exclude exact match)', () => {
|
|
2203
|
-
const result = bst.lower(10);
|
|
2204
|
-
expect(result).toBeDefined();
|
|
2205
|
-
expect(result).toBe(9);
|
|
2206
|
-
expect(result).not.toBe(10);
|
|
2207
|
-
});
|
|
2208
|
-
|
|
2209
|
-
test('should find lower when key does not exist', () => {
|
|
2210
|
-
const result = bst.lower(12);
|
|
2211
|
-
expect(result).toBeDefined();
|
|
2212
|
-
expect(result).toBe(11);
|
|
2213
|
-
});
|
|
2214
|
-
|
|
2215
|
-
test('should return undefined when no lower exists (key <= all)', () => {
|
|
2216
|
-
const result = bst.lower(1);
|
|
2217
|
-
expect(result).toBeUndefined();
|
|
2218
|
-
});
|
|
2219
|
-
|
|
2220
|
-
test('should find maximum element as lower for key > all', () => {
|
|
2221
|
-
const result = bst.lower(100);
|
|
2222
|
-
expect(result).toBeDefined();
|
|
2223
|
-
expect(result).toBe(20);
|
|
2224
|
-
});
|
|
2225
|
-
|
|
2226
|
-
test('should not return the key itself', () => {
|
|
2227
|
-
const result = bst.lower(15);
|
|
2228
|
-
expect(result).not.toBe(15);
|
|
2229
|
-
expect(result).toBe(14);
|
|
2230
|
-
});
|
|
2231
|
-
|
|
2232
|
-
test('should handle lower with node input', () => {
|
|
2233
|
-
const targetNode = bst.getNode(13);
|
|
2234
|
-
expect(targetNode).toBeDefined();
|
|
2235
|
-
const result = bst.lower(targetNode!);
|
|
2236
|
-
expect(result).toBeLessThan(13);
|
|
2237
|
-
expect(result).toBe(11);
|
|
2238
|
-
});
|
|
2239
|
-
|
|
2240
|
-
test('should handle lower with entry input', () => {
|
|
2241
|
-
const result = bst.lower([17, 'test']);
|
|
2242
|
-
expect(result).toBeDefined();
|
|
2243
|
-
expect(result).toBe(16);
|
|
2244
|
-
});
|
|
2245
|
-
|
|
2246
|
-
test('should work with ITERATIVE mode', () => {
|
|
2247
|
-
const result = bst.lower(14);
|
|
2248
|
-
expect(result).toBeDefined();
|
|
2249
|
-
expect(result).toBe(13);
|
|
2250
|
-
});
|
|
2251
|
-
|
|
2252
|
-
test('should work with RECURSIVE mode', () => {
|
|
2253
|
-
const result = bst.lower(14);
|
|
2254
|
-
expect(result).toBeDefined();
|
|
2255
|
-
expect(result).toBe(13);
|
|
2256
|
-
});
|
|
2257
|
-
});
|
|
2258
|
-
|
|
2259
|
-
describe('Edge cases and special scenarios', () => {
|
|
2260
|
-
test('single element tree - ceiling', () => {
|
|
2261
|
-
const singleBst = new BST([5]);
|
|
2262
|
-
expect(singleBst.ceiling(5)).toBe(5);
|
|
2263
|
-
expect(singleBst.ceiling(3)).toBe(5);
|
|
2264
|
-
expect(singleBst.ceiling(7)).toBeUndefined();
|
|
2265
|
-
});
|
|
2266
|
-
|
|
2267
|
-
test('single element tree - higher', () => {
|
|
2268
|
-
const singleBst = new BST([5]);
|
|
2269
|
-
expect(singleBst.higher(5)).toBeUndefined();
|
|
2270
|
-
expect(singleBst.higher(3)).toBe(5);
|
|
2271
|
-
});
|
|
2272
|
-
|
|
2273
|
-
test('single element tree - floor', () => {
|
|
2274
|
-
const singleBst = new BST([5]);
|
|
2275
|
-
expect(singleBst.floor(5)).toBe(5);
|
|
2276
|
-
expect(singleBst.floor(7)).toBe(5);
|
|
2277
|
-
expect(singleBst.floor(3)).toBeUndefined();
|
|
2278
|
-
});
|
|
2279
|
-
|
|
2280
|
-
test('single element tree - lower', () => {
|
|
2281
|
-
const singleBst = new BST([5]);
|
|
2282
|
-
expect(singleBst.lower(5)).toBeUndefined();
|
|
2283
|
-
expect(singleBst.lower(7)).toBe(5);
|
|
2284
|
-
});
|
|
2285
|
-
|
|
2286
|
-
test('empty tree handling', () => {
|
|
2287
|
-
const emptyBst = new BST<number, string>();
|
|
2288
|
-
expect(emptyBst.ceiling(5)).toBeUndefined();
|
|
2289
|
-
expect(emptyBst.higher(5)).toBeUndefined();
|
|
2290
|
-
expect(emptyBst.floor(5)).toBeUndefined();
|
|
2291
|
-
expect(emptyBst.lower(5)).toBeUndefined();
|
|
2292
|
-
});
|
|
2293
|
-
|
|
2294
|
-
test('ceiling and floor of adjacent keys', () => {
|
|
2295
|
-
const ceiling = bst.ceiling(5);
|
|
2296
|
-
const floor = bst.floor(6);
|
|
2297
|
-
expect(ceiling).toBe(5);
|
|
2298
|
-
expect(floor).toBe(6);
|
|
2299
|
-
});
|
|
2300
|
-
|
|
2301
|
-
test('higher and lower of adjacent keys', () => {
|
|
2302
|
-
const higher = bst.higher(5);
|
|
2303
|
-
const lower = bst.lower(6);
|
|
2304
|
-
expect(higher).toBe(6);
|
|
2305
|
-
expect(lower).toBe(5);
|
|
2306
|
-
});
|
|
2307
|
-
});
|
|
2308
|
-
|
|
2309
|
-
describe('Predicate-based search', () => {
|
|
2310
|
-
test('ceiling with predicate function', () => {
|
|
2311
|
-
const result = bst.ceiling((node: BSTNode<number, string>) => node.key >= 10);
|
|
2312
|
-
expect(result).toBeDefined();
|
|
2313
|
-
expect(result).toBeGreaterThanOrEqual(10);
|
|
2314
|
-
});
|
|
2315
|
-
|
|
2316
|
-
test('floor with predicate function', () => {
|
|
2317
|
-
const result = bst.floor((node: BSTNode<number, string>) => node.key <= 15);
|
|
2318
|
-
expect(result).toBeDefined();
|
|
2319
|
-
expect(result).toBeLessThanOrEqual(15);
|
|
2320
|
-
});
|
|
2321
|
-
|
|
2322
|
-
test('higher with predicate function', () => {
|
|
2323
|
-
const result = bst.higher((node: BSTNode<number, string>) => node.key > 10);
|
|
2324
|
-
expect(result).toBeDefined();
|
|
2325
|
-
expect(result).toBeGreaterThan(10);
|
|
2326
|
-
});
|
|
2327
|
-
|
|
2328
|
-
test('lower with predicate function', () => {
|
|
2329
|
-
const result = bst.lower((node: BSTNode<number, string>) => node.key < 15);
|
|
2330
|
-
expect(result).toBeDefined();
|
|
2331
|
-
expect(result).toBeLessThan(15);
|
|
2332
|
-
});
|
|
2333
|
-
});
|
|
2334
|
-
|
|
2335
|
-
describe('Custom comparator', () => {
|
|
2336
|
-
test('should work with reverse order comparator', () => {
|
|
2337
|
-
const reverseBst = new BST([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 20], {
|
|
2338
|
-
comparator: (a: number, b: number) => b - a // reverse order
|
|
2339
|
-
});
|
|
2340
|
-
|
|
2341
|
-
// In reverse order tree: keys are stored in descending order
|
|
2342
|
-
// ceiling (>=) should still work correctly
|
|
2343
|
-
const ceiling = reverseBst.ceiling(10);
|
|
2344
|
-
expect(ceiling).toBeDefined();
|
|
2345
|
-
expect(ceiling).toBeLessThanOrEqual(10);
|
|
2346
|
-
});
|
|
2347
|
-
|
|
2348
|
-
test('should work with string comparator', () => {
|
|
2349
|
-
const stringBst = new BST(
|
|
2350
|
-
[
|
|
2351
|
-
{ name: 'Alice', id: 1 },
|
|
2352
|
-
{ name: 'Bob', id: 2 },
|
|
2353
|
-
{ name: 'Charlie', id: 3 },
|
|
2354
|
-
{ name: 'David', id: 4 },
|
|
2355
|
-
{ name: 'Eve', id: 5 }
|
|
2356
|
-
],
|
|
2357
|
-
{
|
|
2358
|
-
comparator: (a, b) => a.name.localeCompare(b.name)
|
|
2359
|
-
}
|
|
2360
|
-
);
|
|
2361
|
-
|
|
2362
|
-
const ceiling = stringBst.ceiling({ name: 'Bob', id: 0 });
|
|
2363
|
-
expect(ceiling).toBeDefined();
|
|
2364
|
-
expect(ceiling?.name).toBe('Bob');
|
|
2365
|
-
});
|
|
2366
|
-
});
|
|
2367
|
-
|
|
2368
|
-
describe('Performance and correctness validation', () => {
|
|
2369
|
-
test('all range methods return keys in order', () => {
|
|
2370
|
-
const ceiling = bst.ceiling(10);
|
|
2371
|
-
const higher = bst.higher(10);
|
|
2372
|
-
const floor = bst.floor(10);
|
|
2373
|
-
const lower = bst.lower(10);
|
|
2374
|
-
|
|
2375
|
-
expect(floor).toBeLessThanOrEqual(10);
|
|
2376
|
-
expect(ceiling).toBeGreaterThanOrEqual(10);
|
|
2377
|
-
expect(higher).toBeGreaterThan(10);
|
|
2378
|
-
expect(lower).toBeLessThan(10);
|
|
2379
|
-
});
|
|
2380
|
-
|
|
2381
|
-
test('range query iteration with ceiling/higher', () => {
|
|
2382
|
-
const results: number[] = [];
|
|
2383
|
-
let key = bst.ceiling(5);
|
|
2384
|
-
let count = 0;
|
|
2385
|
-
while (key && key <= 15 && count < 20) {
|
|
2386
|
-
results.push(key);
|
|
2387
|
-
key = bst.higher(key);
|
|
2388
|
-
count++;
|
|
2389
|
-
}
|
|
2390
|
-
|
|
2391
|
-
// Should iterate through keys 5, 6, 7, ..., 15
|
|
2392
|
-
expect(results.length).toBeGreaterThan(0);
|
|
2393
|
-
expect(results[0]).toBeGreaterThanOrEqual(5);
|
|
2394
|
-
expect(results[results.length - 1]).toBeLessThanOrEqual(15);
|
|
2395
|
-
// Verify ascending order
|
|
2396
|
-
for (let i = 1; i < results.length; i++) {
|
|
2397
|
-
expect(results[i]).toBeGreaterThan(results[i - 1]);
|
|
2398
|
-
}
|
|
2399
|
-
});
|
|
2400
|
-
|
|
2401
|
-
test('range query iteration with floor/lower', () => {
|
|
2402
|
-
const results: number[] = [];
|
|
2403
|
-
let key = bst.floor(15);
|
|
2404
|
-
let count = 0;
|
|
2405
|
-
while (key && key >= 5 && count < 20) {
|
|
2406
|
-
results.push(key);
|
|
2407
|
-
key = bst.lower(key);
|
|
2408
|
-
count++;
|
|
2409
|
-
}
|
|
2410
|
-
|
|
2411
|
-
// Should iterate through keys 15, 14, 13, ..., 5
|
|
2412
|
-
expect(results.length).toBeGreaterThan(0);
|
|
2413
|
-
expect(results[0]).toBeLessThanOrEqual(15);
|
|
2414
|
-
expect(results[results.length - 1]).toBeGreaterThanOrEqual(5);
|
|
2415
|
-
// Verify descending order
|
|
2416
|
-
for (let i = 1; i < results.length; i++) {
|
|
2417
|
-
expect(results[i]).toBeLessThan(results[i - 1]);
|
|
2418
|
-
}
|
|
2419
|
-
});
|
|
2420
|
-
});
|
|
2421
|
-
|
|
2422
|
-
describe('Boundary value testing', () => {
|
|
2423
|
-
test('boundary: ceiling at min value', () => {
|
|
2424
|
-
const result = bst.ceiling(1);
|
|
2425
|
-
expect(result).toBe(1);
|
|
2426
|
-
});
|
|
2427
|
-
|
|
2428
|
-
test('boundary: floor at max value', () => {
|
|
2429
|
-
const result = bst.floor(20);
|
|
2430
|
-
expect(result).toBe(20);
|
|
2431
|
-
});
|
|
2432
|
-
|
|
2433
|
-
test('boundary: higher at second-last value', () => {
|
|
2434
|
-
const result = bst.higher(19);
|
|
2435
|
-
expect(result).toBe(20);
|
|
2436
|
-
});
|
|
2437
|
-
|
|
2438
|
-
test('boundary: lower at second value', () => {
|
|
2439
|
-
const result = bst.lower(3);
|
|
2440
|
-
expect(result).toBe(1);
|
|
2441
|
-
});
|
|
2442
|
-
|
|
2443
|
-
test('boundary: ceiling slightly below min', () => {
|
|
2444
|
-
const result = bst.ceiling(0);
|
|
2445
|
-
expect(result).toBe(1);
|
|
2446
|
-
});
|
|
2447
|
-
|
|
2448
|
-
test('boundary: floor slightly above max', () => {
|
|
2449
|
-
const result = bst.floor(21);
|
|
2450
|
-
expect(result).toBe(20);
|
|
2451
|
-
});
|
|
2452
|
-
|
|
2453
|
-
test('boundary: higher at max (should be undefined)', () => {
|
|
2454
|
-
const result = bst.higher(20);
|
|
2455
|
-
expect(result).toBeUndefined();
|
|
2456
|
-
});
|
|
2457
|
-
|
|
2458
|
-
test('boundary: lower at min (should be undefined)', () => {
|
|
2459
|
-
const result = bst.lower(1);
|
|
2460
|
-
expect(result).toBeUndefined();
|
|
2461
|
-
});
|
|
2462
|
-
});
|
|
2463
|
-
});
|
|
2464
|
-
|
|
2465
|
-
describe('BST Comparator Tests', () => {
|
|
2466
|
-
describe('Default Comparator with Primitive Types', () => {
|
|
2467
|
-
let bst: BST<number>;
|
|
2468
|
-
|
|
2469
|
-
beforeEach(() => {
|
|
2470
|
-
bst = new BST<number>();
|
|
2471
|
-
});
|
|
2472
|
-
|
|
2473
|
-
it('should compare two numbers correctly - a > b', () => {
|
|
2474
|
-
const result = bst['_compare'](5, 3);
|
|
2475
|
-
expect(result).toBe(1);
|
|
2476
|
-
});
|
|
2477
|
-
|
|
2478
|
-
it('should compare two numbers correctly - a < b', () => {
|
|
2479
|
-
const result = bst['_compare'](2, 8);
|
|
2480
|
-
expect(result).toBe(-1);
|
|
2481
|
-
});
|
|
2482
|
-
|
|
2483
|
-
it('should compare two numbers correctly - a === b', () => {
|
|
2484
|
-
const result = bst['_compare'](5, 5);
|
|
2485
|
-
expect(result).toBe(0);
|
|
2486
|
-
});
|
|
2487
|
-
|
|
2488
|
-
it('should compare negative numbers correctly', () => {
|
|
2489
|
-
const result1 = bst['_compare'](-5, -3);
|
|
2490
|
-
const result2 = bst['_compare'](-10, 0);
|
|
2491
|
-
expect(result1).toBe(-1);
|
|
2492
|
-
expect(result2).toBe(-1);
|
|
2493
|
-
});
|
|
2494
|
-
|
|
2495
|
-
it('should compare zero correctly', () => {
|
|
2496
|
-
const result1 = bst['_compare'](0, 5);
|
|
2497
|
-
const result2 = bst['_compare'](0, 0);
|
|
2498
|
-
const result3 = bst['_compare'](-5, 0);
|
|
2499
|
-
expect(result1).toBe(-1);
|
|
2500
|
-
expect(result2).toBe(0);
|
|
2501
|
-
expect(result3).toBe(-1);
|
|
2502
|
-
});
|
|
2503
|
-
|
|
2504
|
-
it('should compare decimal numbers correctly', () => {
|
|
2505
|
-
const result1 = bst['_compare'](3.14, 3.15);
|
|
2506
|
-
const result2 = bst['_compare'](2.5, 2.5);
|
|
2507
|
-
expect(result1).toBe(-1);
|
|
2508
|
-
expect(result2).toBe(0);
|
|
2509
|
-
});
|
|
2510
|
-
|
|
2511
|
-
it('should compare very large numbers correctly', () => {
|
|
2512
|
-
const result1 = bst['_compare'](Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER - 1);
|
|
2513
|
-
const result2 = bst['_compare'](1e10, 1e9);
|
|
2514
|
-
expect(result1).toBe(1);
|
|
2515
|
-
expect(result2).toBe(1);
|
|
2516
|
-
});
|
|
2517
|
-
});
|
|
2518
|
-
|
|
2519
|
-
describe('Default Comparator with String Types', () => {
|
|
2520
|
-
let bst: BST<string>;
|
|
2521
|
-
|
|
2522
|
-
beforeEach(() => {
|
|
2523
|
-
bst = new BST<string>();
|
|
2524
|
-
});
|
|
2525
|
-
|
|
2526
|
-
it('should compare strings alphabetically - a > b', () => {
|
|
2527
|
-
const result = bst['_compare']('zebra', 'apple');
|
|
2528
|
-
expect(result).toBe(1);
|
|
2529
|
-
});
|
|
2530
|
-
|
|
2531
|
-
it('should compare strings alphabetically - a < b', () => {
|
|
2532
|
-
const result = bst['_compare']('apple', 'zebra');
|
|
2533
|
-
expect(result).toBe(-1);
|
|
2534
|
-
});
|
|
2535
|
-
|
|
2536
|
-
it('should compare identical strings', () => {
|
|
2537
|
-
const result = bst['_compare']('hello', 'hello');
|
|
2538
|
-
expect(result).toBe(0);
|
|
2539
|
-
});
|
|
2540
|
-
|
|
2541
|
-
it('should compare string prefixes correctly', () => {
|
|
2542
|
-
const result1 = bst['_compare']('app', 'apple');
|
|
2543
|
-
const result2 = bst['_compare']('apple', 'app');
|
|
2544
|
-
expect(result1).toBe(-1);
|
|
2545
|
-
expect(result2).toBe(1);
|
|
2546
|
-
});
|
|
2547
|
-
|
|
2548
|
-
it('should compare case-sensitive strings', () => {
|
|
2549
|
-
const result1 = bst['_compare']('Apple', 'apple');
|
|
2550
|
-
const result2 = bst['_compare']('apple', 'Apple');
|
|
2551
|
-
expect(result1).toBe(-1);
|
|
2552
|
-
expect(result2).toBe(1);
|
|
2553
|
-
});
|
|
2554
|
-
|
|
2555
|
-
it('should compare empty strings', () => {
|
|
2556
|
-
const result1 = bst['_compare']('', '');
|
|
2557
|
-
const result2 = bst['_compare']('', 'a');
|
|
2558
|
-
const result3 = bst['_compare']('a', '');
|
|
2559
|
-
expect(result1).toBe(0);
|
|
2560
|
-
expect(result2).toBe(-1);
|
|
2561
|
-
expect(result3).toBe(1);
|
|
2562
|
-
});
|
|
2563
|
-
|
|
2564
|
-
it('should compare special characters in strings', () => {
|
|
2565
|
-
const result1 = bst['_compare']('!', 'a');
|
|
2566
|
-
const result2 = bst['_compare']('a', '1');
|
|
2567
|
-
expect(result1).toBe(-1);
|
|
2568
|
-
expect(result2).toBe(1);
|
|
2569
|
-
});
|
|
2570
|
-
});
|
|
2571
|
-
|
|
2572
|
-
describe('Custom Comparator', () => {
|
|
2573
|
-
interface Person {
|
|
2574
|
-
name: string;
|
|
2575
|
-
age: number;
|
|
2576
|
-
}
|
|
2577
|
-
|
|
2578
|
-
it('should accept custom comparator for objects - compare by age', () => {
|
|
2579
|
-
const comparator = (a: Person, b: Person): number => {
|
|
2580
|
-
if (a.age > b.age) return 1;
|
|
2581
|
-
if (a.age < b.age) return -1;
|
|
2582
|
-
return 0;
|
|
2583
|
-
};
|
|
2584
|
-
|
|
2585
|
-
const bst = new BST<Person>([], { comparator });
|
|
2586
|
-
|
|
2587
|
-
const p1 = { name: 'Alice', age: 30 };
|
|
2588
|
-
const p2 = { name: 'Bob', age: 25 };
|
|
2589
|
-
const p3 = { name: 'Charlie', age: 30 };
|
|
2590
|
-
|
|
2591
|
-
expect(bst['_compare'](p1, p2)).toBe(1);
|
|
2592
|
-
expect(bst['_compare'](p2, p1)).toBe(-1);
|
|
2593
|
-
expect(bst['_compare'](p1, p3)).toBe(0);
|
|
2594
|
-
});
|
|
2595
|
-
|
|
2596
|
-
it('should accept custom comparator - compare by name length', () => {
|
|
2597
|
-
const comparator = (a: string, b: string): number => {
|
|
2598
|
-
if (a.length > b.length) return 1;
|
|
2599
|
-
if (a.length < b.length) return -1;
|
|
2600
|
-
return 0;
|
|
2601
|
-
};
|
|
2602
|
-
|
|
2603
|
-
const bst = new BST<string>([], { comparator });
|
|
2604
|
-
|
|
2605
|
-
expect(bst['_compare']('hello', 'hi')).toBe(1);
|
|
2606
|
-
expect(bst['_compare']('hi', 'hello')).toBe(-1);
|
|
2607
|
-
expect(bst['_compare']('abc', 'def')).toBe(0);
|
|
2608
|
-
});
|
|
2609
|
-
|
|
2610
|
-
it('should accept custom comparator - reverse order', () => {
|
|
2611
|
-
const comparator = (a: number, b: number): number => {
|
|
2612
|
-
if (a < b) return 1;
|
|
2613
|
-
if (a > b) return -1;
|
|
2614
|
-
return 0;
|
|
2615
|
-
};
|
|
2616
|
-
|
|
2617
|
-
const bst = new BST<number>([], { comparator });
|
|
2618
|
-
|
|
2619
|
-
expect(bst['_compare'](5, 3)).toBe(-1);
|
|
2620
|
-
expect(bst['_compare'](2, 8)).toBe(1);
|
|
2621
|
-
expect(bst['_compare'](5, 5)).toBe(0);
|
|
2622
|
-
});
|
|
2623
|
-
|
|
2624
|
-
it('should throw error when comparing object types without custom comparator', () => {
|
|
2625
|
-
const bst = new BST<{ value: number }>();
|
|
2626
|
-
|
|
2627
|
-
expect(() => {
|
|
2628
|
-
bst['_compare']({ value: 1 }, { value: 2 });
|
|
2629
|
-
}).toThrow("When comparing object type keys, a custom comparator must be provided in the constructor's options!");
|
|
2630
|
-
});
|
|
2631
|
-
});
|
|
2632
|
-
|
|
2633
|
-
describe('Comparator Usage in BST Operations', () => {
|
|
2634
|
-
let bst: BST<number>;
|
|
2635
|
-
|
|
2636
|
-
beforeEach(() => {
|
|
2637
|
-
bst = new BST<number>();
|
|
2638
|
-
});
|
|
2639
|
-
|
|
2640
|
-
it('should correctly insert elements based on comparator', () => {
|
|
2641
|
-
bst.set(10);
|
|
2642
|
-
bst.set(5);
|
|
2643
|
-
bst.set(15);
|
|
2644
|
-
bst.set(3);
|
|
2645
|
-
bst.set(7);
|
|
2646
|
-
|
|
2647
|
-
expect(bst.has(10)).toBe(true);
|
|
2648
|
-
expect(bst.has(5)).toBe(true);
|
|
2649
|
-
expect(bst.has(15)).toBe(true);
|
|
2650
|
-
expect(bst.has(3)).toBe(true);
|
|
2651
|
-
expect(bst.has(7)).toBe(true);
|
|
2652
|
-
expect(bst.size).toBe(5);
|
|
2653
|
-
});
|
|
2654
|
-
|
|
2655
|
-
it('should maintain BST property with in-order traversal', () => {
|
|
2656
|
-
bst.setMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
2657
|
-
|
|
2658
|
-
const inOrder: (number | undefined)[] = bst.dfs(node => node?.key);
|
|
2659
|
-
|
|
2660
|
-
for (let i = 1; i < inOrder.length; i++) {
|
|
2661
|
-
expect(inOrder[i]! >= inOrder[i - 1]!).toBe(true);
|
|
2662
|
-
}
|
|
2663
|
-
});
|
|
2664
|
-
|
|
2665
|
-
it('should correctly find min and max using comparator', () => {
|
|
2666
|
-
bst.setMany([15, 10, 20, 8, 12, 18, 25]);
|
|
2667
|
-
|
|
2668
|
-
const arr = Array.from(bst.keys());
|
|
2669
|
-
const min = Math.min(...arr);
|
|
2670
|
-
const max = Math.max(...arr);
|
|
2671
|
-
|
|
2672
|
-
expect(bst.has(min)).toBe(true);
|
|
2673
|
-
expect(bst.has(max)).toBe(true);
|
|
2674
|
-
});
|
|
2675
|
-
|
|
2676
|
-
it('should correctly delete elements maintaining BST property', () => {
|
|
2677
|
-
bst.setMany([10, 5, 15, 3, 7, 12, 18]);
|
|
2678
|
-
bst.delete(10);
|
|
2679
|
-
|
|
2680
|
-
expect(bst.has(10)).toBe(false);
|
|
2681
|
-
const inOrder: (number | undefined)[] = bst.dfs(node => node?.key);
|
|
2682
|
-
|
|
2683
|
-
for (let i = 1; i < inOrder.length; i++) {
|
|
2684
|
-
expect(inOrder[i]! >= inOrder[i - 1]!).toBe(true);
|
|
2685
|
-
}
|
|
2686
|
-
});
|
|
2687
|
-
|
|
2688
|
-
it('should correctly search using comparator', () => {
|
|
2689
|
-
bst.setMany([20, 10, 30, 5, 15, 25, 35]);
|
|
2690
|
-
|
|
2691
|
-
const result = bst.search(15);
|
|
2692
|
-
expect(result.length).toBeGreaterThan(0);
|
|
2693
|
-
expect(result[0]).toBe(15);
|
|
2694
|
-
});
|
|
2695
|
-
});
|
|
2696
|
-
|
|
2697
|
-
describe('Comparator Edge Cases', () => {
|
|
2698
|
-
describe('with numbers', () => {
|
|
2699
|
-
let bst: BST<number>;
|
|
2700
|
-
|
|
2701
|
-
beforeEach(() => {
|
|
2702
|
-
bst = new BST<number>();
|
|
2703
|
-
});
|
|
2704
|
-
|
|
2705
|
-
it('should handle Infinity correctly', () => {
|
|
2706
|
-
const result1 = bst['_compare'](Infinity, 100);
|
|
2707
|
-
const result2 = bst['_compare'](100, Infinity);
|
|
2708
|
-
expect(result1).toBe(1);
|
|
2709
|
-
expect(result2).toBe(-1);
|
|
2710
|
-
});
|
|
2711
|
-
|
|
2712
|
-
it('should handle -Infinity correctly', () => {
|
|
2713
|
-
const result1 = bst['_compare'](-Infinity, -100);
|
|
2714
|
-
const result2 = bst['_compare'](-100, -Infinity);
|
|
2715
|
-
expect(result1).toBe(-1);
|
|
2716
|
-
expect(result2).toBe(1);
|
|
2717
|
-
});
|
|
2718
|
-
|
|
2719
|
-
it('should handle NaN correctly', () => {
|
|
2720
|
-
const result1 = bst['_compare'](NaN, 5);
|
|
2721
|
-
const result2 = bst['_compare'](5, NaN);
|
|
2722
|
-
expect(result1).toBe(0);
|
|
2723
|
-
expect(result2).toBe(0);
|
|
2724
|
-
});
|
|
2725
|
-
});
|
|
2726
|
-
|
|
2727
|
-
describe('with strings', () => {
|
|
2728
|
-
let bst: BST<string>;
|
|
2729
|
-
|
|
2730
|
-
beforeEach(() => {
|
|
2731
|
-
bst = new BST<string>();
|
|
2732
|
-
});
|
|
2733
|
-
|
|
2734
|
-
it('should handle Unicode characters', () => {
|
|
2735
|
-
const result1 = bst['_compare']('😀', 'abc');
|
|
2736
|
-
const result2 = bst['_compare']('中文', 'English');
|
|
2737
|
-
expect(typeof result1).toBe('number');
|
|
2738
|
-
expect(typeof result2).toBe('number');
|
|
2739
|
-
});
|
|
2740
|
-
|
|
2741
|
-
it('should handle whitespace strings', () => {
|
|
2742
|
-
const result1 = bst['_compare'](' ', ' ');
|
|
2743
|
-
const result2 = bst['_compare']('\n', '\t');
|
|
2744
|
-
expect(typeof result1).toBe('number');
|
|
2745
|
-
expect(typeof result2).toBe('number');
|
|
2746
|
-
});
|
|
2747
|
-
|
|
2748
|
-
it('should maintain consistency with multiple comparisons', () => {
|
|
2749
|
-
const a = 'apple';
|
|
2750
|
-
const b = 'banana';
|
|
2751
|
-
const c = 'cherry';
|
|
2752
|
-
|
|
2753
|
-
const ab = bst['_compare'](a, b);
|
|
2754
|
-
const bc = bst['_compare'](b, c);
|
|
2755
|
-
const ac = bst['_compare'](a, c);
|
|
2756
|
-
|
|
2757
|
-
if (ab < 0 && bc < 0) {
|
|
2758
|
-
expect(ac).toBeLessThan(0);
|
|
2759
|
-
}
|
|
2760
|
-
});
|
|
2761
|
-
});
|
|
2762
|
-
});
|
|
2763
|
-
|
|
2764
|
-
describe('Comparator with BST Bound Operations', () => {
|
|
2765
|
-
let bst: BST<number>;
|
|
2766
|
-
|
|
2767
|
-
beforeEach(() => {
|
|
2768
|
-
bst = new BST<number>();
|
|
2769
|
-
bst.setMany([10, 5, 15, 3, 7, 12, 18, 1, 4, 6, 8, 11, 13, 16, 20]);
|
|
2770
|
-
});
|
|
2771
|
-
|
|
2772
|
-
it('should find ceiling using comparator', () => {
|
|
2773
|
-
const result = bst.ceiling(7);
|
|
2774
|
-
expect(result).toBe(7);
|
|
2775
|
-
|
|
2776
|
-
const result2 = bst.ceiling(7.5);
|
|
2777
|
-
expect(result2).toBeGreaterThanOrEqual(7.5);
|
|
2778
|
-
});
|
|
2779
|
-
|
|
2780
|
-
it('should find floor using comparator', () => {
|
|
2781
|
-
const result = bst.floor(7);
|
|
2782
|
-
expect(result).toBe(7);
|
|
2783
|
-
|
|
2784
|
-
const result2 = bst.floor(7.5);
|
|
2785
|
-
expect(result2!).toBeLessThanOrEqual(7.5);
|
|
2786
|
-
});
|
|
2787
|
-
|
|
2788
|
-
it('should find higher using comparator', () => {
|
|
2789
|
-
const result = bst.higher(7);
|
|
2790
|
-
expect(result).toBeGreaterThan(7);
|
|
2791
|
-
});
|
|
2792
|
-
|
|
2793
|
-
it('should find lower using comparator', () => {
|
|
2794
|
-
const result = bst.lower(7);
|
|
2795
|
-
expect(result).toBeLessThan(7);
|
|
2796
|
-
});
|
|
2797
|
-
});
|
|
2798
|
-
|
|
2799
|
-
describe('Comparator Consistency', () => {
|
|
2800
|
-
let bst: BST<number>;
|
|
2801
|
-
|
|
2802
|
-
beforeEach(() => {
|
|
2803
|
-
bst = new BST<number>();
|
|
2804
|
-
});
|
|
2805
|
-
|
|
2806
|
-
it('should satisfy reflexivity: a compared with itself returns 0', () => {
|
|
2807
|
-
const values = [0, 1, -1, 100, -100, 0.5];
|
|
2808
|
-
|
|
2809
|
-
for (const val of values) {
|
|
2810
|
-
expect(bst['_compare'](val, val)).toBe(0);
|
|
2811
|
-
}
|
|
2812
|
-
});
|
|
2813
|
-
|
|
2814
|
-
it('should satisfy antisymmetry: if a > b then b < a', () => {
|
|
2815
|
-
const pairs = [
|
|
2816
|
-
[5, 3],
|
|
2817
|
-
[10, 2],
|
|
2818
|
-
[100, 1]
|
|
2819
|
-
];
|
|
2820
|
-
|
|
2821
|
-
for (const [a, b] of pairs) {
|
|
2822
|
-
const cmp1 = bst['_compare'](a, b);
|
|
2823
|
-
const cmp2 = bst['_compare'](b, a);
|
|
2824
|
-
expect(Math.sign(cmp1)).toBe(-Math.sign(cmp2));
|
|
2825
|
-
}
|
|
2826
|
-
});
|
|
2827
|
-
|
|
2828
|
-
it('should satisfy transitivity: if a < b and b < c then a < c', () => {
|
|
2829
|
-
const a = 1;
|
|
2830
|
-
const b = 5;
|
|
2831
|
-
const c = 10;
|
|
2832
|
-
|
|
2833
|
-
const cmpAB = bst['_compare'](a, b);
|
|
2834
|
-
const cmpBC = bst['_compare'](b, c);
|
|
2835
|
-
const cmpAC = bst['_compare'](a, c);
|
|
2836
|
-
|
|
2837
|
-
if (cmpAB < 0 && cmpBC < 0) {
|
|
2838
|
-
expect(cmpAC).toBeLessThan(0);
|
|
2839
|
-
}
|
|
2840
|
-
});
|
|
2841
|
-
});
|
|
2842
|
-
|
|
2843
|
-
describe('Custom Comparator with BST Operations', () => {
|
|
2844
|
-
interface Student {
|
|
2845
|
-
name: string;
|
|
2846
|
-
grade: number;
|
|
2847
|
-
}
|
|
2848
|
-
|
|
2849
|
-
it('should work with custom comparator for complex operations', () => {
|
|
2850
|
-
const comparator = (a: Student, b: Student): number => {
|
|
2851
|
-
if (a.grade !== b.grade) {
|
|
2852
|
-
return a.grade > b.grade ? 1 : -1;
|
|
2853
|
-
}
|
|
2854
|
-
return a.name.localeCompare(b.name);
|
|
2855
|
-
};
|
|
2856
|
-
|
|
2857
|
-
const bst = new BST<Student>([], { comparator });
|
|
2858
|
-
|
|
2859
|
-
const students: Student[] = [
|
|
2860
|
-
{ name: 'Alice', grade: 85 },
|
|
2861
|
-
{ name: 'Bob', grade: 90 },
|
|
2862
|
-
{ name: 'Charlie', grade: 85 },
|
|
2863
|
-
{ name: 'David', grade: 95 }
|
|
2864
|
-
];
|
|
2865
|
-
|
|
2866
|
-
students.forEach(s => bst.set(s));
|
|
2867
|
-
|
|
2868
|
-
expect(bst.size).toBe(4);
|
|
2869
|
-
expect(bst.has(students[0])).toBe(true);
|
|
2870
|
-
});
|
|
2871
|
-
|
|
2872
|
-
it('should handle custom comparator with add and search', () => {
|
|
2873
|
-
const comparator = (a: number[], b: number[]): number => {
|
|
2874
|
-
const sumA = a.reduce((acc, val) => acc + val, 0);
|
|
2875
|
-
const sumB = b.reduce((acc, val) => acc + val, 0);
|
|
2876
|
-
if (sumA > sumB) return 1;
|
|
2877
|
-
if (sumA < sumB) return -1;
|
|
2878
|
-
return 0;
|
|
2879
|
-
};
|
|
2880
|
-
|
|
2881
|
-
const bst = new BST<number[]>([], { comparator });
|
|
2882
|
-
|
|
2883
|
-
bst.set([1, 2, 3]);
|
|
2884
|
-
bst.set([2, 2, 2]);
|
|
2885
|
-
bst.set([1, 1, 1]);
|
|
2886
|
-
|
|
2887
|
-
expect(bst.size).toBeGreaterThan(0);
|
|
2888
|
-
});
|
|
2889
|
-
});
|
|
2890
|
-
|
|
2891
|
-
describe('Composite Key Comparator Tests', () => {
|
|
2892
|
-
interface CompositeKey {
|
|
2893
|
-
departmentId: number;
|
|
2894
|
-
employeeId: number;
|
|
2895
|
-
}
|
|
2896
|
-
|
|
2897
|
-
it('should compare composite keys by multiple fields - primary then secondary', () => {
|
|
2898
|
-
const comparator = (a: CompositeKey, b: CompositeKey): number => {
|
|
2899
|
-
if (a.departmentId !== b.departmentId) {
|
|
2900
|
-
return a.departmentId > b.departmentId ? 1 : -1;
|
|
2901
|
-
}
|
|
2902
|
-
if (a.employeeId !== b.employeeId) {
|
|
2903
|
-
return a.employeeId > b.employeeId ? 1 : -1;
|
|
2904
|
-
}
|
|
2905
|
-
return 0;
|
|
2906
|
-
};
|
|
2907
|
-
|
|
2908
|
-
const bst = new BST<CompositeKey>([], { comparator });
|
|
2909
|
-
|
|
2910
|
-
const key1 = { departmentId: 1, employeeId: 101 };
|
|
2911
|
-
const key2 = { departmentId: 1, employeeId: 102 };
|
|
2912
|
-
const key3 = { departmentId: 2, employeeId: 101 };
|
|
2913
|
-
|
|
2914
|
-
expect(bst['_compare'](key1, key2)).toBe(-1);
|
|
2915
|
-
expect(bst['_compare'](key2, key3)).toBe(-1);
|
|
2916
|
-
expect(bst['_compare'](key1, key1)).toBe(0);
|
|
2917
|
-
});
|
|
2918
|
-
|
|
2919
|
-
it('should maintain BST property with composite keys', () => {
|
|
2920
|
-
const comparator = (a: CompositeKey, b: CompositeKey): number => {
|
|
2921
|
-
if (a.departmentId !== b.departmentId) {
|
|
2922
|
-
return a.departmentId > b.departmentId ? 1 : -1;
|
|
2923
|
-
}
|
|
2924
|
-
return a.employeeId > b.employeeId ? 1 : a.employeeId < b.employeeId ? -1 : 0;
|
|
2925
|
-
};
|
|
2926
|
-
|
|
2927
|
-
const bst = new BST<CompositeKey>([], { comparator });
|
|
2928
|
-
|
|
2929
|
-
const keys = [
|
|
2930
|
-
{ departmentId: 2, employeeId: 105 },
|
|
2931
|
-
{ departmentId: 1, employeeId: 101 },
|
|
2932
|
-
{ departmentId: 1, employeeId: 103 },
|
|
2933
|
-
{ departmentId: 2, employeeId: 102 },
|
|
2934
|
-
{ departmentId: 1, employeeId: 102 }
|
|
2935
|
-
];
|
|
2936
|
-
|
|
2937
|
-
keys.forEach(k => bst.set(k));
|
|
2938
|
-
|
|
2939
|
-
expect(bst.size).toBe(5);
|
|
2940
|
-
const inOrder = bst.dfs(node => node?.key);
|
|
2941
|
-
|
|
2942
|
-
for (let i = 1; i < inOrder.length; i++) {
|
|
2943
|
-
const cmp = comparator(inOrder[i - 1]!, inOrder[i]!);
|
|
2944
|
-
expect(cmp).toBeLessThanOrEqual(0);
|
|
2945
|
-
}
|
|
2946
|
-
});
|
|
2947
|
-
|
|
2948
|
-
it('should search with composite keys', () => {
|
|
2949
|
-
const comparator = (a: CompositeKey, b: CompositeKey): number => {
|
|
2950
|
-
if (a.departmentId !== b.departmentId) {
|
|
2951
|
-
return a.departmentId > b.departmentId ? 1 : -1;
|
|
2952
|
-
}
|
|
2953
|
-
return a.employeeId > b.employeeId ? 1 : a.employeeId < b.employeeId ? -1 : 0;
|
|
2954
|
-
};
|
|
2955
|
-
|
|
2956
|
-
const bst = new BST<CompositeKey>([], { comparator });
|
|
2957
|
-
|
|
2958
|
-
const keys = [
|
|
2959
|
-
{ departmentId: 1, employeeId: 101 },
|
|
2960
|
-
{ departmentId: 1, employeeId: 103 },
|
|
2961
|
-
{ departmentId: 2, employeeId: 102 }
|
|
2962
|
-
];
|
|
2963
|
-
|
|
2964
|
-
keys.forEach(k => bst.set(k));
|
|
2965
|
-
|
|
2966
|
-
const searchKey = keys[1];
|
|
2967
|
-
const result = bst.search(searchKey);
|
|
2968
|
-
|
|
2969
|
-
expect(result.length).toBeGreaterThan(0);
|
|
2970
|
-
expect(result[0]).toEqual(searchKey);
|
|
2971
|
-
});
|
|
2972
|
-
|
|
2973
|
-
it('should handle deletion with composite keys', () => {
|
|
2974
|
-
const comparator = (a: CompositeKey, b: CompositeKey): number => {
|
|
2975
|
-
if (a.departmentId !== b.departmentId) {
|
|
2976
|
-
return a.departmentId > b.departmentId ? 1 : -1;
|
|
2977
|
-
}
|
|
2978
|
-
return a.employeeId > b.employeeId ? 1 : a.employeeId < b.employeeId ? -1 : 0;
|
|
2979
|
-
};
|
|
2980
|
-
|
|
2981
|
-
const bst = new BST<CompositeKey>([], { comparator });
|
|
2982
|
-
|
|
2983
|
-
const keys = [
|
|
2984
|
-
{ departmentId: 1, employeeId: 101 },
|
|
2985
|
-
{ departmentId: 1, employeeId: 103 },
|
|
2986
|
-
{ departmentId: 2, employeeId: 102 }
|
|
2987
|
-
];
|
|
2988
|
-
|
|
2989
|
-
keys.forEach(k => bst.set(k));
|
|
2990
|
-
|
|
2991
|
-
const keyToDelete = keys[1];
|
|
2992
|
-
const deleted = bst.delete(keyToDelete);
|
|
2993
|
-
|
|
2994
|
-
expect(deleted.length).toBeGreaterThan(0);
|
|
2995
|
-
expect(bst.has(keyToDelete)).toBe(false);
|
|
2996
|
-
expect(bst.size).toBe(2);
|
|
2997
|
-
});
|
|
2998
|
-
|
|
2999
|
-
it('should find ceiling/floor with composite keys', () => {
|
|
3000
|
-
const comparator = (a: CompositeKey, b: CompositeKey): number => {
|
|
3001
|
-
if (a.departmentId !== b.departmentId) {
|
|
3002
|
-
return a.departmentId > b.departmentId ? 1 : -1;
|
|
3003
|
-
}
|
|
3004
|
-
return a.employeeId > b.employeeId ? 1 : a.employeeId < b.employeeId ? -1 : 0;
|
|
3005
|
-
};
|
|
3006
|
-
|
|
3007
|
-
const bst = new BST<CompositeKey>([], { comparator });
|
|
3008
|
-
|
|
3009
|
-
const keys = [
|
|
3010
|
-
{ departmentId: 1, employeeId: 101 },
|
|
3011
|
-
{ departmentId: 1, employeeId: 105 },
|
|
3012
|
-
{ departmentId: 2, employeeId: 102 }
|
|
3013
|
-
];
|
|
3014
|
-
|
|
3015
|
-
keys.forEach(k => bst.set(k));
|
|
3016
|
-
|
|
3017
|
-
const searchKey = { departmentId: 1, employeeId: 103 };
|
|
3018
|
-
const ceiling = bst.ceiling(searchKey);
|
|
3019
|
-
const floor = bst.floor(searchKey);
|
|
3020
|
-
|
|
3021
|
-
expect(ceiling).toBeDefined();
|
|
3022
|
-
expect(floor).toBeDefined();
|
|
3023
|
-
});
|
|
3024
|
-
});
|
|
3025
|
-
|
|
3026
|
-
describe('Key-Value Storage with Comparator', () => {
|
|
3027
|
-
interface PersonKey {
|
|
3028
|
-
id: number;
|
|
3029
|
-
country: string;
|
|
3030
|
-
}
|
|
3031
|
-
|
|
3032
|
-
interface PersonValue {
|
|
3033
|
-
name: string;
|
|
3034
|
-
email: string;
|
|
3035
|
-
age: number;
|
|
3036
|
-
}
|
|
3037
|
-
|
|
3038
|
-
it('should store and retrieve key-value pairs with composite keys', () => {
|
|
3039
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3040
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3041
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3042
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3043
|
-
};
|
|
3044
|
-
|
|
3045
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3046
|
-
|
|
3047
|
-
const key1: PersonKey = { id: 1, country: 'USA' };
|
|
3048
|
-
const value1: PersonValue = { name: 'Alice', email: 'alice@example.com', age: 30 };
|
|
3049
|
-
|
|
3050
|
-
const key2: PersonKey = { id: 2, country: 'Canada' };
|
|
3051
|
-
const value2: PersonValue = { name: 'Bob', email: 'bob@example.com', age: 25 };
|
|
3052
|
-
|
|
3053
|
-
bst.set([key1, value1]);
|
|
3054
|
-
bst.set([key2, value2]);
|
|
3055
|
-
|
|
3056
|
-
expect(bst.size).toBe(2);
|
|
3057
|
-
expect(bst.get(key1)).toEqual(value1);
|
|
3058
|
-
expect(bst.get(key2)).toEqual(value2);
|
|
3059
|
-
});
|
|
3060
|
-
|
|
3061
|
-
it('should update values when adding duplicate keys', () => {
|
|
3062
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3063
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3064
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3065
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3066
|
-
};
|
|
3067
|
-
|
|
3068
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3069
|
-
|
|
3070
|
-
const key: PersonKey = { id: 1, country: 'USA' };
|
|
3071
|
-
const value1: PersonValue = { name: 'Alice', email: 'alice@example.com', age: 30 };
|
|
3072
|
-
const value2: PersonValue = { name: 'Alice Updated', email: 'alice.new@example.com', age: 31 };
|
|
3073
|
-
|
|
3074
|
-
bst.set([key, value1]);
|
|
3075
|
-
expect(bst.get(key)).toEqual(value1);
|
|
3076
|
-
|
|
3077
|
-
bst.set([key, value2]);
|
|
3078
|
-
expect(bst.size).toBe(1);
|
|
3079
|
-
expect(bst.get(key)).toEqual(value2);
|
|
3080
|
-
});
|
|
3081
|
-
|
|
3082
|
-
it('should retrieve all entries in sorted order by key', () => {
|
|
3083
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3084
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3085
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3086
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3087
|
-
};
|
|
3088
|
-
|
|
3089
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3090
|
-
|
|
3091
|
-
const entries: Array<[PersonKey, PersonValue]> = [
|
|
3092
|
-
[
|
|
3093
|
-
{ id: 1, country: 'USA' },
|
|
3094
|
-
{ name: 'Alice', email: 'alice@usa.com', age: 30 }
|
|
3095
|
-
],
|
|
3096
|
-
[
|
|
3097
|
-
{ id: 2, country: 'Canada' },
|
|
3098
|
-
{ name: 'Bob', email: 'bob@ca.com', age: 25 }
|
|
3099
|
-
],
|
|
3100
|
-
[
|
|
3101
|
-
{ id: 1, country: 'Canada' },
|
|
3102
|
-
{ name: 'Charlie', email: 'charlie@ca.com', age: 28 }
|
|
3103
|
-
]
|
|
3104
|
-
];
|
|
3105
|
-
|
|
3106
|
-
entries.forEach(([key, value]) => bst.set([key, value]));
|
|
3107
|
-
|
|
3108
|
-
expect(bst.size).toBe(3);
|
|
3109
|
-
|
|
3110
|
-
const values: (PersonValue | undefined)[] = [];
|
|
3111
|
-
for (const [key] of entries) {
|
|
3112
|
-
const value = bst.get(key);
|
|
3113
|
-
if (value) values.push(value);
|
|
3114
|
-
}
|
|
3115
|
-
|
|
3116
|
-
expect(values.length).toBeGreaterThan(0);
|
|
3117
|
-
});
|
|
3118
|
-
|
|
3119
|
-
it('should delete key-value pairs correctly', () => {
|
|
3120
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3121
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3122
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3123
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3124
|
-
};
|
|
3125
|
-
|
|
3126
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3127
|
-
|
|
3128
|
-
const key1: PersonKey = { id: 1, country: 'USA' };
|
|
3129
|
-
const value1: PersonValue = { name: 'Alice', email: 'alice@example.com', age: 30 };
|
|
3130
|
-
|
|
3131
|
-
const key2: PersonKey = { id: 2, country: 'Canada' };
|
|
3132
|
-
const value2: PersonValue = { name: 'Bob', email: 'bob@example.com', age: 25 };
|
|
3133
|
-
|
|
3134
|
-
bst.set([key1, value1]);
|
|
3135
|
-
bst.set([key2, value2]);
|
|
3136
|
-
|
|
3137
|
-
bst.delete(key1);
|
|
3138
|
-
|
|
3139
|
-
expect(bst.has(key1)).toBe(false);
|
|
3140
|
-
expect(bst.has(key2)).toBe(true);
|
|
3141
|
-
expect(bst.size).toBe(1);
|
|
3142
|
-
expect(bst.get(key1)).toBeUndefined();
|
|
3143
|
-
});
|
|
3144
|
-
|
|
3145
|
-
it('should search and retrieve values with composite keys', () => {
|
|
3146
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3147
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3148
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3149
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3150
|
-
};
|
|
3151
|
-
|
|
3152
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3153
|
-
|
|
3154
|
-
const entries: Array<[PersonKey, PersonValue]> = [
|
|
3155
|
-
[
|
|
3156
|
-
{ id: 1, country: 'USA' },
|
|
3157
|
-
{ name: 'Alice', email: 'alice@usa.com', age: 30 }
|
|
3158
|
-
],
|
|
3159
|
-
[
|
|
3160
|
-
{ id: 2, country: 'USA' },
|
|
3161
|
-
{ name: 'Amy', email: 'amy@usa.com', age: 28 }
|
|
3162
|
-
],
|
|
3163
|
-
[
|
|
3164
|
-
{ id: 1, country: 'Canada' },
|
|
3165
|
-
{ name: 'Bob', email: 'bob@ca.com', age: 25 }
|
|
3166
|
-
]
|
|
3167
|
-
];
|
|
3168
|
-
|
|
3169
|
-
entries.forEach(([key, value]) => bst.set([key, value]));
|
|
3170
|
-
|
|
3171
|
-
const searchKey: PersonKey = entries[1][0];
|
|
3172
|
-
const value = bst.get(searchKey);
|
|
3173
|
-
|
|
3174
|
-
expect(value).toEqual({ name: 'Amy', email: 'amy@usa.com', age: 28 });
|
|
3175
|
-
});
|
|
3176
|
-
|
|
3177
|
-
it('should map over key-value pairs maintaining comparator order', () => {
|
|
3178
|
-
const comparator = (a: PersonKey, b: PersonKey): number => {
|
|
3179
|
-
const countryCompare = a.country.localeCompare(b.country);
|
|
3180
|
-
if (countryCompare !== 0) return countryCompare;
|
|
3181
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3182
|
-
};
|
|
3183
|
-
|
|
3184
|
-
const bst = new BST<PersonKey, PersonValue>([], { comparator, isMapMode: true });
|
|
3185
|
-
|
|
3186
|
-
const entries: Array<[PersonKey, PersonValue]> = [
|
|
3187
|
-
[
|
|
3188
|
-
{ id: 1, country: 'USA' },
|
|
3189
|
-
{ name: 'Alice', email: 'alice@usa.com', age: 30 }
|
|
3190
|
-
],
|
|
3191
|
-
[
|
|
3192
|
-
{ id: 2, country: 'Canada' },
|
|
3193
|
-
{ name: 'Bob', email: 'bob@ca.com', age: 25 }
|
|
3194
|
-
]
|
|
3195
|
-
];
|
|
3196
|
-
|
|
3197
|
-
entries.forEach(([key, value]) => bst.set([key, value]));
|
|
3198
|
-
|
|
3199
|
-
const mapped = bst.map((value, key) => [key, value?.name], { comparator, isMapMode: false });
|
|
3200
|
-
|
|
3201
|
-
expect(mapped.size).toBe(2);
|
|
3202
|
-
const names = Array.from(mapped.values());
|
|
3203
|
-
expect(names).toContain('Alice');
|
|
3204
|
-
expect(names).toContain('Bob');
|
|
3205
|
-
});
|
|
3206
|
-
|
|
3207
|
-
it('should perform range search on key-value pairs', () => {
|
|
3208
|
-
const comparator = (a: number, b: number): number => {
|
|
3209
|
-
return a > b ? 1 : a < b ? -1 : 0;
|
|
3210
|
-
};
|
|
3211
|
-
|
|
3212
|
-
const bst = new BST<number, string>([], { comparator, isMapMode: true });
|
|
3213
|
-
|
|
3214
|
-
for (let i = 1; i <= 10; i++) {
|
|
3215
|
-
bst.set([i, `value-${i}`]);
|
|
3216
|
-
}
|
|
3217
|
-
|
|
3218
|
-
const result = bst.rangeSearch([3, 7]);
|
|
3219
|
-
|
|
3220
|
-
expect(result.length).toBeGreaterThan(0);
|
|
3221
|
-
result.forEach(key => {
|
|
3222
|
-
expect(key).toBeGreaterThanOrEqual(3);
|
|
3223
|
-
expect(key).toBeLessThanOrEqual(7);
|
|
3224
|
-
});
|
|
3225
|
-
});
|
|
3226
|
-
|
|
3227
|
-
it('should maintain sorted iteration over key-value pairs', () => {
|
|
3228
|
-
const comparator = (a: { priority: number; id: number }, b: { priority: number; id: number }): number => {
|
|
3229
|
-
if (a.priority !== b.priority) {
|
|
3230
|
-
return a.priority > b.priority ? 1 : -1;
|
|
3231
|
-
}
|
|
3232
|
-
return a.id > b.id ? 1 : a.id < b.id ? -1 : 0;
|
|
3233
|
-
};
|
|
3234
|
-
|
|
3235
|
-
type TaskKey = { priority: number; id: number };
|
|
3236
|
-
type TaskValue = { title: string; completed: boolean };
|
|
3237
|
-
|
|
3238
|
-
const bst = new BST<TaskKey, TaskValue>([], { comparator, isMapMode: true });
|
|
3239
|
-
|
|
3240
|
-
const tasks: Array<[TaskKey, TaskValue]> = [
|
|
3241
|
-
[
|
|
3242
|
-
{ priority: 2, id: 1 },
|
|
3243
|
-
{ title: 'Task 1', completed: false }
|
|
3244
|
-
],
|
|
3245
|
-
[
|
|
3246
|
-
{ priority: 1, id: 2 },
|
|
3247
|
-
{ title: 'Task 2', completed: false }
|
|
3248
|
-
],
|
|
3249
|
-
[
|
|
3250
|
-
{ priority: 3, id: 3 },
|
|
3251
|
-
{ title: 'Task 3', completed: true }
|
|
3252
|
-
],
|
|
3253
|
-
[
|
|
3254
|
-
{ priority: 1, id: 1 },
|
|
3255
|
-
{ title: 'Task 4', completed: false }
|
|
3256
|
-
]
|
|
3257
|
-
];
|
|
3258
|
-
|
|
3259
|
-
tasks.forEach(([key, value]) => bst.set([key, value]));
|
|
3260
|
-
|
|
3261
|
-
expect(bst.size).toBe(4);
|
|
3262
|
-
|
|
3263
|
-
const inOrder = bst.dfs(node => node?.key.priority);
|
|
3264
|
-
for (let i = 1; i < inOrder.length; i++) {
|
|
3265
|
-
expect(inOrder[i]! >= inOrder[i - 1]!).toBe(true);
|
|
3266
|
-
}
|
|
3267
|
-
});
|
|
3268
|
-
});
|
|
3269
|
-
});
|
|
3270
|
-
|
|
3271
|
-
describe('classic use', () => {
|
|
3272
|
-
it('@example basic BST creation and add operation', async () => {
|
|
3273
|
-
// Create a simple BST with numeric keys
|
|
3274
|
-
const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
3275
|
-
|
|
3276
|
-
// Keep the example output in source comments but avoid noisy test logs.
|
|
3277
|
-
await withMutedConsole(() => bst.print());
|
|
3278
|
-
// _______8__________
|
|
3279
|
-
// / \
|
|
3280
|
-
// ___4___ ____12_____
|
|
3281
|
-
// / \ / \
|
|
3282
|
-
// _2_ _6_ _10__ _14__
|
|
3283
|
-
// / \ / \ / \ / \
|
|
3284
|
-
// 1 3 5 7 9 11 13 15__
|
|
3285
|
-
// \
|
|
3286
|
-
// 16
|
|
3287
|
-
|
|
3288
|
-
// Verify size
|
|
3289
|
-
expect(bst.size).toBe(16);
|
|
3290
|
-
|
|
3291
|
-
// Add new elements
|
|
3292
|
-
bst.set(17);
|
|
3293
|
-
bst.set(0);
|
|
3294
|
-
expect(bst.size).toBe(18);
|
|
3295
|
-
|
|
3296
|
-
// Verify keys are searchable
|
|
3297
|
-
expect(bst.has(11)).toBe(true);
|
|
3298
|
-
expect(bst.has(100)).toBe(false);
|
|
3299
|
-
});
|
|
3300
|
-
|
|
3301
|
-
it('@example BST delete and search after deletion', () => {
|
|
3302
|
-
const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
3303
|
-
|
|
3304
|
-
// Delete a leaf node
|
|
3305
|
-
bst.delete(1);
|
|
3306
|
-
expect(bst.has(1)).toBe(false);
|
|
3307
|
-
|
|
3308
|
-
// Delete a node with one child
|
|
3309
|
-
bst.delete(2);
|
|
3310
|
-
expect(bst.has(2)).toBe(false);
|
|
3311
|
-
|
|
3312
|
-
// Delete a node with two children
|
|
3313
|
-
bst.delete(3);
|
|
3314
|
-
expect(bst.has(3)).toBe(false);
|
|
3315
|
-
|
|
3316
|
-
// Size decreases with each deletion
|
|
3317
|
-
expect(bst.size).toBe(13);
|
|
3318
|
-
|
|
3319
|
-
// Other nodes remain searchable
|
|
3320
|
-
expect(bst.has(11)).toBe(true);
|
|
3321
|
-
expect(bst.has(15)).toBe(true);
|
|
3322
|
-
});
|
|
3323
|
-
|
|
3324
|
-
it('@example Merge 3 sorted datasets', () => {
|
|
3325
|
-
const dataset1 = new BST<number, string>([
|
|
3326
|
-
[1, 'A'],
|
|
3327
|
-
[7, 'G']
|
|
3328
|
-
]);
|
|
3329
|
-
const dataset2 = [
|
|
3330
|
-
[2, 'B'],
|
|
3331
|
-
[6, 'F']
|
|
3332
|
-
];
|
|
3333
|
-
const dataset3 = new BST<number, string>([
|
|
3334
|
-
[3, 'C'],
|
|
3335
|
-
[5, 'E'],
|
|
3336
|
-
[4, 'D']
|
|
3337
|
-
]);
|
|
3338
|
-
|
|
3339
|
-
// Merge datasets into a single BinarySearchTree
|
|
3340
|
-
const merged = new BST<number, string>(dataset1);
|
|
3341
|
-
merged.setMany(dataset2);
|
|
3342
|
-
merged.merge(dataset3);
|
|
3343
|
-
|
|
3344
|
-
// Verify merged dataset is in sorted order
|
|
3345
|
-
expect([...merged.values()]).toEqual(['A', 'B', 'C', 'D', 'E', 'F', 'G']);
|
|
3346
|
-
});
|
|
3347
|
-
|
|
3348
|
-
it('@example BST with custom objects for expression evaluation', () => {
|
|
3349
|
-
interface Expression {
|
|
3350
|
-
id: number;
|
|
3351
|
-
operator: string;
|
|
3352
|
-
precedence: number;
|
|
3353
|
-
}
|
|
3354
|
-
|
|
3355
|
-
// BST efficiently stores and retrieves operators by precedence
|
|
3356
|
-
const operatorTree = new BST<number, Expression>(
|
|
3357
|
-
[
|
|
3358
|
-
[1, { id: 1, operator: '+', precedence: 1 }],
|
|
3359
|
-
[2, { id: 2, operator: '*', precedence: 2 }],
|
|
3360
|
-
[3, { id: 3, operator: '/', precedence: 2 }],
|
|
3361
|
-
[4, { id: 4, operator: '-', precedence: 1 }],
|
|
3362
|
-
[5, { id: 5, operator: '^', precedence: 3 }]
|
|
3363
|
-
],
|
|
3364
|
-
{ isMapMode: false }
|
|
3365
|
-
);
|
|
3366
|
-
|
|
3367
|
-
expect(operatorTree.size).toBe(5);
|
|
3368
|
-
|
|
3369
|
-
// Quick lookup of operators
|
|
3370
|
-
const mult = operatorTree.get(2);
|
|
3371
|
-
expect(mult?.operator).toBe('*');
|
|
3372
|
-
expect(mult?.precedence).toBe(2);
|
|
3373
|
-
|
|
3374
|
-
// Check if operator exists
|
|
3375
|
-
expect(operatorTree.has(5)).toBe(true);
|
|
3376
|
-
expect(operatorTree.has(99)).toBe(false);
|
|
3377
|
-
|
|
3378
|
-
// Retrieve operator by precedence level
|
|
3379
|
-
const expNode = operatorTree.getNode(3);
|
|
3380
|
-
expect(expNode?.key).toBe(3);
|
|
3381
|
-
expect(expNode?.value?.precedence).toBe(2);
|
|
3382
|
-
|
|
3383
|
-
// Delete operator and verify
|
|
3384
|
-
operatorTree.delete(1);
|
|
3385
|
-
expect(operatorTree.has(1)).toBe(false);
|
|
3386
|
-
expect(operatorTree.size).toBe(4);
|
|
3387
|
-
|
|
3388
|
-
// Get tree height for optimization analysis
|
|
3389
|
-
const treeHeight = operatorTree.getHeight();
|
|
3390
|
-
expect(treeHeight).toBeGreaterThan(0);
|
|
3391
|
-
|
|
3392
|
-
// Remaining operators are still accessible
|
|
3393
|
-
const remaining = operatorTree.get(2);
|
|
3394
|
-
expect(remaining).toBeDefined();
|
|
3395
|
-
});
|
|
3396
|
-
|
|
3397
|
-
// Test case for Lowest Common Ancestor (LCA)
|
|
3398
|
-
it('@example Find lowest common ancestor', () => {
|
|
3399
|
-
const bst = new BST<number>([20, 10, 30, 5, 15, 25, 35, 3, 7, 12, 18]);
|
|
3400
|
-
|
|
3401
|
-
// LCA helper function
|
|
3402
|
-
const findLCA = (num1: number, num2: number): number | undefined => {
|
|
3403
|
-
const path1 = bst.getPathToRoot(num1);
|
|
3404
|
-
const path2 = bst.getPathToRoot(num2);
|
|
3405
|
-
// Find the first common ancestor
|
|
3406
|
-
return findFirstCommon(path1, path2);
|
|
3407
|
-
};
|
|
3408
|
-
|
|
3409
|
-
function findFirstCommon(arr1: (number | undefined)[], arr2: (number | undefined)[]): number | undefined {
|
|
3410
|
-
for (const num of arr1) {
|
|
3411
|
-
if (arr2.indexOf(num) !== -1) {
|
|
3412
|
-
return num;
|
|
3413
|
-
}
|
|
3414
|
-
}
|
|
3415
|
-
return undefined;
|
|
3416
|
-
}
|
|
3417
|
-
|
|
3418
|
-
// Assertions
|
|
3419
|
-
expect(findLCA(3, 10)).toBe(7);
|
|
3420
|
-
expect(findLCA(5, 35)).toBe(15);
|
|
3421
|
-
expect(findLCA(20, 30)).toBe(25);
|
|
3422
|
-
});
|
|
3423
|
-
|
|
3424
|
-
// Test case for finding elements in a given range
|
|
3425
|
-
it('Find elements in a range', () => {
|
|
3426
|
-
const bst = new BST<number>([10, 5, 15, 3, 7, 12, 18]);
|
|
3427
|
-
expect(bst.search(new Range(5, 10))).toEqual([5, 7, 10]);
|
|
3428
|
-
expect(bst.rangeSearch([4, 12], node => node.key.toString())).toEqual(['5', '7', '10', '12']);
|
|
3429
|
-
expect(bst.search(new Range(4, 12, true, false))).toEqual([5, 7, 10]);
|
|
3430
|
-
expect(bst.rangeSearch([15, 20])).toEqual([15, 18]);
|
|
3431
|
-
expect(bst.search(new Range(15, 20, false))).toEqual([18]);
|
|
3432
|
-
});
|
|
3433
|
-
|
|
3434
|
-
it('BST get and getNode operations', () => {
|
|
3435
|
-
const bst = new BST<number>([5, 3, 7, 1, 4, 6, 8]);
|
|
3436
|
-
|
|
3437
|
-
// Get value by key
|
|
3438
|
-
const value = bst.get(5);
|
|
3439
|
-
expect(value).toBe(undefined);
|
|
3440
|
-
|
|
3441
|
-
// Get node returns the actual node object
|
|
3442
|
-
const node = bst.getNode(3);
|
|
3443
|
-
expect(node?.key).toBe(3);
|
|
3444
|
-
expect(node?.left).toBeDefined();
|
|
3445
|
-
expect(node?.right).toBeDefined();
|
|
3446
|
-
|
|
3447
|
-
// Get from non-existent key returns undefined
|
|
3448
|
-
expect(bst.get(100)).toBeUndefined();
|
|
3449
|
-
expect(bst.getNode(100)).toBeUndefined();
|
|
3450
|
-
});
|
|
3451
|
-
|
|
3452
|
-
it('BST getHeight and tree structure queries', () => {
|
|
3453
|
-
const bst = new BST<number>([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
|
3454
|
-
|
|
3455
|
-
// Get overall tree height
|
|
3456
|
-
const treeHeight = bst.getHeight();
|
|
3457
|
-
expect(typeof treeHeight).toBe('number');
|
|
3458
|
-
expect(treeHeight).toBeGreaterThan(0);
|
|
3459
|
-
|
|
3460
|
-
// Get height of specific node
|
|
3461
|
-
const heightOf3 = bst.getHeight(3);
|
|
3462
|
-
expect(typeof heightOf3).toBe('number');
|
|
3463
|
-
|
|
3464
|
-
// Root node should have the maximum height
|
|
3465
|
-
const heightOf11 = bst.getHeight(11);
|
|
3466
|
-
expect(heightOf11).toBe(0);
|
|
3467
|
-
|
|
3468
|
-
// Leaf nodes have height 0
|
|
3469
|
-
const heightOf2 = bst.getHeight(2);
|
|
3470
|
-
expect(heightOf2).toBe(1);
|
|
3471
|
-
});
|
|
3472
|
-
});
|