11package com .ctci .treesandgraphs ;
22
33/**
4+ * Write an algorithm to find the "next" node (i.e., in-order successor) of a given node
5+ * in a binary search tree. You may assume that each node has a link to its parent.
6+ *
47 * @author rampatra
58 * @since 2019-02-17
69 */
710public class Successor {
811
12+ /**
13+ * To get the inorder successor what this method does is that it checks if the right child of the input node
14+ * is null and if not, gets the leftmost child of the right child. And, if the right child of the input
15+ * node is null, it checks all the parents until it finds the next successor.
16+ *
17+ * @param node
18+ * @return
19+ */
20+ private static TreeNode getInOrderSuccessor (TreeNode node ) {
21+ if (node == null ) return null ;
22+
23+ if (node .right != null ) {
24+ return getLeftmostNode (node .right );
25+ } else {
26+ TreeNode curr = node ;
27+
28+ while (curr != null ) {
29+ if (curr .parent != null && curr .parent .left == curr ) {
30+ return curr .parent ;
31+ }
32+ curr = curr .parent ;
33+ }
34+ }
35+ return null ;
36+ }
37+
38+ private static TreeNode getLeftmostNode (TreeNode node ) {
39+ TreeNode curr = node ;
40+ while (curr != null && curr .left != null ) {
41+ curr = curr .left ;
42+ }
43+ return curr ;
44+ }
45+
946 public static void main (String [] args ) {
47+ /*
48+ The binary search tree looks like:
1049
50+ 4
51+ / \
52+ 2 8
53+ / \ / \
54+ 1 3 6 9
55+ / \
56+ 0 7
57+
58+ */
59+ TreeNode treeRoot = new TreeNode (4 );
60+ treeRoot .left = new TreeNode (2 );
61+ treeRoot .left .parent = treeRoot ;
62+ treeRoot .right = new TreeNode (8 );
63+ treeRoot .right .parent = treeRoot ;
64+ treeRoot .left .left = new TreeNode (1 );
65+ treeRoot .left .left .parent = treeRoot .left ;
66+ treeRoot .left .right = new TreeNode (3 );
67+ treeRoot .left .right .parent = treeRoot .left ;
68+ treeRoot .left .left .left = new TreeNode (0 );
69+ treeRoot .left .left .left .parent = treeRoot .left .left ;
70+ treeRoot .right .left = new TreeNode (6 );
71+ treeRoot .right .left .parent = treeRoot .right ;
72+ treeRoot .right .right = new TreeNode (9 );
73+ treeRoot .right .right .parent = treeRoot .right ;
74+ treeRoot .right .left .right = new TreeNode (7 );
75+ treeRoot .right .left .right .parent = treeRoot .right .left ;
76+
77+ System .out .println ("InOrder successor of 0 is: " + getInOrderSuccessor (treeRoot .left .left .left ).val );
78+ System .out .println ("InOrder successor of 1 is: " + getInOrderSuccessor (treeRoot .left .left ).val );
79+ System .out .println ("InOrder successor of 2 is: " + getInOrderSuccessor (treeRoot .left ).val );
80+ System .out .println ("InOrder successor of 3 is: " + getInOrderSuccessor (treeRoot .left .right ).val );
81+ System .out .println ("InOrder successor of 4 is: " + getInOrderSuccessor (treeRoot ).val );
82+ System .out .println ("InOrder successor of 6 is: " + getInOrderSuccessor (treeRoot .right .left ).val );
83+ System .out .println ("InOrder successor of 7 is: " + getInOrderSuccessor (treeRoot .right .left .right ).val );
84+ System .out .println ("InOrder successor of 8 is: " + getInOrderSuccessor (treeRoot .right ).val );
85+ System .out .println ("InOrder successor of 9 is: " + getInOrderSuccessor (treeRoot .right .right ));
86+ }
87+
88+ private static class TreeNode {
89+ int val ;
90+ TreeNode parent ;
91+ TreeNode left ;
92+ TreeNode right ;
93+
94+ TreeNode (int val ) {
95+ this .val = val ;
96+ }
1197 }
12- }
98+ }
0 commit comments