Note that there are some explanatory texts on larger screens.

plurals
  1. POAdjust Range Search of BinaryTree to get the Outer Elements
    primarykey
    data
    text
    <p>I have a RedBlack [Balanced, sorted] Binary Tree and I am searching it to find all the values within the range [lower, upper]. </p> <pre><code>public IEnumerable&lt;TData&gt; Range( BinaryTree&lt;TData&gt; root, IComparer&lt;TData&gt; comparer, TData lower, TData upper) { var stack = new Stack&lt;State&gt;(16); BinaryTree&lt;TData&gt; here = root; do { if (here == null) { if (stack.Count == 0) break; State popped = stack.Pop(); yield return popped.Data; here = popped.Next; continue; } if (comparer.Compare(here.Data, lower) &lt; 0) { here = here.Right; } else if (comparer.Compare(here.Data, upper) &gt; 0) { here = here.Left; } else { stack.Push(new State {Next = here.Right, Data = here.Data}); here = here.Left; } } while (true); } </code></pre> <p>So with this code, if I were to have a tree built with the values</p> <pre><code> [0, 1, 4, 5, 6, 9], </code></pre> <p>and search for all elements within the range </p> <pre><code> [3, 8] </code></pre> <p>I would get the following results: </p> <pre><code> [4, 5, 6]. </code></pre> <p>My question is how can I do about adjusting this algorithm in order to get the outer elements of the search? like this: </p> <pre><code> [1, 4, 5, 6, 9] </code></pre> <p>i.e. the value 3 lies between 1 and 4 in the tree, so I want to return 1, similarly the value 8 lies between 6 and 9 and I would want the value 9 to be includes in the result. </p> <p>One catch is I don't want to restart the search from root</p> <p>Currently implemented using NGenerics</p> <p>[Edit] </p> <p>Willing to accept a general algorithmic answer. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload