@@ -0,0 +1,1034 @@
+package com.github.cschen1205.jp.chap04;%0A%0A/**%0A * Created by memeanalytics on 28/6/17.%0A * Write an algorithm to find the %E2%80%98next%E2%80%99 node (e g , in-order successor) of a given node in a binary search tree%0A * where each node has a link to its parent%0A */%0Apublic class Q05 %7B%0A    public static class TreeNode %7B%0A        public TreeNode parent;%0A        public TreeNode left;%0A        public TreeNode right;%0A    %7D%0A%0A    public static TreeNode min(TreeNode e)%7B%0A        if(e.left == null)%7B%0A            return e;%0A        %7D%0A        return min(e.left);%0A    %7D%0A%0A    public static TreeNode inorderSucc(TreeNode e) %7B%0A%0A        TreeNode p = null;%0A        if(e != null) %7B%0A            if(e.right != null %7C%7C p.parent == null)%7B%0A                p = min(e);%0A            %7D else %7B%0A                TreeNode x = e;%0A                while(x != null)%7B%0A                    p = x;%0A                    if(p.left == e) %7B%0A                        break;%0A                    %7D%0A                    x = x.parent;%0A                %7D%0A            %7D%0A        %7D%0A        return p;%0A    %7D%0A%0A%7D%0A

Return inorder successor