FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/BTree/src/BTree.java at master · wodgithub/algorithm · GitHub
wodgithub
algorithm
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
BTree
/
src
/
BTree.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
460 lines (375 loc) · 13.6 KB
Breadcrumbs
algorithm
/
BTree
/
src
/
BTree.java
Copy path
File metadata and controls
460 lines (375 loc) · 13.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Arrays
;
import
java
.
util
.
LinkedList
;
import
java
.
util
.
Queue
;
public
class
BTree
{
private
int
value
;
private
BTree
left
;
private
BTree
right
;
private
BTree
parent
;
public
BTree
() {
}
public
BTree
(
int
v
) {
this
.
value
=
v
;
this
.
left
=
null
;
this
.
right
=
null
;
this
.
parent
=
null
;
}
public
static
BTree
createMinimalBT
(
BTree
root
,
int
[]
array
,
int
s
,
int
e
) {
if
(
s
>=
0
&&
e
<=
array
.
length
-
1
&&
s
<=
e
) {
int
m
= (
e
+
s
) /
2
;
root
=
new
BTree
(
array
[
m
]);
root
.
left
=
createMinimalBT
(
root
.
left
,
array
,
s
,
m
-
1
);
root
.
right
=
createMinimalBT
(
root
.
right
,
array
,
m
+
1
,
e
);
return
root
;
}
else
return
root
;
}
public
static
void
insertNode
(
BTree
node
,
int
v
) {
if
(
node
!=
null
) {
if
(
v
<=
node
.
value
) {
if
(
node
.
left
==
null
) {
node
.
left
=
new
BTree
(
v
);
node
.
left
.
parent
=
node
;
}
else
insertNode
(
node
.
left
,
v
);
}
else
{
if
(
node
.
right
==
null
) {
node
.
right
=
new
BTree
(
v
);
node
.
right
.
parent
=
node
;
}
else
insertNode
(
node
.
right
,
v
);
}
}
}
public
static
void
showByLevel
(
BTree
root
) {
if
(
root
==
null
) {
System
.
out
.
println
(
"a null tree"
);
return
;
}
Queue
<
BTree
>
current
=
new
LinkedList
<
BTree
>();
Queue
<
BTree
>
next
=
new
LinkedList
<
BTree
>();
current
.
add
(
root
);
System
.
out
.
println
(
root
.
value
);
while
(!
current
.
isEmpty
()) {
BTree
node
=
current
.
poll
();
if
(
node
.
left
!=
null
) {
next
.
add
(
node
.
left
);
System
.
out
.
print
(
node
.
left
.
value
+
"
\t
"
);
}
if
(
node
.
right
!=
null
) {
next
.
add
(
node
.
right
);
System
.
out
.
print
(
node
.
right
.
value
+
"
\t
"
);
}
if
(
current
.
isEmpty
()) {
current
.
addAll
(
next
);
next
.
clear
();
System
.
out
.
println
();
}
}
}
public
static
int
width
(
BTree
root
) {
if
(
root
==
null
)
return
0
;
Queue
<
BTree
>
current
=
new
LinkedList
<
BTree
>();
Queue
<
BTree
>
next
=
new
LinkedList
<
BTree
>();
current
.
add
(
root
);
int
width
=
1
;
while
(!
current
.
isEmpty
()) {
BTree
node
=
current
.
poll
();
if
(
node
.
left
!=
null
) {
next
.
add
(
node
.
left
);
}
if
(
node
.
right
!=
null
) {
next
.
add
(
node
.
right
);
}
if
(
current
.
isEmpty
()) {
if
(
next
.
size
() >
width
)
width
=
next
.
size
();
current
.
addAll
(
next
);
next
.
clear
();
}
}
return
width
;
}
public
static
int
height
(
BTree
node
) {
if
(
node
==
null
)
return
0
;
else
return
Math
.
max
(
height
(
node
.
left
),
height
(
node
.
right
)) +
1
;
}
public
static
boolean
containTree
(
BTree
t1
,
BTree
t2
) {
if
(
t1
==
null
)
return
false
;
if
(
t2
==
null
)
return
true
;
if
(
t1
.
value
==
t2
.
value
)
return
matchTree
(
t1
.
left
,
t2
.
left
) &&
matchTree
(
t1
.
right
,
t2
.
right
);
else
if
(
t1
.
value
<
t2
.
value
)
return
containTree
(
t1
.
right
,
t2
);
else
return
containTree
(
t1
.
left
,
t2
);
}
public
static
boolean
matchTree
(
BTree
t1
,
BTree
t2
) {
if
(
t1
==
null
&&
t2
==
null
)
return
true
;
else
if
(
t1
!=
null
&&
t2
!=
null
) {
if
(
t1
.
value
==
t2
.
value
)
return
matchTree
(
t1
.
left
,
t2
.
left
) &&
matchTree
(
t1
.
right
,
t2
.
right
);
}
return
false
;
}
public
static
BTree
commonRoot
(
BTree
root
,
int
p
,
int
q
) {
if
(
root
==
null
)
return
null
;
if
(
root
.
value
==
p
||
root
.
value
==
q
)
return
root
;
BTree
l
=
commonRoot
(
root
.
left
,
p
,
q
);
BTree
r
=
commonRoot
(
root
.
right
,
p
,
q
);
/*
if (r != null && r.value != q && r.value != p)
return r;
if (l != null && l.value != q && l.value != p)
return l;
*/
if
(
l
!=
null
&&
r
!=
null
&&
l
.
value
!=
r
.
value
)
return
root
;
else
return
(
l
!=
null
) ?
l
:
r
;
}
public
static
BTree
coverNode
(
BTree
root
,
int
p
) {
if
(
root
==
null
)
return
null
;
BTree
l
=
coverNode
(
root
.
left
,
p
);
BTree
r
=
coverNode
(
root
.
right
,
p
);
// find the lowest one first
if
(
l
!=
null
||
r
!=
null
)
return
(
l
!=
null
) ?
l
:
r
;
else
if
(
root
.
value
==
p
)
return
root
;
else
return
null
;
}
public
static
BTree
commonRootTopDown
(
BTree
root
,
int
p
,
int
q
) {
if
(
root
==
null
)
return
null
;
if
(
root
.
value
==
p
&&
root
.
value
==
q
)
return
root
;
if
(
root
.
value
==
p
) {
if
(
coverNode
(
root
.
left
,
q
) !=
null
||
coverNode
(
root
.
right
,
q
) !=
null
|| ((
coverNode
(
root
.
left
,
q
) ==
null
&&
coverNode
(
root
.
right
,
q
) ==
null
)))
return
root
;
}
else
if
(
root
.
value
==
q
) {
if
(
coverNode
(
root
.
left
,
p
) !=
null
||
coverNode
(
root
.
right
,
p
) !=
null
|| ((
coverNode
(
root
.
left
,
q
) ==
null
&&
coverNode
(
root
.
right
,
q
) ==
null
)))
return
root
;
}
else
{
BTree
l
=
commonRootTopDown
(
root
.
left
,
p
,
q
);
BTree
r
=
commonRootTopDown
(
root
.
right
,
p
,
q
);
if
(
l
!=
null
&&
r
!=
null
) {
if
(
l
.
value
!=
r
.
value
)
return
root
;
}
return
(
l
!=
null
) ?
l
:
r
;
}
return
null
;
}
public
static
void
findAllPathSum
(
BTree
root
,
int
k
,
ArrayList
<
BTree
>
p
,
ArrayList
<
ArrayList
<
BTree
>>
all
) {
if
(
root
==
null
)
return
;
ArrayList
<
BTree
>
path
=
new
ArrayList
<
BTree
>(
p
);
path
.
add
(
root
);
int
sum
=
0
;
for
(
BTree
node
:
path
) {
sum
+=
node
.
value
;
}
if
(
sum
==
k
) {
all
.
add
(
new
ArrayList
<
BTree
>(
path
));
}
findAllPathSum
(
root
.
left
,
k
,
path
,
all
);
findAllPathSum
(
root
.
right
,
k
,
path
,
all
);
}
public
static
ArrayList
<
ArrayList
<
BTree
>>
findAllPathSum
(
BTree
root
,
int
k
) {
ArrayList
<
ArrayList
<
BTree
>>
all
=
new
ArrayList
<
ArrayList
<
BTree
>>();
ArrayList
<
BTree
>
path
=
new
ArrayList
<
BTree
>();
findAllPathSum
(
root
,
k
,
path
,
all
);
return
all
;
}
public
static
BTree
leftMostNode
(
BTree
node
) {
while
(
node
.
left
!=
null
)
node
=
node
.
left
;
return
node
;
}
// left, root, right
public
static
BTree
inorderSuc
(
BTree
root
,
int
k
) {
if
(
root
==
null
)
return
null
;
if
(
root
.
value
==
k
) {
if
(
root
.
right
!=
null
)
return
leftMostNode
(
root
.
right
);
else
{
BTree
parent
=
root
.
parent
;
BTree
tmp
=
root
;
while
(
parent
!=
null
) {
if
(
parent
.
left
==
tmp
)
break
;
else
{
// parent.right == root
tmp
=
parent
;
parent
=
parent
.
parent
;
}
}
return
parent
;
}
}
else
{
BTree
lnode
=
inorderSuc
(
root
.
left
,
k
);
BTree
rnode
=
inorderSuc
(
root
.
right
,
k
);
return
(
lnode
!=
null
) ?
lnode
:
rnode
;
}
}
//root, left, right
public
static
BTree
preorderSuc
(
BTree
node
,
int
k
) {
if
(
node
==
null
)
return
null
;
if
(
node
.
value
==
k
) {
if
(
node
.
left
!=
null
)
return
node
.
left
;
else
if
(
node
.
right
!=
null
)
return
node
.
right
;
else
{
BTree
tmp
=
node
;
BTree
parent
=
node
.
parent
;
while
(
parent
!=
null
) {
if
(
parent
.
right
==
tmp
) {
tmp
=
parent
;
parent
=
parent
.
parent
;
}
else
{
// parent.left == tmp
if
(
parent
.
right
!=
null
)
return
parent
.
right
;
else
{
tmp
=
parent
;
parent
=
parent
.
parent
;
}
}
}
return
parent
;
}
}
else
{
BTree
lnode
=
preorderSuc
(
node
.
left
,
k
);
BTree
rnode
=
preorderSuc
(
node
.
right
,
k
);
return
(
lnode
!=
null
) ?
lnode
:
rnode
;
}
}
// left, right, root
public
static
BTree
postorderSuc
(
BTree
root
,
int
k
) {
if
(
root
==
null
)
return
null
;
if
(
root
.
value
==
k
) {
BTree
parent
=
root
.
parent
;
if
(
parent
!=
null
) {
if
(
parent
.
left
==
root
) {
if
(
parent
.
right
!=
null
)
return
parent
.
right
;
else
return
parent
;
}
else
// parent.right == root
return
parent
;
}
return
null
;
}
else
{
BTree
lnode
=
postorderSuc
(
root
.
left
,
k
);
BTree
rnode
=
postorderSuc
(
root
.
right
,
k
);
return
(
lnode
!=
null
) ?
lnode
:
rnode
;
}
}
public
static
int
treeSize
(
BTree
root
) {
if
(
root
==
null
)
return
0
;
else
return
treeSize
(
root
.
left
) +
treeSize
(
root
.
right
) +
1
;
}
public
static
BTree
k_smallest_element
(
BTree
root
,
int
k
) {
if
(
k
>
treeSize
(
root
))
return
null
;
int
i
=
treeSize
(
root
.
left
);
if
(
i
==
k
-
1
)
return
root
;
else
if
(
i
>
k
-
1
)
return
k_smallest_element
(
root
.
left
,
k
);
else
// ( i < k-1)
return
k_smallest_element
(
root
.
right
,
k
-
i
-
1
);
}
public
static
int
counter
=
0
;
public
static
void
k_smallest_element1
(
BTree
root
,
int
k
) {
if
(
root
==
null
)
return
;
k_smallest_element1
(
root
.
left
,
k
);
if
(++
counter
==
k
)
// count root node
System
.
out
.
println
(
"tree smallest: "
+
root
.
value
);
k_smallest_element1
(
root
.
right
,
k
);
}
public
static
void
main
(
String
[]
args
) {
int
[]
a1
=
new
int
[]{
2
,
3
,
4
,
5
,
7
,
9
,
12
,
13
,
10
,
18
,
20
};
BTree
t1
=
new
BTree
();
t1
=
createMinimalBT
(
t1
,
a1
,
0
,
a1
.
length
-
1
);
System
.
out
.
println
(
"tree height: "
+
height
(
t1
));
System
.
out
.
println
(
"t3 binary tree width "
+
width
(
t1
));
System
.
out
.
println
(
"tree size: "
+
treeSize
(
t1
));
System
.
out
.
println
(
"tree 4 smallest: "
+
k_smallest_element
(
t1
,
4
).
value
);
k_smallest_element1
(
t1
,
3
);
showByLevel
(
t1
);
int
[]
a2
=
new
int
[]{
1
,
3
,
5
,
6
,
9
};
Arrays
.
sort
(
a2
);
BTree
t2
=
new
BTree
();
t2
=
createMinimalBT
(
t2
,
a2
,
0
,
a2
.
length
-
1
);
System
.
out
.
println
(
"tree height: "
+
height
(
t2
));
System
.
out
.
println
(
"t3 binary tree width "
+
width
(
t2
));
showByLevel
(
t2
);
System
.
out
.
println
(
containTree
(
t1
,
t2
));
int
p
=
12
,
q
=
18
;
BTree
cr
=
commonRootTopDown
(
t1
,
p
,
q
);
System
.
out
.
println
(
"top down common root: "
+
p
+
" and "
+
q
);
if
(
cr
!=
null
&&
coverNode
(
t1
,
p
) !=
null
&&
coverNode
(
t1
,
q
) !=
null
)
System
.
out
.
println
(
cr
.
value
);
p
=
12
;
q
=
13
;
BTree
cr1
=
commonRoot
(
t1
,
p
,
q
);
System
.
out
.
println
(
"common root: "
+
p
+
" and "
+
q
);
if
(
cr1
!=
null
&&
coverNode
(
t1
,
p
) !=
null
&&
coverNode
(
t1
,
q
) !=
null
)
System
.
out
.
println
(
cr1
.
value
);
int
sum
=
19
;
ArrayList
<
ArrayList
<
BTree
>>
result
=
findAllPathSum
(
t1
,
sum
);
System
.
out
.
println
(
"find sum: "
+
sum
);
for
(
ArrayList
<
BTree
>
l
:
result
) {
for
(
BTree
n
:
l
) {
System
.
out
.
print
(
n
.
value
+
" "
);
}
System
.
out
.
println
();
}
BTree
t3
=
new
BTree
(
10
);
insertNode
(
t3
,
3
);
insertNode
(
t3
,
7
);
insertNode
(
t3
,
4
);
insertNode
(
t3
,
5
);
insertNode
(
t3
,
2
);
insertNode
(
t3
,
1
);
insertNode
(
t3
,
0
);
insertNode
(
t3
,
15
);
insertNode
(
t3
,
12
);
insertNode
(
t3
,
18
);
insertNode
(
t3
,
16
);
insertNode
(
t3
,
20
);
System
.
out
.
println
(
"t3 binary tree"
);
System
.
out
.
println
(
"t3 binary tree width "
+
width
(
t3
));
showByLevel
(
t3
);
int
suc
=
3
;
BTree
nextNode
=
inorderSuc
(
t3
,
suc
);
System
.
out
.
println
(
"t3 inorder successor "
+
suc
);
if
(
nextNode
!=
null
)
System
.
out
.
println
(
nextNode
.
value
);
int
suc2
=
3
;
BTree
nextNode1
=
postorderSuc
(
t3
,
suc2
);
System
.
out
.
println
(
"t3 postorder successor "
+
suc2
);
if
(
nextNode1
!=
null
)
System
.
out
.
println
(
nextNode1
.
value
);
int
suc3
=
0
;
BTree
nextNode2
=
preorderSuc
(
t3
,
suc3
);
System
.
out
.
println
(
"t3 preorder successor "
+
suc3
);
if
(
nextNode2
!=
null
)
System
.
out
.
println
(
nextNode2
.
value
);
}
}
Back
|
FazBrowse Home
|
New Git URL