Editing Cartesian 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 22: Line 22:
  
 
Thus, we begin by constructing a singleton tree containing only the first element <math>a_1</math> in the sequence; this is a valid Cartesian tree for the segment of the sequence containing only the first element. Then, we repeatedly examine the next element <math>a_{i+1}</math>. At each step, we keep track of <math>v_i</math>, that is, the node that has label <math>a_i</math> (the last value inserted):
 
Thus, we begin by constructing a singleton tree containing only the first element <math>a_1</math> in the sequence; this is a valid Cartesian tree for the segment of the sequence containing only the first element. Then, we repeatedly examine the next element <math>a_{i+1}</math>. At each step, we keep track of <math>v_i</math>, that is, the node that has label <math>a_i</math> (the last value inserted):
* If <math>a_{i+1} > a_i</math>, then we just insert a new node <math>v_{i+1}</math> as the right child of <math>v_i</math>, and label this new node with <math>a_{i+1}</math>.
+
* If <math>a_{i+1} > a_i</math>, then we just insert a new node <math>v_{i+1}</math> as the right child of <math>v_1</math>, and label this new node with <math>a_{i+1}</math>.
 
* Otherwise, we consider node <math>P(v_i)</math>, the parent of <math>v_i</math>. If the label of <math>P(v_i)</math> is less than or equal to <math>a_{i+1}</math>, we stop. Otherwise, we look to ''its'' parent, <math>P(P(v_i))</math>, and see whether ''its'' label is less than or equal to <math>a_{i+1}</math> and so on. If we stop at node <math>u</math>, with right child <math>w</math>, then we insert the new node <math>v_{i+1}</math> so that it is the right child of <math>u</math>, and make <math>w</math> the left child of <math>v_{i+1}</math>.
 
* Otherwise, we consider node <math>P(v_i)</math>, the parent of <math>v_i</math>. If the label of <math>P(v_i)</math> is less than or equal to <math>a_{i+1}</math>, we stop. Otherwise, we look to ''its'' parent, <math>P(P(v_i))</math>, and see whether ''its'' label is less than or equal to <math>a_{i+1}</math> and so on. If we stop at node <math>u</math>, with right child <math>w</math>, then we insert the new node <math>v_{i+1}</math> so that it is the right child of <math>u</math>, and make <math>w</math> the left child of <math>v_{i+1}</math>.
 
* If we trace all the way up to the root of the tree so far, and still find no node with label less than or equal to <math>a_{i+1}</math>, then <math>a_{i+1}</math> is the smallest element seen so far; so make the new node <math>v_{i+1}</math> the root of the tree, and make the old root <math>v_{i+1}</math>'s left child.
 
* If we trace all the way up to the root of the tree so far, and still find no node with label less than or equal to <math>a_{i+1}</math>, then <math>a_{i+1}</math> is the smallest element seen so far; so make the new node <math>v_{i+1}</math> the root of the tree, and make the old root <math>v_{i+1}</math>'s left child.

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)