1

Here is the function I wrote for traversing an interval tree. I notice it fails to visit some nodes though. Assuming the code is pretty clear, I want to know where it fails.

public boolean searchTree(Node node,int x)
{

            while(node!=null&&!node.getInterval().containsPoint(x))
            {
                if(node.getNodeLeft()!=null&&(node.getNodeLeft().getMax()>=x))
                {
                    node=node.getNodeLeft();
                }
                else
                {
                    node=node.getNodeRight();
                }       
            }
           return node!=null;
}

1 Answers1

0

Any node in the tree is centered around a point, say p. Left subtree contains all intervals that are to the left of p, right subtree contains all the intervals that are to the right of p. The node itself contains all intervals, which overlap p.

Now if your x < p, then there might be intervals from the left subtree, which contain x, but also there might be intervals from the node itself, which contain x (and p). The only guarantee is that the right subtree won't contain intervals that contain x.

So you are missing those intervals in the node itself.

I didn't know what an interval tree was, so my inderstanding is from here http://en.wikipedia.org/wiki/Interval_tree

Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
  • Introduction to Algorithm have a chapter talking about interval tree – pengdu Apr 27 '12 at 10:54
  • Petar Ivanov , I think you are giving the answer based on the Centered interval tree design. My design is the one presented in the book Introduction to Algorithms Introduction by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. –  Apr 27 '12 at 11:41