Editing Segment tree
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. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==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/> |