-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
191 lines (163 loc) · 5.67 KB
/
BST.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
public class BST {
private static int comparisons = 0;
private static boolean useLeftSubtreeOnDelete = true;
private int el;
private int duplicates = 0;
private BST leftSubTree;
private BST rightSubTree;
public void printNodeComparisons() {
System.out.println(comparisons);
}
// vstavi element
public void insert(int el) {
if (this.duplicates == 0) {
this.el = el;
this.duplicates = 1;
} else {
comparisons++;
if (el == this.el){
this.duplicates++;
} else if (el < this.el) {
if (leftSubTree == null)
leftSubTree = new BST();
leftSubTree.insert(el);
} else {
if (rightSubTree == null)
rightSubTree = new BST();
rightSubTree.insert(el);
}
}
}
// poisci, ali obstaja element
public boolean find(int key) {
if (this.duplicates == 0) {
return false;
}
comparisons += 1;
// element najden
if (this.el == key) {
return true;
}
// ce element manjsi, pojdi v levo poddrevo
else if (key < this.el){
if (this.leftSubTree == null)
return false;
return leftSubTree.find(key);
}
// element je vecji, pojdi v desno poddrevo
else {
if (this.rightSubTree == null)
return false;
return rightSubTree.find(key);
}
}
// vmesni obhod
public void printInorder() {
if (this.duplicates == 0)
return;
if (leftSubTree != null)
leftSubTree.printInorder();
System.out.println(this.el);
if (rightSubTree != null) {
rightSubTree.printInorder();
}
}
// vmesni obhod
public void printPreorder() {
if (this.duplicates == 0)
return;
System.out.println(this.el);
if (leftSubTree != null) {
leftSubTree.printPreorder();
}
if (rightSubTree != null) {
rightSubTree.printPreorder();
}
}
// premi obhod
public void printPostorder() {
if (this.duplicates == 0)
return;
if (leftSubTree != null)
leftSubTree.printPostorder();
if (rightSubTree != null)
rightSubTree.printPostorder();
System.out.println(this.el);
}
// izbrisi element
public void delete(int key) {
// poisci, ali obstaja element
if (this.duplicates == 0) {
return;
}
comparisons++;
// element najden
if (this.el == key){
// duplicirana vrednost
if (this.duplicates > 1){
this.duplicates--;
return;
}
// odstrani element
boolean imaLevoPoddrevo = this.leftSubTree != null && this.leftSubTree.duplicates > 0;
boolean imaDesnoPoddrevo = this.rightSubTree != null && this.rightSubTree.duplicates > 0;
// vozlisce nima poddreves
if (!imaLevoPoddrevo && !imaDesnoPoddrevo){
this.duplicates = 0;
}
// vozlisce ima samo levo poddrevo
else if (imaLevoPoddrevo && !imaDesnoPoddrevo) {
this.el = this.leftSubTree.el;
this.duplicates = this.leftSubTree.duplicates;
this.rightSubTree = this.leftSubTree.rightSubTree;
this.leftSubTree = this.leftSubTree.leftSubTree;
}
// vozlisce ima samo desno poddrevo
else if (!imaLevoPoddrevo && imaDesnoPoddrevo) {
this.el = this.rightSubTree.el;
this.duplicates = this.rightSubTree.duplicates;
this.leftSubTree = this.rightSubTree.leftSubTree;
this.rightSubTree = this.rightSubTree.rightSubTree;
}
// vozlisce ima levo in desno poddrevo
else {
if (useLeftSubtreeOnDelete) {
// poišči največji element v levem poddrevesu ki bo zamenjal izbrisani element
BST zamenjava = this.leftSubTree;
while (zamenjava.rightSubTree != null) {
zamenjava = zamenjava.rightSubTree;
}
this.el = zamenjava.el;
this.duplicates = zamenjava.duplicates;
// rekurzijsko odstrani zamenjavo;
zamenjava.duplicates = 1;
zamenjava.delete(zamenjava.el);
} else {
// poišči najmanjši element v desnem poddrevesu ki bo zamenjal izbrisani element
BST zamenjava = this.rightSubTree;
while (zamenjava.leftSubTree != null) {
zamenjava = zamenjava.leftSubTree;
}
this.el = zamenjava.el;
this.duplicates = zamenjava.duplicates;
// rekurzijsko odstrani zamenjavo;
zamenjava.duplicates = 1;
zamenjava.delete(zamenjava.el);
}
useLeftSubtreeOnDelete = !useLeftSubtreeOnDelete;
}
}
// ce element manjsi, pojdi v levo poddrevo
else if (key < this.el){
if (this.leftSubTree == null)
return;
leftSubTree.delete(key);
}
// element je vecji, pojdi v desno poddrevo
else {
if (this.rightSubTree == null)
return ;
rightSubTree.delete(key);
}
}
}