-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLokudBilTre.java
295 lines (271 loc) · 9.43 KB
/
LokudBilTre.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
/*************************************************************************
* Tre fyrir lokud bil
*
* Hlutir af tagi LokufBilTre eru hnutar i tvileitartre af strengjum.
* Tilvisun i hlut af tagi LokudBilTre getur thvi stadid fyrir heilt tvileitartre
*
* Thyding: javac LokudBilTre.java
* Keyrsla: java LokudBilTre
* Naudsynleg fylgiskjol: Interval.java
*
*************************************************************************/
import java.util.LinkedList;
import java.util.*;
public class LokudBilTre<Value> {
private Node root;
// Fastayrding gagna:
// LokudBilTre er tilvisun a hlut af thessu tagi.
// Tom tilvisun (null) stendur fyrir tomt tre.
// Ef tilvisunin er ekki tom visar hun a hlut
// sem stendur fyrir rot (undir)tresins. Tred uppfyllir
// annars vegar tvileitartrjaaskilyrdi:
// Oll gildi i vinstra undirtre eru alltaf (fyrir
// oll undirtre) <= rotargildi og oll gildi i
// haegra undirtre eru >= rotargildi.
// Rot heildartresins hefur root==null,
// en allir adrir hnutar hafa root sem visar a
// annan LokudBilTre hlut.
private class Node {
Interval interval;
Value value;
Node left, right; // vinstra/haegra undirtre
int N; // Staerd undirtres i thessum hnut
int max; // Staersta gildi undirtres thessa hnuts
Node(Interval interval, Value value) {
this.interval = interval;
this.value = value;
this.N = 1;
this.max = interval.high;
}
}
// Notkun: x = tree.get();
// Fyrir: tree er ekki-tomt tre
// Eftir: x er bilid i rotinni a tree
public Value get(Interval interval) {
return get(root, interval);
}
private Value get(Node x, Interval interval) {
if(x == null){
return null;
}
int cmp = interval.compareIntervals(x.interval);
if(cmp < 0){
return get(x.left, interval);
}
else if(cmp > 0){
return get(x.right, interval);
}
else{
return x.value;
}
}
/**
+ adgerdin
*/
// Notkun: tree.insert(i,v);
// Fyrir: v er strengur, i er Interval (ma vera tomt)
// Eftir: tree er tred sem ut kemur thegar i er baett i hnut med gildid v
public void insert(Interval interval, Value value) {
root = rootInsert(root, interval, value);
}
private Node rootInsert(Node x, Interval interval, Value value) {
if (x == null) return new Node(interval, value);
int cmp = interval.compareIntervals(x.interval);
if (cmp < 0) {
x.left = rootInsert(x.left, interval, value);
x = rotateRight(x);
}
else{
x.right = rootInsert(x.right, interval, value);
x = rotateLeft(x);
}
return x;
}
// Notkun: x = joinLeftRight(l,r);
// Fyrir: Oll gildi i left eru <= oll gildi i right
// Eftir: x inniheldur alla hnuta ur left og right, i rettri rod.
// l og r eru nu ogild (ordin ad innri hnutum i tre).
private Node joinLeftRight(Node a, Node b) {
if (a == null) return b;
if (b == null) return a;
if (Math.random() * (size(a) + size(b)) < size(a)) {
a.right = joinLeftRight(a.right, b);
fix(a);
return a;
}
else {
b.left = joinLeftRight(a, b.left);
fix(b);
return b;
}
}
/**
- adgerdin
*/
// Notkun: tree.remove(i);
// Fyrir: i er Interval i treinu og tree er tre
// Eftir: i hefur verid fjaraegt ur tree
public Value remove(Interval interval) {
Value value = get(interval);
root = remove(root, interval);
return value;
}
private Node remove(Node h, Interval interval) {
if (h == null) return null;
int cmp = interval.compareIntervals(h.interval);
if (cmp < 0) h.left = remove(h.left, interval);
else if (cmp > 0) h.right = remove(h.right, interval);
else h = joinLeftRight(h.left, h.right);
fix(h);
return h;
}
/**
?o adgerdin
*/
// running time is proportional to R log N, where R is the number of intersections
// Notkun: l = x.intersects(i);
// Fyrir: x er tre og i er Interval
// Eftir: l er listi af Interval hlutum tresins sem skarast vid lokada bilid i
public LinkedList intersects(Interval interval) {
LinkedList<Interval> list = new LinkedList<Interval>();
intersects(root, interval, list);
return list;
}
// leitad i undirtre med rot x
public boolean intersects(Node x, Interval interval, LinkedList<Interval> list) {
boolean found1 = false;
boolean found2 = false;
boolean found3 = false;
if (x == null)
return false;
if (x.interval.intersects(interval)) {
list.add(x.interval);
found1 = true;
}
if (x.left != null && x.left.max >= interval.low)
found2 = intersects(x.left, interval, list);
if (found2 || x.left == null || x.left.max < interval.low)
found3 = intersects(x.right, interval, list);
return found1 || found2 || found3;
}
/**
?i adgerdin
*/
// Notkun: l = x.includes(i);
// Fyrir: x er tre og i er Interval
// Eftir: l er listi af Interval hlutum tresins sem innihalda lokada bilid i
public LinkedList includes(Interval interval) {
LinkedList<Interval> list = new LinkedList<Interval>();
includes(root, interval, list);
return list;
}
public boolean includes(Node x, Interval interval, LinkedList<Interval> list) {
boolean found1 = false;
boolean found2 = false;
boolean found3 = false;
if (x == null)
return false;
if (x.interval.includes(interval)) {
list.add(x.interval);
found1 = true;
}
if (x.left != null && x.left.max >= interval.low)
found2 = includes(x.left, interval, list);
if (found2 || x.left == null || x.left.max < interval.low)
found3 = includes(x.right, interval, list);
return found1 || found2 || found3;
}
/**
?p adgerdin
*/
// Notkun: l = x.inside(p);
// Fyrir: x er tre og p er heiltala
// Eftir: l er listi af Interval hlutum tresins sem innihalda heiltoluna p
public LinkedList inside(int p) {
Interval interval = new Interval(p, p);
LinkedList<Interval> list = new LinkedList<Interval>();
inside(root, p, list, interval);
return list;
}
public boolean inside(Node x, int p, LinkedList<Interval> list, Interval interval) {
boolean found1 = false;
boolean found2 = false;
boolean found3 = false;
if (x == null)
return false;
if (x.interval.findPoint(p)) {
list.add(x.interval);
found1 = true;
}
if (x.left != null && x.left.max >= p)
found2 = inside(x.left, p, list, interval);
if (found2 || x.left == null || x.left.max < p)
found3 = inside(x.right, p, list, interval);
return found1 || found2 || found3;
}
/**
*/
// Notkun: x = size(t)
// Fyrir: t er hnutur
// Eftir: x er fjoldi hnuta hluttres sem hefur rot i t
public int size() { return size(root); }
private int size(Node x) {
if (x == null) return 0;
else return x.N;
}
// fix auxilliar information (subtree count and max fields)
// Notkun: fix(t)
// Fyrir: t er hnutur
// Eftir: uppfaerir fjolda hluttrjaa og staersta gildi undirtres hnutsins
private void fix(Node x) {
if (x == null) return;
x.N = 1 + size(x.left) + size(x.right);
x.max = max3(x.interval.high, max(x.left), max(x.right));
}
// Notkun: x = max(t)
// Fyrir: t er hnutur
// Eftir: x er staersta gildi undirtres t
private int max(Node x) {
if (x == null) return Integer.MIN_VALUE;
return x.max;
}
// Notkun: x = max3(t)
// Fyrir: a,b,c eru heiltolur og a ma ekki vera null
// Eftir: x er staersta talan af theim 3 sem eru i inntakinu
private int max3(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
}
// Notkun: x = rotateRight(t)
// Fyrir: t og x eru hnutar
// Eftir: hnutnum hefur verid snuid til haegri
private Node rotateRight(Node h) {
Node x = h.left;
h.left = x.right;
x.right = h;
fix(h);
fix(x);
return x;
}
// Notkun: x = rotateRight(t)
// Fyrir: t og x eru hnutar
// Eftir: hnutnum hefur verid snuid til vinstri
private Node rotateLeft(Node h) {
Node x = h.right;
h.right = x.left;
x.left = h;
fix(h);
fix(x);
return x;
}
// Notkun: printList(l)
// Fyrir: l er listi af bilum (Intervals)
// Eftir: Listanum l hefur verid radad eftir minnsta staki hvers bils.
// A medan listinn er ekki tomur er fremsta stak hans prentad.
public static void printList(LinkedList list){
Collections.sort(list, new IntervalComparator());
while(!list.isEmpty()){
System.out.print(list.removeFirst() + " ");
}
System.out.println();
}
}