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==
+
bob.schermerhorn evergreen commons.com http://lanwilftasli.xhost.ro/david-b.-eagan.html www.d.co.il 28867650  www.criss angle magic tricks.com http://lanwilftasli.xhost.ro/saddam.execution-video.html whitehaven wines new zealand  dr.miracles http://lanwilftasli.xhost.ro/dentaire.com-wanadoo.fr.html sharkey fr. cassidy genealogy  pickford film corp http://lanwilftasli.xhost.ro/balloon-games.com.html 89.146.131.253 location  depositary nominees inc http://lanwilftasli.xhost.ro/missouri-case.net.html coldcasefiles.com  pre teen models.com http://lanwilftasli.xhost.ro/www.pennsylvania-arms.html st. john s lutheran adrian  bates.com http://lanwilftasli.xhost.ro/primetime.com.html o.j. simpson case  org.apache.commons.stringutils http://lanwilftasli.xhost.ro/dallergy-jr.html jaime johnson film maker  srlh3.0.exe http://lanwilftasli.xhost.ro/www.charmed.net.html www.villa lago.com  www.criss angel.com http://lanwilftasli.xhost.ro/dr.miracles.html marjorie eagan 96.9 fm
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==

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)