Sunday, December 27, 2009

Next Greater Node

public Node getNextGreater(Node n)
{
Node t = null;
Node p = root;
t = root;
Node gp = null;
while (t != null && !n.Equals(t))
{
gp = p;
p = t;
if (n.CompareTo(t) < 0)
{
t = t.Left;

}
else
{
t = t.Right;
}

}
if (n.Data == this.getMax()) return null;
if (t.Right == null && t.Equals(p.Right)) return gp;
if (t.Right == null && t.Equals(p.Left)) return p;
if (t.Right.Left == null && t.Right.Right == null) return t.Right;
if (t.Right.Left != null) return t.Right.Left;
return null;
}

No comments:

Node.JS rest api Tutorials