In computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structure; that is, its structure cannot be modified once it is built. A similar data structure is the interval tree.
A segment tree for a set I ofn intervals uses O(nlogn) storage and can be built in O(nlogn) time. Segment trees support searching for all the intervals that contain a query point in O(logn+k) , k being the number of retrieved intervals or segments.
Applications of the segment tree are in the areas of computational geometry, and geographic information systems.
The segment tree can be generalized to higher dimension spaces as well.
——Wikipedia
线段树是用于存储区间的一种树状数据结构。它允许查询所存储的区间含有一个给定的点。它是一个静态数据结构,也就是说,它的结构一旦建立就不能修改(不能中途增删节点)。
一棵维护n个数据的线段树的空间复杂度为
O(nlogn)
,建树的时间复杂度为
O(nlogn)
。线段树可以在
O(logn+k)
的时间内搜索所有包含查询点的区间(k是查询区间的数目)。
有时线段树会应用于计算几何。线段树可以推广到更高维空间。
线段树作为一种常用的数据结构,有常用性,基础性和易用性等诸多特点。它的基本操作有构造线段树(建树),区间查询,单点修改,区间修改。
一、构造线段树
首先,(一维)线段树是一棵二叉树。同时,“线段”两字反映出线段树的另一个特点:每个节点表示的是一个“线段”,或者说是一个区间。一棵线段树的根节点表示的是“整体”的区间,而它的左右子树也是一棵线段树,分别表示的是这个区间的左半边和右半边。