Editing Segment tree

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
 
The '''segment tree''' is a highly versatile [[data structure]], based upon the [[Divide and conquer|divide-and-conquer]] paradigm, which can be thought of as a tree of intervals of an underlying array, constructed so that queries on ranges of the array as well as modifications to the array's elements may be efficiently performed.
 
The '''segment tree''' is a highly versatile [[data structure]], based upon the [[Divide and conquer|divide-and-conquer]] paradigm, which can be thought of as a tree of intervals of an underlying array, constructed so that queries on ranges of the array as well as modifications to the array's elements may be efficiently performed.
 
==Motivation==
 
One of the most common applications of the segment tree is the solution to the [[range minimum query]] problem. In this problem, we are given some array and repeatedly asked to find the minimum value within some specified range of indices. For example, if we are given the array <math>[9,2,6,3,1,5,0,7]</math>, we might be asked for the minimum element between the third and the sixth, inclusive, which would be <math>\min(6,3,1,5) = 1</math>. Then, another query might ask for the minimum element between the first and third, inclusive, and we would answer 2, and so on. Various solutions to this problem are discussed in the [[range minimum query]] article, but the segment tree is often the most appropriate choice, especially when modification instructions are interspersed with the queries. For the sake of brevity, we shall focus for several following sections on the type of segment tree designed to answer the range minimum query without explicitly re-stating each time that we are doing so. Bear in mind, however, that other types of segment tree exist, which are discussed later in the article.
 
 
===The divide-and-conquer solution===
 
The divide-and-conquer solution would be as follows:
 
* If the range contains one element, that element itself is trivially the minimum within that range.
 
* Otherwise, divide the range into two smaller ranges, each approximately half the size of the original, and find their respective minima. The minimum for the original range is then the smaller of the two minima of the sub-ranges.
 
Hence, if <math>a_i</math> denotes the <math>i</math><sup>th</sup> element in the array, finding the minimum could be encoded as the following recursive function:
 
:<math>\displaystyle
 
f(x,y) =
 
\begin{cases}
 
a_x & \mathrm{if\ } x = y \\
 
\min(f(x,\lfloor\frac{x+y}{2}\rfloor),f(\lfloor\frac{x+y}{2}\rfloor+1,y)) & \mathrm{otherwise} \\
 
\end{cases}
 
</math>
 
assuming that <math>x \le y</math>.<br/>
 
Hence, for example, the first query from the previous section would be <math>f(3,6)</math> and it would be recursively evaluated as <math>\min(f(3,4),f(5,6))</math>.
 
  
 
==Structure==
 
==Structure==
Line 43: Line 25:
  
 
===Query===
 
===Query===
 +
Hai!!! Mesa happie to se yousa!
 
[[File:Segtree_query_92631587.png|200px|thumb|right|Only the two nodes marked in yellow must be accessed to find the minimum of the elements corresponding to the leaves marked in grey.]]
 
[[File:Segtree_query_92631587.png|200px|thumb|right|Only the two nodes marked in yellow must be accessed to find the minimum of the elements corresponding to the leaves marked in grey.]]
 
To ''query'' a segment tree is to use it to determine a function of a range in the underlying array (in this case, the minimum element of that range). The execution of a query is more complex than the execution of an update and will be illustrated by example. Suppose we wish to know the minimum element between the first and sixth, inclusive. We shall represent this query as <math>f(1,6)</math>. Each node in the segment tree contains the minimum in some interval: for example, the root node contains <math>f(1,8)</math>, its left child <math>f(1,4)</math>, its right <math>f(5,8)</math>, and so on, with each leaf containing <math>f(x,x)</math> for some <math>x</math>. There is no node that contains <math>f(1,6)</math>, but we notice that <math>f(1,6) = \min(f(1,4),f(5,6))</math>, and that there ''are'' nodes in the segment tree containing those two values (shown in yellow). (This expression for <math>f(1,6)</math> is not the one given by the definition of <math>f</math>, but it is fairly clear that <math>f(x,y) = \min(f(x,z),f(z+1,y))</math> where <math>x \le z < y</math>, regardless of the actual value of <math>z</math>.)<br/>
 
To ''query'' a segment tree is to use it to determine a function of a range in the underlying array (in this case, the minimum element of that range). The execution of a query is more complex than the execution of an update and will be illustrated by example. Suppose we wish to know the minimum element between the first and sixth, inclusive. We shall represent this query as <math>f(1,6)</math>. Each node in the segment tree contains the minimum in some interval: for example, the root node contains <math>f(1,8)</math>, its left child <math>f(1,4)</math>, its right <math>f(5,8)</math>, and so on, with each leaf containing <math>f(x,x)</math> for some <math>x</math>. There is no node that contains <math>f(1,6)</math>, but we notice that <math>f(1,6) = \min(f(1,4),f(5,6))</math>, and that there ''are'' nodes in the segment tree containing those two values (shown in yellow). (This expression for <math>f(1,6)</math> is not the one given by the definition of <math>f</math>, but it is fairly clear that <math>f(x,y) = \min(f(x,z),f(z+1,y))</math> where <math>x \le z < y</math>, regardless of the actual value of <math>z</math>.)<br/>

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)