-
Notifications
You must be signed in to change notification settings - Fork 0
/
MWayNode.java
376 lines (307 loc) · 7.79 KB
/
MWayNode.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
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
/**
*
*
*
*
* @author Maria Sigal
* Marina Khytynova
*
* Tutorium , Dienstag 16:00
*
*/
public class MWayNode {
public int[] A;//arrays that holds number of children and keys
public MWayNode parent;
public LinkedList<MWayNode> childern;//children nodes
public static void main(String[] args) {
MWayNode root = new MWayNode(10,14);
MWayNode node1 =new MWayNode(8);
node1.parent = root;
MWayNode node2 =new MWayNode(11,13);
node2.parent = root;
MWayNode node3 =new MWayNode(16);
node3.parent = root;
root.childern = new LinkedList<MWayNode>();
root.childern.add(node1);
root.childern.add(node2);
root.childern.add(node3);
System.out.println("2-4 Tree represantion " + root.toString());
System.out.println(" tree height "+root.height());
System.out.println(" is 2-4 tree "+ root.check_2_4Baum());
root.insert(15);
System.out.println("tree after 15 insertion " +root.toString());
root.insert(12);
System.out.println("tree after 12 insertion " +root.toString());
//no 2-4 , a leaves height is not the same
root = new MWayNode(10,215);
node1 =new MWayNode(8);
node1.parent = root;
node2 = new MWayNode();
node3 =new MWayNode(16);
node3.parent = root;
root.childern = new LinkedList<MWayNode>();
root.childern.add(node1);
root.childern.add(node2);
root.childern.add(node3);
System.out.println(" second tree height "+root.height());
System.out.println(" is second tree 2-4 tree "+ root.check_2_4Baum());
//a tree with overflow
root = new MWayNode(10,215);
node1 =new MWayNode(8);
node1.parent = root;
node2 = new MWayNode(98);
node3 =new MWayNode(716);
node3.parent = root;
root.A[0]=5;
root.childern = new LinkedList<MWayNode>();
root.childern.add(node1);
root.childern.add(node2);
root.childern.add(node3);
System.out.println("tree with overflow tree height "+root.height());
System.out.println(" is third tree 2-4 tree "+ root.check_2_4Baum());
}
//Begin 8.a
/**
* constructor to create a leaf
*
*/
public MWayNode(){
A = new int[5];
childern = new LinkedList<MWayNode>();
A[0]=0;
}
/**
* a constructor to create a node with
* one key , one key has 2 leaves
*
* @param k1
*/
public MWayNode(int k1){
A = new int[5];
A[0]=2;//number of children, in our case leaves
A[1]=k1;
//create children
childern = new LinkedList<MWayNode>();
childern.add(new MWayNode());
childern.add(new MWayNode());
//assign parent to children
for(MWayNode child:this.childern){
child.parent=this;
}
}
/**
* a constructor to create a node with
* 2 key , 2 keys have 3 children leaves
*
* @param k1, k2
*/
public MWayNode(int k1, int k2){
A = new int[5];
A[0]=3;
A[1]=k1;
A[2]=k2;
childern = new LinkedList<MWayNode>();
childern.add(new MWayNode());
childern.add(new MWayNode());
childern.add(new MWayNode());
for(MWayNode child:this.childern){
child.parent=this;
}
}
/**
* a constructor to create a node with
* 3 key , 3 keys have 4 children leaves
*
* @param k1, k2
*/
public MWayNode(int k1, int k2, int k3){
A = new int[5];
A[0]=4;
A[1]=k1;
A[2]=k2;
A[3]=k3;
childern = new LinkedList<MWayNode>();
childern.add(new MWayNode());
childern.add(new MWayNode());
childern.add(new MWayNode());
childern.add(new MWayNode());
for(MWayNode child:this.childern){
child.parent=this;
}
}
public int size(){
return A[0];
}
//end 8.a
/**
* returns
*rep(v) := () wenn v Blatt ist.
rep(v) := (k1, . . . , kd−1)[rep(v1), . . . , rep(vd)] wenn v ein d-Knoten mit den Schl¨usseln
k1, . . . , kd−1 und den Kindern v1, . . . , vd ist.
of a tree
*/
//Begin 8.b
public String toString(){
return toString(this);
}
private String toString(MWayNode node){
//if leaf
if (node.A[0]==0){
return "()";
}
String str ="(";
int i=1;
//go through all keys , concatenate them to the string
for(;i<node.A[0]-1;i++){
str+=node.A[i]+",";
}
str+=node.A[i]+")";
// now concatenate nodes
str +="[";
for(MWayNode c:node.childern){
// go recursively
str += toString(c) +",";
}
str = str.substring(0,str.length()-1);
str +="]";
return str;
}
//end 8.b
//Begin 8.c
//the tree is 2-4 if the leaves have the same depth
//and all nodes except leaves have 2 to 4 children
public boolean check_2_4Baum(){
return check24Baum(this);
}
private boolean check24Baum(MWayNode node){
boolean is24Baum=false;
//if leaf return true
if(node.A[0] ==0){
return true;
}
int heights[]=new int[4];
int i=0;
//check of height of the nodes
for(MWayNode child:node.childern){
heights[i]=height(child);
i++;
}
boolean sameHeight=false;
//check whether all entries have the same height
for(i=0;i<node.childern.size()-1;i++){
sameHeight = heights[i] ==heights[i+1];
// if no return false
if(!sameHeight) return false;
}
//check whether the entry has minimum 2 children and maximum 4 children
if((node.A[0]>=2 && node.A[0]<=4) && sameHeight){
is24Baum =true;
//check recursively
for(MWayNode child:node.childern){
is24Baum =is24Baum && check24Baum(child);
}
}
return is24Baum;
}
/**
*
* calculates height of a node
* @return
*/
public int height(){
return height(this);
}
private int height(MWayNode node){
if(node.A[0] ==0 && node.childern.size()==0){
return 0;
}
int heights[]=new int[4];
int i=0;
for(MWayNode child :node.childern){
heights[i] = height(child);
i++;
}
return 1+findMax(heights);
}
private int findMax(int[] array){
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
//2.d
/**
* insert key in tree
*
*
*
* @param k
*/
public void insert(int k){
//first we find a leaf
MWayNode leave = findLeaf(k,this);
MWayNode parent = leave.parent;
int[] keysCopy = new int[5];
LinkedList<MWayNode> children = new LinkedList<MWayNode>();
//we create new keys array
//increment number of entries
keysCopy[0]=parent.A[0]+1;
int j=1;
//System.out.println("parent.A[0] = " +keysCopy[0]+": " + Arrays.toString(parent.A));
int i=1;
//we go through the keys in leaf parent
// to find the position where to insert the key
for(i=1;i<parent.A[0];i++){
//if key is smaller than current key
// we found the right position
if(k<=parent.A[i]){
// store new key
keysCopy[j] =k;
//copy the successor key for original array
keysCopy[j+1] =parent.A[i];
//create new leaf for new inserted key
MWayNode newLeaf = new MWayNode();
//update the parent
newLeaf.parent = parent;
//add the new leaf to children list
children.add(newLeaf);
j=j+2;
}else{
//copy key from original keys array
keysCopy[j] =parent.A[i];
j++;
}
//copy child node
children.add(parent.childern.get(i-1));
}
children.add(parent.childern.get(i-1));
//update keys
parent.A = keysCopy;
//System.out.println("keysCopy " + Arrays.toString(keysCopy));
//System.out.println("children " + children);
//update entries
parent.childern = children;
}
private MWayNode findLeaf(int k, MWayNode node){
if(node.A[0]==0){
return node;
}
int position=node.A[0]-1;
for(int i=1;i<=node.A[0]-1;i++){
//System.out.println("find " + Arrays.toString(node.A));
if(k<=node.A[i]){
position = i-1;
//System.out.println("found poisitionfff " + Arrays.toString(node.A) + " " + position);
break;
}
}
//System.out.println("found poisition " + Arrays.toString(node.A) + " " + position);
return findLeaf(k, node.childern.get(position));
}
}