-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlackTree.java
executable file
·1065 lines (980 loc) · 36.2 KB
/
RedBlackTree.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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.Scanner;
import java.lang.String;
/**
* The program build bank Of Customers ,the Name of Customer LastName ,id and
* How much money he wants to put in the bank account . The program Can return
* Who is the richest Customer in the bank ,How much money he have in bank
* account . The customer can also put money into the account and spend money
* from the account . The program Can return Who owes money to the bank and how
* much. The program Can Print all customer in Bank ,also The program Can Delete
* a customer or all customers at the bank .
* --------------------------------------------------------------------------------------------------------------------------------------------
* Algorithm use the red-black tree to Build Bank O(log n), Insert to tree O(log
* n) ,Delete customer O(log n),Search customer in tree O(log n) , FIX tree
* O(log n) ,Delete all tree o(1) Rotate left and right 0(1) . To get all the
* customer who have minus in bank We use linked list , Build linked list
* o(n),Search customer in linked list o(n) To get the rich customer in tree we
* use max-heap ,build max-heap o(n) Search for rich customer o(1).
* --------------------------------------------------------------------------------------------------------------------------------------------
*
* @author (Vladimir Geytman) (Year 2015)
*
*/
public class RedBlackTree { // Start of a program
private final int RED = 0; // color of Tree
private final int BLACK = 1;
/**
* Constructor of RedBlackTree
*
* @param key .
* @param color.
* @param sum of money.
* @param nam .
* @param LastName
*/
public class Node {
int key = -1, color = BLACK;
Node left = nil, right = nil, parent = nil;
int sumofmoney;
String name;
String LastName;
Node(String name, String LastName, int key, int sumofmoney) {
this.name = name;
this.LastName = LastName;
this.key = key;
this.sumofmoney = sumofmoney;
}
}
/**
* Constructor of Linked list
*
* @param key .
* @param sum of money.
* @param nam .
* @param LastName
*/
public class Nodemin {
int id;
int _data;
String name;
String LastName;
public Nodemin _next;
public Nodemin(String name, String LastName, int id, int _data) {
this.name = name;
this.LastName = LastName;
this.id = id;
this._data = _data;
_next = null; // next Linked list
}
}
/**
* Constructor of id in Tree Node
*
* @param id .
* @param gold.
*/
public static class TreeNode {
int data;
int idid;
TreeNode left;
TreeNode right;
TreeNode(int data, int idid) {
this.data = data;
this.idid = idid;
}
}
/**
* Constructor of Id in Tree Node for max
*
* @param id .
* @param gold.
*/
class Nodemax {
Nodemax next;
int num;
int id;
public Nodemax(int val, int id) {
num = val;
this.id = id;
next = null;
}
}
public Nodemax headmax; // head to max in tree node
public Nodemin _head; // head to Linked list
/**
* Constructor of head Linked list
*
* @param head
*/
public RedBlackTree() {
_head = null;
}
public boolean isEmpty() {
return (_head == null);
}
// Get Maximum element in binary search tree for id
public static TreeNode MaximumElement(TreeNode root) {
if (root.left == null)
return root;
else {
return MaximumElement(root.left);
}
}
// DeleteNode node of id !!
public static TreeNode deleteNode(TreeNode root, int value) {
if (root == null)
return null;
if (root.data < value) {
root.left = deleteNode(root.left, value);
} else if (root.data > value) {
root.right = deleteNode(root.right, value);
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
TreeNode temp = root;
// Finding minimum element from right
TreeNode minNodeForRight = MaximumElement(temp.right);
// Replacing current node with minimum node from right subtree
root.data = minNodeForRight.data;
// Deleting minimum node from right now
deleteNode(root.right, minNodeForRight.data);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
public static TreeNode insert(TreeNode root, TreeNode nodeToBeInserted) { // insert in node of Id for my id
if (root == null) {
root = nodeToBeInserted;
return root;
}
if (root.data < nodeToBeInserted.data) {
if (root.left == null)
root.left = nodeToBeInserted;
else
insert(root.left, nodeToBeInserted);
} else if (root.data > nodeToBeInserted.data)
if (root.right == null)
root.right = nodeToBeInserted;
else
insert(root.right, nodeToBeInserted);
return root;
}
public static void inOrder(TreeNode root) // print in inOrder for me
{
if (root == null)
return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
/**
* The method will add people to Linked list min .
*
* @param name
* @param LastName
* @param id
* @param data
*/
public void addElement(String name, String LastName, int id, int _data) {
if (isEmpty()) {
_head = new Nodemin(name, LastName, id, _data);
} else {
Nodemin newNode = new Nodemin(name, LastName, id, _data);
newNode._next = _head;
_head = newNode;
}
}
/**
* The method will remove people of Linked list min .
*/
public boolean removeElement(int id) {
if (isEmpty()) {
return false;
}
Nodemin runner = _head;
// If the data is in the head - we can just remove the head
if (runner.id == id) {
_head = _head._next;
return true;
}
// The data is not in the first node
else {
Nodemin prev = _head;
runner = _head._next;
while (runner != null) {
if (runner.id == id) {
// Now the previous node points on the one after
// the one we removed
prev._next = runner._next;
return true;
}
runner = runner._next;
prev = prev._next;
}
}
// The element we wanted to remove is not in list
return false;
}
private final Node nil = new Node("0", "0", -1, 0);
private Node root = nil;
/**
* The method will print Tree Red Black Treepeople of people in banke
*
* @param node
*/
public void printTree(Node node) {
if (node == nil) {
return;
}
printTree(node.left);
System.out.print(((node.color == RED) ? "Color: Red " : "Color: Black ") + "Key: " + node.key + " Parent: "
+ node.parent.key + "\n");
printTree(node.right);
}
/**
* The method boolean
*
* @param id
* @return false or true
*/
public boolean contains(int id) {
Nodemin runner = _head;
while (runner != null) {
if (runner.id == id) {
return true;
}
runner = runner._next;
}
return false;
}
/**
* The method find for Deleted
*
* @param id
* @param node
* @return sum of money
*/
private int findD(int id, Node node) {
if (root == nil) {
return 0;
}
if (id < node.key) {
if (node.left != nil) {
return findD(id, node.left);
}
} else if (id > node.key) {
if (node.right != nil) {
return findD(id, node.right);
}
} else if (id == node.key) {
if (node.sumofmoney < 0) {
System.out.println(" "); // Just for beauty
System.out.println("You must To put money in to bank Before you leave " + node.sumofmoney + " $");
System.out.println(" "); // Just for beauty
return node.sumofmoney;
}
if (node.sumofmoney > 0) {
System.out.println(" "); // Just for beauty
System.out.println("You must to Take all you'r money Before you leave " + node.sumofmoney + " $");
System.out.println(" "); // Just for beauty
return node.sumofmoney;
}
}
return 0;
}
/**
* The method find id in my node
*
* @param id
* @param node
*/
private Node onlifind(int id, Node node) {
if (root == nil) {
return null;
}
if (id < node.key) {
if (node.left != nil) {
return onlifind(id, node.left);
}
} else if (id > node.key) {
if (node.right != nil) {
return onlifind(id, node.right);
}
} else if (id == node.key) {
System.out.println("Several money on account : " + node.name + " " + node.LastName + " Num-Id " + node.key
+ " is->> ** " + node.sumofmoney + " **");
return node;
}
return null;
}
/**
* The method find id in my node for all RedBlackTree
*
* @param id
* @param node
* @return node
*/
private Node findid(int id, Node node) {
if (root == nil) {
return null;
}
if (id < node.key) {
if (node.left != nil) {
return findid(id, node.left);
}
} else if (id > node.key) {
if (node.right != nil) {
return findid(id, node.right);
}
} else if (id == node.key) {
return node;
}
return null;
}
/**
* The method will print Linked list of people in banke have min
*
*/
public void printList() {
Nodemin runner = _head;
while (runner != null) {
System.out.println("name " + runner.name + " gold " + runner._data + " di " + runner.id + " =>");
runner = runner._next;
}
System.out.print("null");
System.out.println(""); // Just for beauty
}
/**
* The method add sum to sum of money in for sam customer in RedBlackTree
*
* @param id
* @param shum
* @param node
*/
private Node addshum(int idd, int shum, Node node, TreeNode rootNode) {
if (root == nil) {
return null;
}
if (idd < node.key) {
if (node.left != nil) {
return addshum(idd, shum, node.left, rootNode);
}
} else if (idd > node.key) {
if (node.right != nil) {
return addshum(idd, shum, node.right, rootNode);
}
} else if (idd == node.key) {
node.sumofmoney = node.sumofmoney + (shum);
deleteNode(rootNode, idd);
System.out.println(" ");
TreeNode rootNode1 = new TreeNode(idd, node.sumofmoney);
insert(rootNode, rootNode1);
delete(idd);
insert(node.sumofmoney, idd);
System.out.println(" ");
System.out.print("The Account customer is : ");
System.out.println(node.name + " " + node.LastName + " " + node.key + " " + node.sumofmoney);
if (node.sumofmoney < 0) {
if (contains(node.key) == true) {
removeElement(node.key);
addElement(node.name, node.LastName, node.key, node.sumofmoney);
} else {
addElement(node.name, node.LastName, node.key, node.sumofmoney);
}
}
if (node.sumofmoney >= 0) {
if (contains(node.key) == true) {
removeElement(node.key);
}
}
if (shum > 0) {
System.out.println(" ");
System.out.println(
"The Customer " + node.name + " His account number is " + node.key + " put " + shum + " $");
System.out.println(" ");
}
if (shum <= 0) {
System.out.println(" ");
System.out.println(
"The Customer " + node.name + " His account number is " + node.key + " tak out " + shum + " $");
System.out.println(" ");
}
return node;
}
return null;
}
/**
* The method find Node for all RedBlackTree
*
* @param id
* @param Node
* @param Node
*/
private Node findNode(Node findNode, Node node) {
if (root == nil) {
return null;
}
if (findNode.key < node.key) {
if (node.left != nil) {
return findNode(findNode, node.left);
}
} else if (findNode.key > node.key) {
if (node.right != nil) {
return findNode(findNode, node.right);
}
} else if (findNode.key == node.key) {
return node;
}
return null;
}
/**
* The method insert Red Black Tree
*
* @param Node
*/
private void insert(Node node) {
Node temp = root;
if (root == nil) {
root = node;
node.color = BLACK;
node.parent = nil;
} else {
node.color = RED;
while (true) {
if (node.key < temp.key) {
if (temp.left == nil) {
temp.left = node;
node.parent = temp;
break;
} else {
temp = temp.left;
}
} else if (node.key >= temp.key) {
if (temp.right == nil) {
temp.right = node;
node.parent = temp;
break;
} else {
temp = temp.right;
}
}
}
fixTree(node);
}
}
/**
* The method fix Tree
*
* @param Node
*/
private void fixTree(Node node) { // Takes as argument the newly inserted node
while (node.parent.color == RED) {
Node uncle = nil;
if (node.parent == node.parent.parent.left) {
uncle = node.parent.parent.right;
if (uncle != nil && uncle.color == RED) {
node.parent.color = BLACK;
uncle.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
if (node == node.parent.right) {
// Double rotation needed
node = node.parent;
rotateLeft(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
// if the "else if" code hasn't executed, this
// is a case where we only need a single rotation
rotateRight(node.parent.parent);
} else {
uncle = node.parent.parent.left;
if (uncle != nil && uncle.color == RED) {
node.parent.color = BLACK;
uncle.color = BLACK;
node.parent.parent.color = RED;
node = node.parent.parent;
continue;
}
if (node == node.parent.left) {
// Double rotation needed
node = node.parent;
rotateRight(node);
}
node.parent.color = BLACK;
node.parent.parent.color = RED;
// if the "else if" code hasn't executed, this
// is a case where we only need a single rotation
rotateLeft(node.parent.parent);
}
}
root.color = BLACK;
}
/**
* The method fix Tree rotate Left
*
* @param Node
*/
void rotateLeft(Node node) {
if (node.parent != nil) {
if (node == node.parent.left) {
node.parent.left = node.right;
} else {
node.parent.right = node.right;
}
node.right.parent = node.parent;
node.parent = node.right;
if (node.right.left != nil) {
node.right.left.parent = node;
}
node.right = node.right.left;
node.parent.left = node;
} else { // Need to rotate root
Node right = root.right;
root.right = right.left;
right.left.parent = root;
root.parent = right;
right.left = root;
right.parent = nil;
root = right;
}
}
/**
* The method fix Tree rotateRight
*
* @param Node
*/
void rotateRight(Node node) {
if (node.parent != nil) {
if (node == node.parent.left) {
node.parent.left = node.left;
} else {
node.parent.right = node.left;
}
node.left.parent = node.parent;
node.parent = node.left;
if (node.left.right != nil) {
node.left.right.parent = node;
}
node.left = node.left.right;
node.parent.right = node;
} else { // Need to rotate root
Node left = root.left;
root.left = root.left.right;
left.right.parent = root;
root.parent = left;
left.right = root;
left.parent = nil;
root = left;
}
}
/**
* method for max tree
*
* @param val
* @param id
*/
public RedBlackTree(int val, int id) {
headmax = new Nodemax(val, id);
}
public void append(int val, int id) {
Nodemax tmpNode = headmax;
while (tmpNode.next != null) {
tmpNode = tmpNode.next;
}
tmpNode.next = new Nodemax(val, id);
}
/**
* insert to max tree
*
* @param val
* @param id
*/
public void insert(int val, int id) {
Nodemax currentNode = headmax;
Nodemax nextNode = headmax.next;
if (currentNode.num < val) {
Nodemax tmpNode = headmax;
headmax = new Nodemax(val, id);
headmax.next = tmpNode;
return;
}
if (nextNode != null && nextNode.num < val) {
currentNode.next = new Nodemax(val, id);
currentNode.next.next = nextNode;
return;
}
while (nextNode != null && nextNode.num > val) {
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = new Nodemax(val, id);
currentNode.next.next = nextNode;
}
/**
* Delete of max tree
*
* @param val
* @param id
*/
public void delete(int id) {
Nodemax prevNode = null;
Nodemax currNode = headmax;
if (headmax.id == id) {
headmax = headmax.next;
return;
}
while (currNode != null && currNode.id != id) {
prevNode = currNode;
currNode = currNode.next;
}
if (currNode == null) {
System.out.println("A node with that value does not exist.");
} else {
prevNode.next = currNode.next;
}
}
public void print()
// print node in max Tree
{
Nodemax tmpNode = headmax;
while (tmpNode != null) {
System.out.print(tmpNode.num + " , " + tmpNode.id + " -> ");
tmpNode = tmpNode.next;
}
System.out.print("null");
System.out.println(" ");
}
public void printmax() // //print the max in nood Tree
{
Nodemax tmpNode = headmax;
System.out.println(" ");
if (tmpNode == null) {
System.out.println("Sorry No max");
} else {
System.out.print("The richest Man in my bank is: " + tmpNode.id + " He have " + tmpNode.num + " $ ");
System.out.println(" ");
}
System.out.println(" ");
}
void deleteTree() { // Deletes whole tree
root = nil;
}
// Deletion Code .
// This operation doesn't care about the new Node's connections
// with previous node's left and right. The caller has to take care
// of that.
void transplant(Node target, Node with) {
if (target.parent == nil) {
root = with;
} else if (target == target.parent.left) {
target.parent.left = with;
} else
target.parent.right = with;
with.parent = target.parent;
}
/**
* The method Delete node in Tree
*
* @param Node
*/
boolean delete(Node z) {
if ((z = findNode(z, root)) == null)
return false;
Node x;
Node y = z; // temporary reference y
int y_original_color = y.color;
if (z.left == nil) {
x = z.right;
transplant(z, z.right);
} else if (z.right == nil) {
x = z.left;
transplant(z, z.left);
} else {
y = treeMinimum(z.right);
y_original_color = y.color;
x = y.right;
if (y.parent == z)
x.parent = y;
else {
transplant(y, y.right);
y.right = z.right;
y.right.parent = y;
}
transplant(z, y);
y.left = z.left;
y.left.parent = y;
y.color = z.color;
}
if (y_original_color == BLACK)
deleteFixup(x);
return true;
}
/**
* The method Delete fix in Tree
*
* @param Node
*/
void deleteFixup(Node x) {
while (x != root && x.color == BLACK) {
if (x == x.parent.left) {
Node w = x.parent.right;
if (w.color == RED) {
w.color = BLACK;
x.parent.color = RED;
rotateLeft(x.parent);
w = x.parent.right;
}
if (w.left.color == BLACK && w.right.color == BLACK) {
w.color = RED;
x = x.parent;
continue;
} else if (w.right.color == BLACK) {
w.left.color = BLACK;
w.color = RED;
rotateRight(w);
w = x.parent.right;
}
if (w.right.color == RED) {
w.color = x.parent.color;
x.parent.color = BLACK;
w.right.color = BLACK;
rotateLeft(x.parent);
x = root;
}
} else {
Node w = x.parent.left;
if (w.color == RED) {
w.color = BLACK;
x.parent.color = RED;
rotateRight(x.parent);
w = x.parent.left;
}
if (w.right.color == BLACK && w.left.color == BLACK) {
w.color = RED;
x = x.parent;
continue;
} else if (w.left.color == BLACK) {
w.right.color = BLACK;
w.color = RED;
rotateLeft(w);
w = x.parent.left;
}
if (w.left.color == RED) {
w.color = x.parent.color;
x.parent.color = BLACK;
w.left.color = BLACK;
rotateRight(x.parent);
x = root;
}
}
}
x.color = BLACK;
}
Node treeMinimum(Node subTreeRoot) { // tree Minimum
while (subTreeRoot.left != nil) {
subTreeRoot = subTreeRoot.left;
}
return subTreeRoot;
}
/********************************************************************************************************************************************/
/********************************************************************************************************************************************/
/** The Consol OF Program **/
/********************************************************************************************************************************************/
/********************************************************************************************************************************************/
/********************************************************************************************************************************************/
public void consoleUI() { // Start of a console
Scanner scan = new Scanner(System.in);
Scanner y = new Scanner(System.in);
Scanner l = new Scanner(System.in);
String str_y;
String str_l;
int id;
int shum;
int count = 0; // arr.length
Node node;
Nodemin head;
headmax = new Nodemax(0, 0);
TreeNode rootNode = new TreeNode(0, 0);
while (true) { // Press 1-8
System.out.println("**************************************************************************");
System.out.println(
"-If the client wants to put money in the account or take money Press 1 \n-New customer Press 2 \n-Delete customer (ID) Press 3");
System.out.println("-Print the rich customer at the bank Press 4 "); // Print max
System.out.println("-Checking the Balances in the customer's account (by id ) Press 5 ");
System.out.println("-Print all customer in Bank Press 6 ");
System.out.println("-Printing all customers With Negative balance in Bank Press 7 ");
System.out.println("- * Delete all tree all customer Press 8 * ");
System.out.println("**************************************************************************");
System.out.println(" "); // Just for beauty
int choice = scan.nextInt();
switch (choice) { // num choice = 1 of case .
case 1:
System.out.println(" "); // Just for beauty
System.out.println("Insert your ID");
id = scan.nextInt();
if ((findid(id, root)) == null) {
System.out.println("-------------------------");
System.out.println("-Id Does not exist sorry ");
System.out.println("-------------------------");
System.out.println(" "); // Just for beauty
break;
}
System.out.println(" ");
System.out.println("If the client wants to spend money from the account (Add a minus(-) before )");
System.out
.println("if you want to put money to account Enter the desired amount of money (only int()) ");
shum = scan.nextInt();
System.out.println(" "); // Just for beauty
addshum(id, shum, root, rootNode);
break;
case 2:
System.out.println(" "); // Just for beauty
System.out.println("Insert a new customer ");
System.out.println("Insert name ");
str_y = y.nextLine();
System.out.println("Insert LastName ");
str_l = l.nextLine();
System.out.println("Insert id --> (id >1000000) and (id <9999999)");
id = scan.nextInt();
while ((id < 1000000) || (id > 9999999)) // you'r id need be > 1000000 and < 9999999
{
System.out.println("Insert Correct id!! (id >1000000) and (id <9999999)");
id = scan.nextInt();
}
System.out.println("Enter the desired amount of money , money need to be >=0 ");
shum = scan.nextInt();
while (shum < (0)) // money > 0
{
System.out.println("Enter the desired amount of money , money need to be >=0");
shum = scan.nextInt();
}
node = new Node(str_y, str_l, id, shum);
if ((findNode(node, root)) == null) {
insert(node);
count = count + 1;
System.out.println(" "); // Just for beauty
System.out.print("+ New Client is Added: ");
System.out.println(str_y + " " + str_l + " " + id + " " + id + " " + shum); // Customer number equal
// to the number of his
// identity
printTree(root);
System.out.println(" "); // Just for beauty
insert(shum, id);
if (count == 0) {
TreeNode rootNode1 = new TreeNode(id, shum);
insert(null, rootNode);
}
else {
TreeNode rootNode1 = new TreeNode(id, shum);
insert(rootNode, rootNode1);
}
System.out.println(" ");
break;
}
else // id not exists
{
System.out.println("Id is already exists !! sorry ");
System.out.println(" "); // Just for beauty
break;
}
case 3:
System.out.println("Enter the ID of the customer who wants to leave : ");
id = scan.nextInt();
node = new Node("0", "0", id, 0);
if ((findNode(node, root)) == null) { // id not exists
System.out.println(" "); // Just for beauty
System.out.print("this ID not exist ! Sorry ! ");
System.out.println(" "); // Just for beauty
break;
} else {
while ((findD(id, root)) != 0) // while you dont have 0 in banke
{
System.out.println(" "); // Just for beauty
System.out.println("if you want to put money to account Enter the desired amount of money ");
System.out.println("if you wants to spend money from the account (Add a minus(-) before ) ");
System.out.println(" "); // Just for beauty
shum = scan.nextInt();
System.out.println(" "); // Just for beauty
addshum(id, shum, root, rootNode);
}
if (delete(node)) // delete nude in tree
{
System.out.print("- " + id + " The id ( Account) is delete... Goodbye :( ");
System.out.println(" ");
deleteNode(rootNode, id);
System.out.println(" ");
System.out.println(" "); // Just for beauty
System.out.println(" "); // Just for beauty
count = count - 1;