private static Node MinValue(Node root){ Node n = root; /* loop down to find the leftmost leaf */ while (n != null) { n = n.left; } return n; }
//find next largest node using parent pointer public static Node nextLargest(Node n, Node root){ //if n.right exist, then find the min node in its right subtree if (n.right != null) return MinValue (n.right); //if not, loop up to find a node which is the left child of its parent //the parent is the next largest node Node p = n.parent; while (p != null && n == p.right) { n = p; p = p.parent; } return p; }