-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrgHierarchy.java
298 lines (260 loc) · 5.95 KB
/
OrgHierarchy.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import java.io.*;
import java.util.*;
// Tree node
class Node {
Node boss = null;
int Data;
int level;
Node manager;
Vector<Node> emp= new Vector<Node>();
public Node(int id,int l, Node boss){
this.Data=id;
this.level=l;
this.manager=boss;
}
public Node(int Data) {
this.Data = Data;
}
}
public class OrgHierarchy implements OrgHierarchyInterface{
//root node
Node root;
int id;
Vector<Node> v= new Vector<Node>();
public boolean isEmpty(){
if(root==null)
return true;
else{
return false;
}
}
public int size(){
return v.size();
}
/*public int size(){
return orgsize(root);
}
public int orgsize(Node r){
if(r.emp.size()==0){
return 0;
}
else{
int n=0;
while(i < r.emp.size()) {
n+=orgsize(r.emp.get(i));
}
return r.emp.size()+n;
}
}
*/
public int level(int id) throws IllegalIDException, EmptyTreeException{
if(isEmpty()==true){
throw new EmptyTreeException("First No level tree is empty");}
Node temp = findNode(id);
if(temp==null){
throw new IllegalIDException("Second ID not present");
}else{
return temp.level;
}
}
public void hireOwner(int id) throws NotEmptyException{
if(isEmpty()==false){
throw new NotEmptyException("Can't hire Owner if tree not Empty");
}
else{
root = new Node(id);
root.Data=id;
root.level=1;
root.manager=null;
v.add(root);
}
}
public void hireEmployee(int id, int bossid) throws IllegalIDException, EmptyTreeException{
if(isEmpty()==true) {
throw new EmptyTreeException("Fourth No tree No employee");}
Node temp = findNode(bossid);
if(temp==null){
throw new IllegalIDException("Fifth No ID present");
}
else{
Node node= new Node(id, temp.level+1, temp);
// temp.emp.add(node);
int j = 0;
while(j < temp.emp.size()) {
if(temp.emp.get(j).Data < id) {
j += 1;
}else {
break;
}
}
temp.emp.add(j,node);
// System.out.println("start== " + temp.emp.get(j).Data);
int i = 0;
while(i<v.size()) {
if(v.get(i).Data < id) {
i += 1;
}else {
break;
}
}
v.add(i,node);
// System.out.println("__start== " + v.get(i).Data);
}
}
private Node findNode(int bossid) {
int i = 0;
while(i < v.size()) {
if(v.get(i).Data == bossid) {
return v.get(i);
}else {
i += 1;
}
}
return null;
}
public void fireEmployee(int id) throws IllegalIDException,EmptyTreeException{
if(isEmpty()==true) {
throw new EmptyTreeException("Fourth No tree No employee");}
Node temp = findNode(id);
if(temp==null||temp.emp.size()!=0){
throw new IllegalIDException("Fifth No ID present");
}else {
Node manage=temp.manager;
manage.emp.remove(temp);
int i = 0;
while(i < v.size()) {
if(v.get(i).Data == id) {
v.remove(i);
}else {
i += 1;
}
}
}
}
public void fireEmployee(int id, int manageid) throws IllegalIDException,EmptyTreeException{
if(isEmpty()==true) {
throw new EmptyTreeException("Fourth No tree No employee");}
Node temp = findNode(id);
if(temp==null){
throw new IllegalIDException("Fifth No ID present");
}
else {
Node node= temp.manager;
int i=0;
while(i<node.emp.size()){
if(node.emp.get(i).Data==manageid){
int j=0;
while(j<temp.emp.size()){
temp.emp.get(j).manager=node.emp.get(i);
node.emp.get(i).emp.add(temp.emp.get(j));
j++;
}
//node.emp.get(i).emp=temp.emp;
break;
}
i++;
}
if(i>=node.emp.size()){
throw new IllegalIDException("Fifth No manageID present");
}
node.emp.remove(temp);
i = 0;
while(i < v.size()) {
if(v.get(i).Data == id) {
v.remove(i);
manageid = id;
}else {
i += 1;
}
}
}
}
public int boss(int id) throws IllegalIDException,EmptyTreeException{
if(isEmpty()==true)
throw new EmptyTreeException("Sixth No boss if tree is empty");
Node temp=findNode(id);
if(temp==null||temp.manager==null){
throw new IllegalIDException("Seventh No ID present");
}
else {
return temp.manager.Data;
}
}
public int lowestCommonBoss(int id1, int id2) throws IllegalIDException,EmptyTreeException{
if(isEmpty()==true){
throw new EmptyTreeException("Eight No lowestcommonboss tree is empty");
}
Node anode=findNode(id1);
Node bnode=findNode(id2);
if(anode==null||bnode==null){
throw new IllegalIDException("Ninth No Id available");
}
else{
if(anode.level>bnode.level){
while(anode.level!=bnode.level){
anode=anode.manager;
}
}
else {
while(anode.level!=bnode.level){
bnode=bnode.manager;
}
}
while(anode!=bnode){
anode=anode.manager;
bnode=bnode.manager;
}
return anode.Data;
}
}
public String toString(int id) throws IllegalIDException, EmptyTreeException{
String own = "";
if(isEmpty()) {
throw new EmptyTreeException("No tree No string");
}Node temp=findNode(id);
if(temp==null){
throw new IllegalIDException("ID not present can't convert to string");
}else{
own += temp.Data;
Vector<Integer> list= new Vector<Integer>();
Vector<Node> list_node1= new Vector<Node>();
for(int i=0; i< temp.emp.size(); i++) {
list.add(temp.emp.get(i).Data);
list_node1.add(temp.emp.get(i));
}
own += "," + list.get(0);
for(int i=1; i< list.size(); i++) {
own +=" " + list.get(i);
}
Vector<Node> list_node2 = new Vector<Node>();
while(!list_node1.isEmpty()){
list.clear();
for(int i=0; i< list_node1.size(); i++) {
for(int j=0; j< list_node1.get(i).emp.size(); j++) {
int k = 0;
while(k < list.size()) {
if(list.get(k) < list_node1.get(i).emp.get(j).Data) {
k += 1;
}else {
break;
}
}
list_node2.add(list_node1.get(i).emp.get(j));
list.add(k, list_node1.get(i).emp.get(j).Data);
}
}
if(!list.isEmpty()) {
own += "," + list.get(0);}
for(int i=1; i< list.size(); i++) {
own +=" " + list.get(i);
}
list_node1.clear();
for(Node n: list_node2) {
list_node1.add(n);
}
list_node2.clear();
}
}
return own;
}
}