-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuccessorDeleteSolution.java
191 lines (100 loc) · 3.25 KB
/
SuccessorDeleteSolution.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package union_find;
import java.util.Arrays;
/**Given an array S with N elements, write an algorithm with a performance of O(log N) which accomplishes the following:
*
* 1. deletion of element x and finds the successor of x such that the successor is equal or greater than x.
*/
public class SuccessorDeleteSolution {
int [] roots;
int [] actualList;
int [] sz;
int index;
int successor;
//constructor
SuccessorDeleteSolution(int [] array){
int length = array.length;
roots = new int[length]; //has the roots of the elements in array with x
actualList = new int[length]; // has the actualarray the one we have to remove x and find successor
sz = new int[length]; //monitors the growth of connected elements
/**initiate the arrays. Note actualList will be initiated with actual values of the array.
* roots has the roots starting from 0 to length -1.
* sz has the sizes initiated as 1.
*/
for(int i = 0; i<length;i++) {
roots[i] = i;
sz[i] = 1;
actualList[i] = array[i];
}
}
//method 0 - get index of the given number
public int getIndex(int x) {
index = 0;
while(actualList[index]!=x) {
index++;
}
return index;
}
//method 1 - get the root of x
public int root(int x) {
int i = getIndex(x); //root of x corresponds to index of x in the roots array.
while(i != roots[i]) { //chase the parents
roots[i] = roots[roots[i]];
i = roots[i];
}
return i; //root
}
//method 2 - quick union algorithm
public void union(int x, int y) {
int rootX = root(x);
int rootY = root(y);
int indexX = getIndex(x);
int indexY = getIndex(y);
if(sz[rootX]>sz[rootY]) {
roots[rootY] = rootX;
sz[rootX] += sz[rootY];
actualList[indexY] = actualList[indexX]; //if you changed root also delete x by changing it to the next number
successor = actualList[indexX]; // get the successor
}
else {
roots[rootX] = rootY;
sz[rootY]+=sz[rootX];
actualList[indexX] = actualList[indexY];
successor = actualList[indexY];
}
}
//method 3 - delete
public void delete(int x) {
//get index of x
int indexX = getIndex(x);
//next number
if((indexX+1)<actualList.length) {
int next_number = actualList[indexX+1];
union(x,next_number);
}
else {
throw new IndexOutOfBoundsException();
}
}
//method 4 - get the successor after deleting x
public int findSuccessor(int x) {
delete(x);
return successor;
}
//test the logic
public static void main(String[] args) {
int [] initialArray = {24,67,73,75,76,80,81,82,86,90};
SuccessorDeleteSolution sd = new SuccessorDeleteSolution(initialArray);
System.out.println(Arrays.toString(sd.actualList));
System.out.println(Arrays.toString(sd.roots));
System.out.println(Arrays.toString(sd.sz));
System.out.println(sd.getIndex(80));
System.out.println(sd.root(80));
System.out.println(sd.actualList[6]);
sd.delete(73);
sd.union(80, 81);
System.out.println(Arrays.toString(sd.actualList));
System.out.println(Arrays.toString(sd.roots));
System.out.println(Arrays.toString(sd.sz));
System.out.println("The successor will be : " + sd.findSuccessor(67));
}
}