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
| package tree_final;
public class BinaryTree {
private Node root;
public BinaryTree()
{ root = null; }
private void PrintRec(Node root){
if(root != null)
return;
root.Print();
System.out.println();
PrintRec(root.left);
PrintRec(root.right);
}
public void Print(){
PrintRec(root);
return;
}
public void insert(int data, String name, String surname,
String course, String ticket, int day, int month, int year ){
Node n = new Node(data, name, surname, course, ticket, day, month, year);
if (root == null){
root = n;
}
else {
Node current = root;
Node parent;
while(true) {
parent = current;
if(data < current.data){
current = current.left;
}
if (current == null) {
parent.left = n;
return;
}
else {
current = current.right;
if(current == null){
parent.right = n;
return;
}
}
}
}
}
public Node find(int key){
Node current = root;
while(current.data != key){
if(key < current.data){
current = current.left;
}
else {
current = current.right;
}
if(current == null) {
return null;
}
}
return current;
}
public void inorderPrint(Node root) {
if (root == null) {
inorderPrint(root.left);
System.out.println(root.data + "");
inorderPrint(root.right);
}
}
public boolean delete(int key) {
Node current = root;
Node parent = root;
boolean isLeft = true;
if (current.course == "3" && (current.month>=6 && current.month<=8)) {
while(current.data != key) {
parent = current;
if(key < current.data) {
isLeft = true;
current = current.left;
}
else {
if(current == null) {
return false;
}
}
}
if(current.left == null && current.right == null) {
if (current == root)
root = null;
else if(isLeft)
parent.left = null;
else
parent.right = null;
}
else if (current.right == null)
if( current == root)
root = current.left;
else if(isLeft)
parent.left = current.left;
else
parent.right = current.right;
else if(current.left == null)
if(current == root)
root = current.right;
else if(isLeft)
parent.left = current.right;
else
parent.right = current.right;
else {
Node successor = getSuccessor(current);
if(current == root)
root = successor;
else if(isLeft)
parent.left = successor;
else
parent.right = successor;
successor.left = current.left;
}
}
return true;
}
private Node getSuccessor(Node delNode)
{
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.right;
while(current != null)
{
successorParent = successor;
successor = current;
current = current.left;
}
if(successor != delNode.right)
{
successorParent.left = successor.right;
successor.right = delNode.right;
}
return successor;
}
public static void main(String[] args ) {
BinaryTree theTree = new BinaryTree();
theTree.insert(1, "vasa", "pupkin", "3", "456987", 2, 7 , 1992);
theTree.insert(2, "ivan", "ivanov", "2", "789898", 3, 5 , 1991 );
theTree.insert(4, "dima", "vengel", "3", "7896546", 9, 4, 1992);
theTree.Print();
theTree.Print();
theTree.Print();
}
}
class Node {
Node left;
Node right;
int data;
String name;
String surname;
String course;
String ticket;
int day;
int month;
int year;
public Node(int data, String name, String surname, String course, String ticket, int day, int month, int year){
this.data = data;
this.name = name;
this.surname = surname;
this.course = course;
this.ticket = ticket;
this.day = day;
this.month = month;
this.year = year;
}
public void Print(){
System.out.println("data" + data);
System.out.println("Имя" + name);
System.out.println("Фамилия" + surname);
System.out.println("Курс" + course);
System.out.println("День" + day);
System.out.println("месяц" + month);
System.out.println("год" + year);
}
} |