Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Range query over binary search tree?

Name: Anonymous 2008-09-09 8:01

Okay, so does anyone know or can think of an efficient way to complete a range query over a given number range ie (x, y) in a standard binary search tree, using a minimum number of key comparisons? I wrote one that finds the minimum value <= x and then traverses the tree value by value until it finds a value that is above y, but I just realised that it would be a lot more efficient if it was recursive, and did not provide the range query's response in a sorted order.

Unfortunately, I have writer's block. Still getting used to getting my head in algorithm-space. Can anyone give me some pointers to get me started?

Name: Anonymous 2008-09-10 5:25

Or alternatively, I guess I could post it here, and hope for some suggestions? It's pretty ugly code, especially in the traverse function - I haven't programmed in Java for years.

    public List<V> getValues(K rangeMin, K rangeMax)
    {
        count = 0; //Automatically set count equal to zero incase getLastCompare was not called.
        List<V> values = new LinkedList<V>();
        Node<K,V> p = root;
        boolean found = false;
        while(p.inited == 1 && !found){
            if(rangeMax.compareTo(p.key()) < 0){
                p = p.left();
                count++;
            }
            else if(rangeMin.compareTo(p.key()) > 0){
                p = p.right();
                count++;
            }
            else if(p.inited != 0){
                count++;
                values = traverse(p, rangeMin, rangeMax, values);
                found = true;
            }
        }
        return values;
    }

    private List<V> traverse(Node<K,V> node, K rangeMin, K rangeMax, List<V> values)
    {
        Node<K,V> p = node;
        values.add(node.value());
        if(p.left().inited == 1){
            p = p.left();
            count++;
            if(rangeMin.compareTo(p.key()) <= 0){
                values = traverse(p, rangeMin, rangeMax, values);
            }
            else{
                while(p.inited == 1 && rangeMin.compareTo(p.key()) > 0){
                    p = p.right();
                    count++;
                }
                if(p.inited == 1){
                    values = traverse(p, rangeMin, rangeMax, values);
                }
            }
        }
        p = node;
        if(p.right().inited == 1){
            p = p.right();
            count++;
            if(rangeMax.compareTo(p.key()) >= 0){
                values = traverse(p, rangeMin, rangeMax, values);
            }
            else{
                while(p.inited == 1 && rangeMax.compareTo(p.key()) < 0){
                    p = p.left();
                    count++;
                }
                if(p.inited == 1){
                    values = traverse(p, rangeMin, rangeMax, values);
                }
            }
        }
        return values;
    }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List