NODE * Tree_Successor ( NODE *x //don't need the root: find in the subtree of x ) { //return the smallest node of those larger than x. NODE *suc; NODE *y; if (x->right) { suc = Tree_Minimum (x->right); } else { //look upward y = x->p; while (y && x == y->right) { //y < x //traverse until y > x (x is left child) x = y; y = y->p; } suc = y; } return suc; }
rb_vertex<T>* Tree_Successor(rb_vertex<T>* subject) { if (subject->get_right_child() != 0) { return Tree_Minimum(subject); } rb_vertex<T>* temp = subject->get_parent(); while(temp != 0 and subject == temp->get_right_child()) { subject = temp; temp = temp->get_parent(); } return temp; }