-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaAlgorithm_6.java
230 lines (199 loc) · 7.18 KB
/
JavaAlgorithm_6.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
package javaAlgorithms;
public class JavaAlgorithm_6 {
//дерево и связанные с ним методы
public static void main(String[] args) {
long startTime = System.nanoTime();
Tree theTree = new Tree();
theTree.insert(new Person(4, "Jhon", 35));
theTree.insert(new Person(2, "Paul", 78));
theTree.insert(new Person(3, "George", 24));
theTree.insert(new Person(5, "Ringo", 21));
theTree.max().display();
theTree.min().display();
theTree.find(3).display();
theTree.delete(2);
System.out.println();
theTree.displayTree();
long endTime = System.nanoTime();
System.out.println("На выполнение расчетов ушло " + (endTime - startTime) + " единиц времени");
}
static class Person {
public String name;
public int id;
public int age;
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
static class Node {
public Person person;
public Node leftChild;
public Node rightChild;
public void display() {
System.out.println("ID: " + person.id + " Имя " + person.name + " Возраст: " + person.age);
}
}
static class Tree {
private Node root;
public void insert (Person person) {
Node node = new Node();
node.person = person;
if (root == null)
root = node;
else {
Node current = root;
Node parent;
while (true) {
parent = current;
if (person.id < current.person.id) {
current = current.leftChild;
if (current == null) {
parent.leftChild = node;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = node;
return;
}
}
}
}
}
public Node find (int key) {
Node current = root;
while (current.person.id != key) {
if (key < current.person.id)
current = current.leftChild;
else
current = current.rightChild;
if (current == null)
return null;
}
return current;
}
//симметричный обход дерева
public void inOrder (Node rootNode) {
if (rootNode != null) {
inOrder(rootNode.leftChild);
rootNode.display();
inOrder(rootNode.rightChild);
}
}
//прямой обход дерева
public void preOrder (Node rootNode) {
if (rootNode != null) {
rootNode.display();
preOrder(rootNode.leftChild);
preOrder(rootNode.rightChild);
}
}
//обратный обход дерева
public void postOrder (Node rootNode) {
if (rootNode != null) {
postOrder(rootNode.leftChild);
postOrder(rootNode.rightChild);
rootNode.display();
}
}
public Node min () {
Node current, last = null;
current = root;
while (current != null) {
last = current;
current = current.leftChild;
}
return last;
}
public Node max () {
Node current, last = null;
current = root;
while (current != null) {
last = current;
current = current.rightChild;
}
return last;
}
public boolean delete (int id) {
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while (current.person.id != id) {
parent = current;
if (id < current.person.id) {
isLeftChild = true;
current = current.leftChild;
} else {
isLeftChild = false;
current = current.rightChild;
}
if (current == null) {
return false;
}
}
if (current.leftChild == null && current.rightChild == null) {
if (current == root) {
root = null;
} else if (isLeftChild) {
parent.leftChild = null;
} else {
parent.rightChild = null;
}
} else if (current.rightChild == null) {
if (current == null) {
root = current.leftChild;
} else if (isLeftChild) {
parent.leftChild = current.leftChild;
} else {
parent.rightChild = current.leftChild;
}
} else if (current.leftChild == null) {
if (current == null) {
root = current.rightChild;
} else if (isLeftChild) {
parent.leftChild = current.rightChild;
} else {
parent.rightChild = current.rightChild;
}
} else {
Node successor = getSuccessor (current);
if (current == root) {
root = successor;
} else if (isLeftChild) {
parent.leftChild = successor;
} else {
parent.rightChild = successor;
}
successor.leftChild = current.leftChild;
}
return true;
}
public Node getSuccessor (Node node) {
Node successorParent = node;
Node successor = node;
Node current = node.rightChild;
while (current != null) {
successorParent = successor;
successor = current;
current = current.leftChild;
}
if (successor != node.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = node.rightChild;
}
return successor;
}
public void displayTree () {
Node current = root;
System.out.println("Симметричный");
inOrder(root);
System.out.println("Прямой");
preOrder(root);
System.out.println("Обратный");
postOrder(root);
}
}
}