Alternative headline: traverse all nodes in a tree between two nodes.
I have a DOM tree (an ordered tree, where child elements at each level have a specific order). I am given start and stop nodes in the tree. The start is guaranteed to be before the stop in a depth-first traversal of the tree. I want to find/traverse all the nodes between these two efficiently. This means that if I can determine that a node between the two does NOT include the stop node, I do not want to recurse through all its descendants.
Is this a well-known (named?) problem, that has algorithms for the traversal, that perhaps computes shared ancestors or such to determine whether traverse a node's descendants?
Example 0 (same level):
<body>
<p>ignore this</p>
<div id="start">start here</div>
<p id="A">visit A</p>
<p id="B">visit B</p>
<p id="C">visit C <span>but <b>not</b> <i>all this</i></span></p>
<div id="stop">stop here</div>
<p>ignore this</p>
</body>
In Example 0, I want to visit the following, in order:
#A#B#C(but not all its descendants)
Example 1 (stop level is deeper):
<body>
<p>ignore this</p>
<div><h2><span id="start">start here</span> A1</h2> A2</div>
<section id="B"><many><more><levels><of><content>…</many></section>
<section id="C">
<div id="D"><many><more><levels><of><content>…</many></div>
<div id="E">
<div id="F"><many><more><levels><of><content>…</many></div>
<div id="stop">stop here</div>
<p>ignore this</p>
</div>
</section>
<p>ignore this</p>
</body>
In Example 1, I want to visit the following, in order:
- the TextNodes
A1andA2 #B(but not all its descendants)#C(or maybe not?)#D(but not all its descendants)#E(or maybe not?)#F(but not all its descendants)
Example 2 (stop is higher):
<body>
<p>ignore this</p>
<div>
<h2>
<span id="start">start here</span>
<div id="A">visit A</div>
<section id="B"><many><more><levels><of><content>…</many></section>
</h2>
<section id="C"><many><more><levels><of><content>…</many></section>
</div>
<section id="D"><many><more><levels><of><content>…</many></section>
<div id="stop">stop here</div>
<p>ignore this</p>
</body>
In Example 2, I want to visit the following, in order:
#A#B(but not its descendants)#C(but not its descendants)#D(but not its descendants)