-
Notifications
You must be signed in to change notification settings - Fork 688
/
InOrderSuccessor.java
158 lines (126 loc) · 4.4 KB
/
InOrderSuccessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package trees;
import java.util.ArrayList;
import java.util.List;
public class InOrderSuccessor {
/*
* Inorder successor of a node in binary Search Tree (BST) is the next node in inorder traversal.
* Write a method to find inorder successor of a given value "d" in a BST.
* Binary Node: 100, 50, 200, 25, 75, 125, 350
*
* Inorder successor of 25 is 50
* Inorder successor of 50 is 75
* Inorder successor of 75 is 100
* Inorder successor of 100 is 125
* Inorder successor of 125 is 200
* Inorder successor of 200 is 350
* Inorder successor of 350 is NULL since it is the last node
*
* Runtime Complexity:
* Logarithmic, O(logn)
*
* Memory Complexity:
* Constant, O(1).
*
* Find the value d in BST. If d has a right child then the left most child in right child's subtree
* will be the in-order successor of d. This would also be the child with the minimum value in that subtree.
* Find the value d in BST. If d has no right child then:
* in-order successor is NULL if d is right most node in the BST i.e. last node in the in-order traversal
* in-order successor is the node with minimum value higher than d in the parent chain of d
*
* */
private static class Node {
private int data;
private Node left, right;
Node(int item) {
data = item;
left = right = null;
}
public int getData() {
return data;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}
private Node root1;
private static Node findMin(Node root) {
if (root == null) return null;
while (root.left != null) {
root = root.left;
}
return root;
}
protected static Node inOrderSuccessorBst(Node root,
int d) {
if (root == null) {
return null;
}
Node successor = null;
while (root != null) {
if (root.data < d) {
root = root.right;
} else if (root.data > d) {
successor = root;
root = root.left;
} else {
if (root.right != null) {
successor = findMin(root.right);
}
break;
}
}
return successor;
}
protected List<Integer> values = new ArrayList<>();
protected List<Integer> storeKeyValues(Node root) {
treeTravel(root);
return values;
}
private void treeTravel(Node node) {
if (node != null) {
treeTravel(node.left);
values.add(node.data);
treeTravel(node.right);
}
}
public static void main(String[] args) {
ArrayList<Integer> orig_data = new ArrayList<>();
orig_data.add(25);
orig_data.add(125);
orig_data.add(200);
orig_data.add(350);
orig_data.add(50);
orig_data.add(75);
orig_data.add(100);
InOrderSuccessor inOrderSuccessor = new InOrderSuccessor();
inOrderSuccessor.root1 = new Node(100);
inOrderSuccessor.root1.left = new Node(50);
inOrderSuccessor.root1.right = new Node(200);
inOrderSuccessor.root1.left.left = new Node(25);
inOrderSuccessor.root1.left.right = new Node(75);
inOrderSuccessor.root1.right.left = new Node(125);
inOrderSuccessor.root1.right.right = new Node(350);
List<Integer> bstToList = inOrderSuccessor.storeKeyValues(inOrderSuccessor.root1);
for (Integer d : orig_data) {
Node successor = inOrderSuccessorBst(inOrderSuccessor.root1, d);
int i = bstToList.indexOf(d);
Integer expected_val = null;
if (i < bstToList.size() - 1) {
expected_val = bstToList.get(i + 1);
}
if (successor != null) {
if (expected_val.intValue() != successor.data) {
System.out.println("*******" + d + " ==== " + expected_val + ", " + successor.data + "*****");
}
}
if (successor != null) {
System.out.print("(" + d + ", " + successor.data + ") ");
} else {
System.out.print("(" + d + ", null) ");
}
}
}
}