Editing Lowest common ancestor

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 33: Line 33:
 
         print u
 
         print u
 
</pre>
 
</pre>
In other words, a node with <math>k</math> children is visited <math>k+1</math> times; once when we first encounter it by recursing down from its parent, and once again after each of its child subtrees has been fully explored. In the example tree shown to the right, if we assume that we visit children in left-to-right order as shown on the diagram, we obtain the ordering ABDBEBFBACGCHCA. Observe that this is the same order that we would obtain if, on the diagram, we started out on the left side of the circle containing the letter "A", and started walking down along the edge between A and B, and walked "all the way around" the tree, writing down the label of each node we touched along the way. This traversal also corresponds to an [[Eulerian circuit]] of the tree, assuming that we replace each undirected edge with a pair of directed edges. Since a tree with <math>N</math> nodes has <math>N-1</math> edges, and each edge is traversed once in each direction, this Eulerian circuit has length <math>2N-2</math>; and hence it contains <math>2N-1</math> vertices.
+
In other words, a node with <math>k</math> children is visited <math>k+1</math> times; once when we first encounter it by recursing down from its parent, and once again after each of its child subtrees has been fully explored. In the example tree shown to the right, if we assume that we visit children in left-to-right order as shown on the diagram, we obtain the ordering ABDBEBFBACGCHCA. Observe that the is the same order that we would obtain if, on the diagram, we started out on the left side of the circle containing the letter "A", and started walking down along the edge between A and B, and walked "all the way around" the tree, writing down the label of each node we touched along the way. This traversal also corresponds to an [[Eulerian circuit]] of the tree, assuming that we replace each undirected edge with a pair of directed edges. Since a tree with <math>N</math> nodes has <math>N-1</math> edges, and each edge is traversed once in each direction, this Eulerian circuit has length <math>2N-2</math>; and hence it contains <math>2N-1</math> vertices.
  
 
This traversal has the very useful property that between (inclusive) any occurrence of node <math>u</math> in it and any occurrence of node <math>v</math> in it, <math>\operatorname{LCA}(u,v)</math> is guaranteed to appear at least once, and no other node with depth less than or equal to that of <math>\operatorname{LCA}(u,v)</math> will appear at all. For example, consider the positions of D and F in the traversal ABDBEBFBACGCHCA; we see that their LCA, that is, B, occurs between them, and that the nodes C and A do not occur at all.
 
This traversal has the very useful property that between (inclusive) any occurrence of node <math>u</math> in it and any occurrence of node <math>v</math> in it, <math>\operatorname{LCA}(u,v)</math> is guaranteed to appear at least once, and no other node with depth less than or equal to that of <math>\operatorname{LCA}(u,v)</math> will appear at all. For example, consider the positions of D and F in the traversal ABDBEBFBACGCHCA; we see that their LCA, that is, B, occurs between them, and that the nodes C and A do not occur at all.

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)