Sunday, December 27, 2009

Next Smaller Node


public Node getNextSmaller(Node r)
{
Node t = root;
Node gp = root;
Node p = root;
t = root;

while (t != null && !r.Equals(t))
{
gp = p;
p = t;
if (r.CompareTo(t) < 0)
{
t = t.Left;

}
else
{
t = t.Right;
}

}
if (t.Data == this.getMin()) return null;
if (t.Left == null && p.Data < t.Data) return p;
if (t.Left == null && p.Data > t.Data) return gp;
if (t.Left != null && t.Left.Right != null)
{
t = t.Left.Right;
while (t.Right != null)
{
t = t.Right;
}
return t;
}
if (t.Left != null && t.Left.Right == null) return t.Left;

return null;
}

No comments:

Node.JS rest api Tutorials