Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7610d5

Browse files
committed
Update README.
1 parent2e5fd8d commitc7610d5

File tree

1 file changed

+46
-4
lines changed
  • src/data-structures/tree/segment-tree

1 file changed

+46
-4
lines changed
Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
#Segment Tree
22

3-
A segment tree is a data structure designed to perform certain array operations efficiently - especially those involving range queries. A common application is the[Range Minimum Query](https://en.wikipedia.org/wiki/Range_minimum_query) (RMQ) problem, where we are given an array of numbers and need to support operations of updating values of the array and finding the minimum of a contiguous subarray. A segment tree implementation for the RMQ problem takes O(n) to initialize, and O(log n) per query or update. The "minimum" operation can be replaced by any array operation (such as sum).
3+
A segment tree is a data structure designed to perform
4+
certain array operations efficiently - especially those
5+
involving range queries.
46

5-
A segment tree is a binary tree with contiguous subarrays as nodes. 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. If the array has size n, we can prove that the segment tree has size at most 4n. Each node stores the minimum of its corresponding subarray.
7+
A common application is the[Range Minimum Query](https://en.wikipedia.org/wiki/Range_minimum_query) (RMQ) problem,
8+
where we are given an array of numbers and need to
9+
support operations of updating values of the array and
10+
finding the minimum of a contiguous subarray.
11+
A segment tree implementation for the RMQ problem
12+
takes`O(n)` to initialize, and`O(log n)` per query or
13+
update. The "minimum" operation can be replaced by any
14+
array operation (such as sum).
615

7-
In the implementation, we do not explicity store this tree structure, but represent it using a $4n$ sized array. The left child of node i is 2i and the right child is 2i+1. This is a standard way to represent segment trees, and lends itself to an efficient implementation.
16+
A segment tree is a binary tree with contiguous
17+
sub-arrays as nodes. The root of the tree represents the
18+
whole array. The two children of the root represent the
19+
first and second halves of the array. Similarly, the
20+
children of each node corresponds to the two halves of
21+
the array corresponding to the node. If the array has
22+
size`n`, we can prove that the segment tree has size at
23+
most`4n`. Each node stores the minimum of its
24+
corresponding sub-array.
825

9-
We build the tree bottom up, with the value of each node being the minimum of its children's values. This will take time O(n), with one operation for each node. Updates are also done bottom up, with values being recomputed starting from the leaf, and up to the root. The number of operations done is the height of the tree, which is O(log n) To answer queries, each node splits the query into two parts, one subquery 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.
26+
In the implementation, we do not explicitly store this
27+
tree structure, but represent it using a`4n` sized array.
28+
The left child of node i is`2i+1` and the right child
29+
is`2i+2`. This is a standard way to represent segment
30+
trees, and lends itself to an efficient implementation.
31+
32+
We build the tree bottom up, with the value of each node
33+
being the minimum of its children's values. This will
34+
take time`O(n)`, with one operation for each node. Updates
35+
are also done bottom up, with values being recomputed
36+
starting from the leaf, and up to the root. The number
37+
of operations done is the height of the tree, which
38+
is`O(log n)`. To answer queries, each node splits the
39+
query into two parts, one sub-query for each child.
40+
If a query contains the whole subarray of a node, we
41+
can use the precomputed value at the node. Using this
42+
optimisation, we can prove that only`O(log n)` minimum
43+
operations are done.
44+
45+
![Segment Tree](https://www.geeksforgeeks.org/wp-content/uploads/segment-tree1.png)
46+
47+
##References
48+
49+
-[Wikipedia](https://en.wikipedia.org/wiki/Segment_tree)
50+
-[YouTube](https://www.youtube.com/watch?v=ZBHKZF5w4YU&index=65&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
51+
-[GeeksForGeeks](https://www.geeksforgeeks.org/segment-tree-set-1-sum-of-given-range/)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp