-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJobScheduler.java
860 lines (793 loc) · 32.5 KB
/
JobScheduler.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
import java.util.*;
import java.io.*;
/*
Serin Evcim
211401015
*/
//*******************************************************CLASS JOBSCHEDULAR ********************************************************/
public class JobScheduler {
/* instances:
* timer :keeps the time
* jobFilePath :file path for the file which includes information about jobs
* reader :file reader to read jobFilePath
* schedularTree :Node based minHeap, keeps jobs according to their arrival time
* jobs :all jobs that jobFilePath icludes, i used it in dependencyBlockedJobs() and resourceBlockedJobs(), set it in insertJob()
* completedJobs :keeps completed jobs, set it at the beggining of run
* allTimeLineOutput:every element of this arrayList keeps an array whose length is resourceCount, every element's index is current timer,
* these arrays save the jobs that currently working at i'th resource to i'th index, set it in the end of run
* dependencyBlocked:keeps the dependent jobs temporarily while inserting jobs to resources
* resources :keeps resources
* resourceCount :count of resources
* dependencyMap :maps the job id to the arrayList which stores the id's of jobs that create dependency for the job
*/
public Integer timer = 0;
public String jobFilePath;
Scanner reader;
public MinHeap<Job> schedulerTree;
ArrayList<Job> jobs;
ArrayList<Job> completedJobs;
ArrayList<Job[]> allTimeLineOutput;
ArrayList<Job> dependencyBlocked;
ArrayList<Resource> resources;
int resourceCount;
HashMap<Integer, ArrayList<Integer>> dependencyMap;
//----------------------------------------CONSTRUCTOR--------------------------------------------
public JobScheduler(String jfp) {
jobFilePath = jfp;
try{
reader = new Scanner(new File(jobFilePath));
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
schedulerTree = new MinHeap<>();
jobs = new ArrayList<>();
completedJobs = new ArrayList<>();
allTimeLineOutput = new ArrayList<>();
dependencyBlocked = new ArrayList<>();
dependencyMap = new HashMap<>();
}
//---------------------------------------GIVEN METHODS--------------------------------------------
/* reads given file line by line and stores the integer in splittedInt respectively
* hashMap consists of an integer as key(depended job's id) and an value arraylsist(job ids that create dependency)
* stores these integers to hashMap, first element is key and the other is an element of arraylist
* if key exists in hashMap then value is added to the arraylist of that key
*/
public void insertDependencies(String dependencyPath){
File file = new File(dependencyPath);
String[] splitted = new String[2];
int[] splittedInt = new int[2];
try{
Scanner fileReader = new Scanner(file);
while(fileReader.hasNextLine()){
splitted = (fileReader.nextLine()).split(" ", 2);
for(int i=0; i<2; i++){
splittedInt[i] = Integer.parseInt(splitted[i]);
}
if(dependencyMap.containsKey(splittedInt[0])){
dependencyMap.get(splittedInt[0]).add(splittedInt[1]);
}else{
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(splittedInt[1]);
dependencyMap.put(splittedInt[0], arrayList);
}
}
fileReader.close();
}catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
/* checks the jobFilePath, if the reader has next line returbs true otherwise false
*/
public boolean stillContinues(){
if(reader.hasNextLine())
return true;
return false;
}
/* first, checks the resources, if there is a finished job saves it to an arrayList and removes it from resource
* then, inserts a job from schedularTree to the resources
* finally, save the currently working jobs to allTimeLineOutput
*/
public void run(){
// checking completed jobs and saving them to arraylist
for(int i=0; i<resourceCount; i++){
if(resources.get(i).currentJob!= null){
if(resources.get(i).currentJob.getDuration()==0){
completedJobs.add(resources.get(i).currentJob);
resources.get(i).currentJob = null;
}
}
}
// inserting jobs to resources or decreasing durations of working jobs
// when the duration is 0 job is completed and its checken above
for(int i=0; i<resourceCount; i++){
if(resources.get(i).currentJob == null){ //then we will insert the min of schedularTree
if(schedulerTree.root!=null){
while(isDependend(schedulerTree.min())){
/* if root(min) of scheduarTree is dependent we will save it to an arrayList,
* remove it from schedularTree and
* try to insert the new root (recheck if it is dependent either - loop)
*/
dependencyBlocked.add(schedulerTree.remove());
}
}
// the root of schedularTree is no longer dependent so we inserted it
resources.get(i).currentJob = schedulerTree.remove();
// reinsert the jobs we saved to arrayList above
for(int j=0; j<dependencyBlocked.size();j++){
schedulerTree.add(dependencyBlocked.get(j));
}
dependencyBlocked = new ArrayList<>(); //for resetting the arraylist
}else{//there is a job working already so we decrease the duration
resources.get(i).currentJob.setDuration(resources.get(i).currentJob.getDuration() - 1);
}
}
//setting allTimeLineOutput
Job[] currentlyWorkingJobs = new Job[resourceCount];
for(int i=0; i<resourceCount; i++)
currentlyWorkingJobs[i] = resources.get(i).currentJob;
allTimeLineOutput.add(currentlyWorkingJobs);
}
/* sets the resourceCount by count and,
* creates 'resources' arraylist according to given resourceCount
*/
public void setResourcesCount(Integer count){
Resource newResource;
resources = new ArrayList<>();
for(int i=0; i<count; i++){
newResource = new Resource(i);
resources.add(newResource);
}
resources.trimToSize();
resourceCount = count;
}
/* if jobFilePath's line includes information about a job, reads it and creates a job
* then, inserts the job to the arrayList 'jobs' and schedularTree
* increases timer
*/
public void insertJob(){
String line = reader.nextLine();
if(!line.equals("no job")){
String[] splitted = line.split(" ", 2);
Job newJob = new Job(timer, Integer.parseInt(splitted[0]), Integer.parseInt(splitted[1])-1);
jobs.add(newJob);
schedulerTree.add(newJob);
}
timer++;
}
/* prints the completed jobs by looking at the arrayList completedJobs
*/
public void completedJobs(){
System.out.print("completed jobs: ");
for(int k=0; k<completedJobs.size(); k++){
if(k!=0) System.out.print(",");
System.out.print(completedJobs.get(k));
}
System.out.println("\n");
}
/* prints the dependency blocked jobs by looking at the arrayList 'jobs'
* if a job from this arrayList arrived, inserted to the schedularTree and dependent then, prints
*/
public void dependencyBlockedJobs(){
System.out.print("dependency blocked jobs: ");
for(int k=0; k<jobs.size(); k++){
if((jobs.get(k).getArrivalTime() <= timer) && (schedulerTree.jobSet.contains(jobs.get(k))) && (isDependend(jobs.get(k))))
if(dependencyMap.containsKey(jobs.get(k).getId()))
System.out.print("(" + jobs.get(k) + "," + dependencyMap.get(jobs.get(k).getId()).get(stopper(dependencyMap.get(jobs.get(k).getId()))) + ")");
}
System.out.println("\n");
}
/* prints the resource blocked jobs by looking at thr arrayList 'jobs'
* if a job from this arrayList arrived, inserted to the schedularTree but not depended then, prints
*/
public void resourceBlockedJobs(){
boolean printedTheFirstResourceBlocked = false; // for making a nice looking when using ','
System.out.print("resource blocked jobs: ");
for(int k=0; k<jobs.size(); k++){
if((jobs.get(k).getArrivalTime() <= timer) && (schedulerTree.jobSet.contains(jobs.get(k))) && (!isDependend(jobs.get(k)))){
if(printedTheFirstResourceBlocked) System.out.print(",");
System.out.print(jobs.get(k));
printedTheFirstResourceBlocked = true;
}
}
System.out.println("\n");
}
/* printd the working jobs by looking at the arrayList 'resources'
* reaches the currentJob' of resources and prints
*/
public void workingJobs(){
System.out.print("working jobs: ");
for( int i=0; i<resourceCount; i++){
if(resources.get(i).currentJob != null)
System.out.print("(" + resources.get(i).currentJob.getId() + "," + (resources.get(i).resourceId+1) + ")");
}
System.out.println();
}
/* calls run as long as there are jobs remaining in the schedularTree or in resources
* increases the timer
*/
public void runAllRemaining(){
while((schedulerTree.size != 0) || !areAllResourcesEmpty()){
this.run();
timer++;
}
}
/* printd the all time line by looking at the arrayList 'allTimeLineOutput'
*/
public void allTimeLine(){
for(int i=0; i<resourceCount; i++){
System.out.print(" R" + (i+1) + " ");
}
System.out.println();
for(int i=0; i<allTimeLineOutput.size(); i++){
if(allTimeLineOutput.get(i)!= null){
System.out.print("" + i);
for(int k=0; k<resourceCount; k++){
System.out.print(" " + allTimeLineOutput.get(i)[k] + " ");
}
System.out.println();
}
}
}
/* calls the toString method of MinHeap */
public String toString(){
return schedulerTree.toString();
}
//--------------------------------METHODS THAT I ADDED - PRİVATE METHODS----------------------------------
/* returns true if the job given as parameter is depended, false otherwise
* called by run(), dependencyBlockedJobs() and resourceBlockedJobs()
*/
private boolean isDependend(Job j){
if(j==null) return false;
if(j.getArrivalTime()<timer){
if(dependencyMap.containsKey(j.getId())){
if(isAllStopperJobsFinished(dependencyMap.get(j.getId()))){
return false;
}
return true;
}
return false;
}
return true;
}
/* returnt true if all of the jobs in arrayList given as parameter (which contains job id's that create dependency) are completed, false otherwise
* called by isDependent()
*/
private boolean isAllStopperJobsFinished(ArrayList<Integer> j){
boolean flag = true;
if(j!= null){
for(int k=0; k<j.size(); k++){
if(j.get(k)!= null){
if(!isCompleted(j.get(k))){
flag = false;
}
}
}
}
return flag;
}
/* returns true if the job which has the id given as paremeter is completed, false otherwise
* called by stopper()
*/
private boolean isCompleted(int k){
for( int i=0; i<completedJobs.size(); i++){
if(completedJobs.get(i).getId() == k)
return true;
}
return false;
}
/* returns the index of the stopper job (the job that creates dependency) in the arrayList given as parameter,
* returns 0 if arraylists size is 0
* called by dependencyBlockedJobs()
*/
private int stopper(ArrayList<Integer> dep){
for(int i=0; i<dep.size(); i++){
if(!isCompleted(dep.get(i)))
return i;
}
return 0;
}
/*returns true if all resources are empty, false otherwise
* called by runAllRemaining()
*/
private boolean areAllResourcesEmpty(){
for(int i=0; i<resourceCount; i++){
if(resources.get(i)!= null){
if(resources.get(i).currentJob != null){
return false;
}
}
}
return true;
}
//----------------------------------------------------INNER CLASSES---------------------------------------------------
public class Resource {
/* currentJob :stores the inserted job
* resourceId :resource's id
*/
Job currentJob;
int resourceId;
//--------CONSTRUCTOR
public Resource(int id){
resourceId = id;
currentJob = null;
}
}
public class Job implements Comparable<Job>{
/* arrivalTime :the line that job has read from
* id :job's id
* duration :the amount of time that job must stay in a resource
*/
private int arrivalTime;
private int id;
private int duration;
//--------CONSTRUCTOR
public Job(int a, int i, int d){
arrivalTime = a;
id = i;
duration = d;
}
//--------METHODS
//if a swap is needed in minHeapify returns 1 else 0
public int compareTo(Job o){ // 'this' will be the LastNode at the first call
if(this.arrivalTime < o.arrivalTime)
return 1;
return 0;
}
//returns the id as string
public String toString(){
return String.valueOf(id);
}
// returns true if this and object given as parameter has the same id, false otherwise
public boolean equals(Object o){
Job j = (Job) o;
if(this.id == j.id)
return true;
return false;
}
//--------GETTERS AND SETTERS
public int getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
public class MinHeap<E extends Comparable<E>> {
/* root :root of the tree
* LastAdded :the node en of the tree
* size :number of the nodes tree has
* jobSet :jobs that tree includes currently
*/
Node<E> root;
Node<E> LastAdded;
int size;
ArrayList<Job> jobSet;
//--------CONSTRUCTOR
MinHeap(){
root = new Node<E>();
LastAdded = root;
size = 0;
jobSet = new ArrayList<>();
}
//--------METHODS
//returns the root since this is a minHeap
E min(){
if(size==0) return null;
return this.root.getElement();
}
// adds a job which has the element given as parameter to the tree(further explanation is inside the method)
void add(E e){
if(size == 0){
Node<E> newn = new Node<>(e);
root = newn;
jobSet.add((Job)e);
LastAdded = root;
size++;
}
else if(size == 1){
if(!root.hasLeftClild()){ //add left child if it does not exists, add right child otherwise
root.addLeftChild(e);
root.leftChild.setParent(root);
jobSet.add((Job)e);
LastAdded = root.leftChild;
size++;
}else if(!root.hasRightClild()){
root.addRightChild(e);
root.rightChild.setParent(root);
jobSet.add((Job)e);
LastAdded = root.rightChild;
size++;
}
}else{
Node<E> ptr = LastAdded.parent; //LastAdded is a leaf, get its parent
if(!ptr.hasLeftClild()){ //add left child if it does not exists, add right child otherwise
ptr.addLeftChild(e);
ptr.leftChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.leftChild;
size++;
}else if(!ptr.hasRightClild()){
ptr.addRightChild(e);
ptr.rightChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.rightChild;
size++;
}else{
/* go up until came on a left child
* if we have reached the root that means root's right side subtree is full
* then we are going start filling new line aka: height of tree increases 1
* so we go left until we reach a leaf then we can add a new node
* if we didnt reached the roof that means the subtree (which has current node as its top node) is full
* so we go the sibling of current node and start to fill there
*/
while(!ptr.isRightChild()){
ptr = ptr.parent;
}
if(ptr == root){ // NEEDS CHECKİNG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
while(ptr.isLeaf()){
ptr = ptr.leftChild;
}
if(!ptr.hasLeftClild()){
ptr.addLeftChild(e);
ptr.leftChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.leftChild;
size++;
}else if(!ptr.hasRightClild()){
ptr.addRightChild(e);
ptr.rightChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.rightChild;
size++;
}
}else{
ptr = ptr.parent;
ptr = ptr.rightChild;
if(ptr.isLeaf()){
if(!ptr.hasLeftClild()){
ptr.addLeftChild(e);
ptr.leftChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.leftChild;
size++;
}else if(!ptr.hasRightClild()){
ptr.addRightChild(e);
ptr.rightChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.rightChild;
size++;
}
}else{
while(ptr.isLeaf()){
ptr = ptr.leftChild;
}
if(!ptr.hasLeftClild()){
ptr.addLeftChild(e);
ptr.leftChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.leftChild;
size++;
}else if(!ptr.hasRightClild()){
ptr.addRightChild(e);
ptr.rightChild.setParent(ptr);
jobSet.add((Job)e);
LastAdded = ptr.rightChild;
size++;
}
}
}
}
}
//upheap
minHeapify(LastAdded);
}
// removes the root - min
E remove(){
E removed = null;
if(size == 0){
return removed;
}else if(size == 1){
removed = root.element;
root = null;
jobSet.remove(jobSet.indexOf(removed));
size--;
}
else{
removed = root.element;
root.element = LastAdded.element;
if(LastAdded.isRightChild()){
LastAdded.parent.rightChild = null;
jobSet.remove(jobSet.indexOf(removed));
LastAdded = this.getLastAdded();
size--;
}else{
LastAdded.parent.leftChild = null;
jobSet.remove(jobSet.indexOf(removed));
LastAdded = this.getLastAdded();
size--;
}
}
//downheap
if(root != null)
minHeapifyFromRoof(root);
return removed;
}
//works as upHeap()
private void minHeapify(Node<E> n){ // n wil be LastAdded for the first time it called
if(n != root){
if(n.element.compareTo(n.parent.element) == 1){
E temp = n.parent.element;
n.parent.element = n.element;
n.element = temp;
minHeapify(n.parent);
}
}
}
//works as downHeap()
private void minHeapifyFromRoof(Node<E> n){ //n will be the roof when it first called
if(n.leftChild != null && n.rightChild != null){
if(n.leftChild.element.compareTo(n.rightChild.element)==1 && n.leftChild.element.compareTo(n.element)==1){
E temp = n.leftChild.element;
n.leftChild.element = n.element;
n.element = temp;
minHeapifyFromRoof(n.leftChild);
}
else if(n.rightChild.element.compareTo(n.leftChild.element)==1 && n.rightChild.element.compareTo(n.element)==1){
E temp = n.rightChild.element;
n.rightChild.element = n.element;
n.element = temp;
minHeapifyFromRoof(n.rightChild);
}
}
else if(n.leftChild == null && n.rightChild != null && n.rightChild.element.compareTo(n.element) == 1){
E temp = n.rightChild.element;
n.rightChild.element = n.element;
n.element = temp;
minHeapifyFromRoof(n.rightChild);
}
else if(n.rightChild == null && n.leftChild != null &&n.leftChild.element.compareTo(n.element) == 1){
E temp = n.leftChild.element;
n.leftChild.element = n.element;
n.element = temp;
minHeapifyFromRoof(n.leftChild);
}
}
//returns a string for current tree (further explanation is inside the method)
public String toString(){
ArrayList<Node<Job>> treesLine = new ArrayList<>(1);
ArrayList<Node<Job>> tempTreesLine;
/* these arrayLists are the lines of our tree to print it line by line,
* first treesLine only contains the root: h=1 and do the appending
* then we set tempTreesLine, it contains treesLine's child nodes: h=2
* now, we can assign tempTreesLine to the treesLine and do appending in loop
*/
StringBuilder str = new StringBuilder();
treesLine.add((Node<Job>)this.root);
str.append(" ");
if(treesLine.get(0) != null)
str.append(treesLine.get(0).element.getId());
str.append("\n");
if(treesLine.get(0)!= null){
while(treesLine.get(0).leftChild!= null){
tempTreesLine = new ArrayList<>(treesLine.size()*2);
for(int i=0; i<treesLine.size(); i++){
tempTreesLine.add(treesLine.get(i).leftChild);
tempTreesLine.add(treesLine.get(i).rightChild);
}
treesLine = tempTreesLine;
for(int i=0; i<treesLine.size(); i++){
if(treesLine.get(i) != null)
str.append(treesLine.get(i).element.getId());
str.append(" ");
}
str.append("\n");
}
}
return str.toString();
}
/* i will give an example to explain the algorithm i created here:
* lets assume we have a tree that has 26 nodes it looks like this:
* node amount in line total node count until line
* 0 2^0 = 1 1 = 2^1 -1
* o 0 2^1 = 2 3 = 2^2 -1
* o o 0 o 2^2 = 4 7 = 2^3 -1
* o o o o o 0 o o 2^3 = 8 15 = 2^4 -1
* o o o o o o o o o o 0 11 (max: 2^4 = 16) 11 (max: 2^5 -1)
*
* now, using the information i wrote above lets find the last node strarting from root:
* i used ' and '' as flags
* size:26 --> the biggest 2^k is:2^4
* 26 = (2^4-1) + 11' = 15 + 11' --> 11'>2^3 GO RIGHT (recalculate the size by this condition)
* new size is: 11'-1 = 10
* size:10 --> the biggest 2^k is:2^3
* 10 = (2^3-1) + 3 = 7' + 3'' --> 3''<=2^2 GO LEFT
* new size is: [(7'-1)/2]+3'' = 6
* size:6 --> the biggest 2^k is:2^2
* 6 = (2^2-1) + 3 = 3 + 3' --> 3'>2^1 GO RIGHT
* new size is: 3'-1 = 2
* size:2 --> the biggest 2^k is:2^1
* 2 = (2^1-1) + 1 = 1' + 1'' --> 1''<=2^0 GO LEFT
* nwe size is: [(1'-1)/2]+1'' = 1
* we found the last node!
*/
public Node<E> getLastAdded(){
Node<E> walker = (Node<E>)root;
int sizeT = this.size;
int iterator = (int)Math.log(sizeT);
int leftTo = -1;
int rightTo = -1;
for(int i = iterator ; i > 0 ; i--){
leftTo = (int)Math.pow(2, i) - 1;
rightTo = (int)(sizeT - Math.pow(2, i) - 1);
if(rightTo <= (int)Math.pow(2,iterator-1)){
walker = walker.leftChild;
sizeT = ((leftTo-1)/2) + rightTo;
}
else{
walker = walker.rightChild;
sizeT = rightTo-1;
}
}
return walker;
}
//--------INNER CLASS NODE
public class Node<E>{
private Node<E> rightChild;
private Node<E> leftChild;
private Node<E> parent;
private E element; //Job
//--------CONSTRUCTORS
Node(){
rightChild = null;
leftChild = null;
parent = null;
element = null;
}
Node(E elmnt){
rightChild = null;
leftChild = null;
parent = null;
element = elmnt;
}
//--------METHODS
void addRightChild(E e){
Node<E> n = new Node<E>(e);
rightChild = n;
n.setParent(this);
}
void addLeftChild(E e){
Node<E> n = new Node<E>(e);
leftChild = n;
n.setParent(this);
}
boolean hasRightClild(){
return ((rightChild==null) ? false:true);
}
boolean hasLeftClild(){
return ((leftChild==null) ? false:true);
}
boolean isRightChild(){
if(this == root) return false;
Node<E> p = this.parent;
if(p.rightChild != null){
if(p.rightChild == this)
return true;
return false;
}
return false;
}
private boolean isLeaf(){
if((rightChild == null) && (leftChild == null))
return true;
return false;
}
//--------GETTERS AND SETTERS
public Node<E> getRightChild() {
return rightChild;
}
public void setRightChild(Node<E> rightChild) {
this.rightChild = rightChild;
}
public Node<E> getLeftChild() {
return leftChild;
}
public void setLeftChild(Node<E> leftChild) {
this.leftChild = leftChild;
}
public Node<E> getParent() {
return parent;
}
public void setParent(Node<E> parent) {
this.parent = parent;
}
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
}
}
public class ArrayList<K>{
private int defaultLength = 10;
private int size;
private transient K[] data;
public ArrayList(){
data = (K[]) new Object[defaultLength];
}
public ArrayList(int length){
data = (K[]) new Object[defaultLength];
defaultLength = length;
}
//METHODS:
public void trimToSize(){
if (size != data.length){
K[] newData = (K[]) new Object[size];
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public K get(int index){
checkBoundExclusive(index);
return data[index];
}
public K set(int index, K e)
{
checkBoundExclusive(index);
K result = data[index];
data[index] = e;
return result;
}
public boolean add(K e){
if(size==data.length){
K[] newData = (K[]) new Object[defaultLength*2];
defaultLength *= 2;
System.arraycopy(data, 0, newData, 0, size);
data = newData;
data[size] = e;
}
else data[size] = e;
size++;
return true;
}
public K remove(int index){
checkBoundExclusive(index);
K r = data[index];
if (index != --size)
System.arraycopy(data, index + 1, data, index, size - index);
data[size] = null;
return r;
}
public int indexOf(Object e){
for (int i = 0; i < size; i++)
if (e.equals(data[i]))
return i;
return -1;
}
public boolean contains(Object e){
return indexOf(e) != -1;
}
private void checkBoundExclusive(int index) {
if (index >= size)
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
}