1 Star 0 Fork 0

QuickSpeed/javascript-algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
.husky
assets
src
algorithms
data-structures
bloom-filter
disjoint-set
doubly-linked-list
graph
hash-table
heap
linked-list
priority-queue
queue
stack
tree
__test__
avl-tree
binary-search-tree
fenwick-tree
red-black-tree
segment-tree
__test__
README.md
README.pt-BR.md
SegmentTree.js
BinaryTreeNode.js
README.md
README.pt-BR.md
README.zh-CN.md
trie
playground
utils/comparator
.babelrc
.editorconfig
.eslintrc
.gitignore
.npmrc
BACKERS.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
README.ar-AR.md
README.de-DE.md
README.es-ES.md
README.fr-FR.md
README.id-ID.md
README.it-IT.md
README.ja-JP.md
README.ko-KR.md
README.md
README.pl-PL.md
README.pt-BR.md
README.ru-RU.md
README.tr-TR.md
README.uk-UA.md
README.zh-CN.md
README.zh-TW.md
jest.config.js
package-lock.json
package.json
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Segment Tree

Read this in other languages: Português

In computer science, a segment tree also known as a statistic tree is a tree data structure used for storing information about intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, it's a structure that cannot be modified once it's built. A similar data structure is the interval tree.

A segment tree is a binary tree. The root of the tree represents the whole array. The two children of the root represent the first and second halves of the array. Similarly, the children of each node corresponds to the two halves of the array corresponding to the node.

We build the tree bottom up, with the value of each node being the "minimum" (or any other function) of its children's values. This will take O(n log n) time. The number of operations done is the height of the tree, which is O(log n). To do range queries, each node splits the query into two parts, one sub-query for each child. If a query contains the whole subarray of a node, we can use the precomputed value at the node. Using this optimisation, we can prove that only O(log n) minimum operations are done.

Min Segment Tree

Sum Segment Tree

Application

A segment tree is a data structure designed to perform certain array operations efficiently - especially those involving range queries.

Applications of the segment tree are in the areas of computational geometry, and geographic information systems.

Current implementation of Segment Tree implies that you may pass any binary (with two input params) function to it and thus you're able to do range query for variety of functions. In tests you may find examples of doing min, max and sum range queries on SegmentTree.

References

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/quick-speed/javascript-algorithms.git
git@gitee.com:quick-speed/javascript-algorithms.git
quick-speed
javascript-algorithms
javascript-algorithms
master

搜索帮助