binary search tree的java实现
1
/**
2
* ADT binary search tree
3
*/
4
package binarySearchTree;
5
6
import java.io.Serializable;
7
import java.util.Iterator;
8
9
/**
10
* @author
11
* @version 1.0
12
*/
13
public interface IBinarySearchTree extends Serializable
14
{
15
/**
16
* Get the size of binary search tree
17
*
18
* @return an int of binary search tree's size
19
*/
20
public int size();
21
22
/**
23
* Method to clear the binary search tree
24
*/
25
public void clear();
26
27
/**
28
* Check to know if the binary search tree is empty or not
29
*
30
* @return true if the binary searchg tree is empty
31
*/
32
public boolean isEmpty();
33
34
/**
35
* Method adding an object into the binary search tree
36
*
37
* @param o the object is to be add in the binary search tree
38
*/
39
@SuppressWarnings("unchecked")
40
public void add(Comparable o);
41
42
/**
43
* Remove an object in the binary search tree
44
*
45
* @param o the object is to be remove from binary search tree
46
* @return an object that is removed
47
*/
48
@SuppressWarnings("unchecked")
49
public Object remove(Comparable o);
50
51
/**
52
* Find an object in the binary search tree
53
*
54
* @param o a specific object is wanted to be find in binary search tree
55
* @return an object if that object exist in the tree, return null if not
56
*/
57
@SuppressWarnings("unchecked")
58
public Object find(Comparable o);
59
60
/**
61
* Return the iterator of the binary search tree
62
*
63
* @return an Iterator object of the binary search tree
64
*/
65
@SuppressWarnings("unchecked")
66
public Iterator iterator();
67
}
68

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

1
package binarySearchTree;
2
3
import java.io.Serializable;
4
import java.util.Iterator;
5
6
import list.*;
7
8
/**
9
* @author
10
* @version 1.0
11
*/
12
public class BinarySearchTree implements IBinarySearchTree
13
{
14
private class BinarySearchTreeNode implements Serializable
15
{
16
/**
17
* Attributes
18
*/
19
private static final long serialVersionUID = 1L;
20
private BinarySearchTreeNode smallerLeft;
21
private BinarySearchTreeNode biggerRight;
22
private Object object;
23
24
/**
25
* Constructor
26
*/
27
public BinarySearchTreeNode(Object object)
28
{
29
this.object = object;
30
this.smallerLeft = null;
31
this.biggerRight = null;
32
}
33
34
/**
35
* Constructor
36
*/
37
public BinarySearchTreeNode()
38
{
39
this.object = null;
40
this.smallerLeft = null;
41
this.biggerRight = null;
42
}
43
44
/**
45
* Class attribute getter
46
*
47
* @return the smallerLeft
48
*/
49
public BinarySearchTreeNode getSmallerLeft()
50
{
51
return this.smallerLeft;
52
}
53
54
/**
55
* Class attribute setter
56
*
57
* @param smallerLeft the smallerLeft to set
58
*/
59
public void setSmallerLeft(BinarySearchTreeNode smallerLeft)
60
{
61
this.smallerLeft = smallerLeft;
62
}
63
64
/**
65
* Class attribute getter
66
*
67
* @return the biggerRight
68
*/
69
public BinarySearchTreeNode getBiggerRight()
70
{
71
return this.biggerRight;
72
}
73
74
/**
75
* Class attribute setter
76
*
77
* @param biggerRight the biggerRight to set
78
*/
79
public void setBiggerRight(BinarySearchTreeNode biggerRight)
80
{
81
this.biggerRight = biggerRight;
82
}
83
84
/**
85
* Class attribute getter
86
*
87
* @return the object
88
*/
89
public Object getObject()
90
{
91
return this.object;
92
}
93
94
/**
95
* Class attribute setter
96
*
97
* @param object the object to set
98
*/
99
public void setObject(Object object)
100
{
101
this.object = object;
102
}
103
}
104
private class Direction implements Serializable
105
{
106
/**
107
* Attributes
108
*/
109
private static final long serialVersionUID = 1L;
110
private int number;
111
112
/**
113
* Class attribute getter
114
*
115
* @return the number
116
*/
117
public int getNumber()
118
{
119
return this.number;
120
}
121
122
/**
123
* Class attribute setter
124
*
125
* @param number the number to set
126
*/
127
public void setNumber(int number)
128
{
129
this.number = number;
130
}
131
}
132
private class FatherNode implements Serializable
133
{
134
/**
135
* Attributes
136
*/
137
private static final long serialVersionUID = 1L;
138
private BinarySearchTreeNode bstn;
139
140
/**
141
* Class attribute getter
142
*
143
* @return the bstn
144
*/
145
public BinarySearchTreeNode getBstn()
146
{
147
return this.bstn;
148
}
149
150
/**
151
* Class attribute setter
152
*
153
* @param bstn the bstn to set
154
*/
155
public void setBstn(BinarySearchTreeNode bstn)
156
{
157
this.bstn = bstn;
158
}
159
}
160
/**
161
* Attributes
162
*/
163
private static final long serialVersionUID = 1L;
164
private BinarySearchTreeNode root;
165
private int size;
166
167
public BinarySearchTree()
168
{
169
this.root = null;
170
this.size = 0;
171
}
172
173
/*
174
* (non-Javadoc)
175
*
176
* @see binarySearchTree.IBinarySearchTree#size()
177
*/
178
public int size()
179
{
180
return this.size;
181
}
182
183
/*
184
* (non-Javadoc)
185
*
186
* @see binarySearchTree.IBinarySearchTree#clear()
187
*/
188
public void clear()
189
{
190
this.root = null;
191
this.size = 0;
192
}
193
194
/*
195
* (non-Javadoc)
196
*
197
* @see binarySearchTree.IBinarySearchTree#isEmpty()
198
*/
199
public boolean isEmpty()
200
{
201
return this.root == null;
202
}
203
204
@SuppressWarnings("unchecked")
205
private BinarySearchTreeNode findNodeForInsertion(Comparable o,
206
BinarySearchTreeNode current, Direction direction)
207
{
208
if(o.compareTo((Comparable)current.getObject()) >= 0)// right side
209
{
210
if(current.getBiggerRight() == null)
211
{
212
direction.setNumber(2);
213
return current;
214
}
215
else
216
{
217
return findNodeForInsertion(o, current.getBiggerRight(),
218
direction);
219
}
220
}
221
else
222
// left side
223
{
224
if(current.getSmallerLeft() == null)
225
{
226
direction.setNumber(1);
227
return current;
228
}
229
else
230
{
231
return findNodeForInsertion(o, current.getSmallerLeft(),
232
direction);
233
}
234
}
235
}
236
237
/*
238
* (non-Javadoc)
239
*
240
* @see binarySearchTree.IBinarySearchTree#add(java.lang.Comparable)
241
*/
242
@SuppressWarnings("unchecked")
243
public void add(Comparable o)
244
{
245
if(this.root == null)
246
{
247
BinarySearchTreeNode bstn = new BinarySearchTreeNode(o);
248
this.root = bstn;
249
this.size++;
250
}
251
else
252
{
253
Direction direction = new Direction();
254
BinarySearchTreeNode bstn = findNodeForInsertion(o, this.root,
255
direction);
256
if(direction.getNumber() == 2)// right side
257
{
258
BinarySearchTreeNode newLeaf = new BinarySearchTreeNode(o);
259
bstn.setBiggerRight(newLeaf);
260
}
261
else
262
// left side
263
{
264
BinarySearchTreeNode newLeaf = new BinarySearchTreeNode(o);
265
bstn.setSmallerLeft(newLeaf);
266
}
267
}
268
}
269
270
@SuppressWarnings("unchecked")
271
private BinarySearchTreeNode findNode(Comparable o,
272
BinarySearchTreeNode current, FatherNode upper, Direction direction)
273
{
274
if(o.compareTo((Comparable)current.getObject()) > 0)// right side
275
{
276
if(current.getBiggerRight() == null)// can't find it,error
277
{
278
upper.setBstn(null);
279
direction.setNumber(0);
280
return null;
281
}
282
else
283
{
284
upper.setBstn(current);
285
direction.setNumber(2);
286
return findNode(o, current.getBiggerRight(), upper, direction);
287
}
288
}
289
else if(o.compareTo((Comparable)current.getObject()) < 0)
290
// left side
291
{
292
if(current.getSmallerLeft() == null)// can't find it,error
293
{
294
upper.setBstn(null);
295
direction.setNumber(0);
296
return null;
297
}
298
else
299
{
300
upper.setBstn(current);
301
direction.setNumber(1);
302
return findNode(o, current.getSmallerLeft(), upper, direction);
303
}
304
}
305
else
306
// equal
307
{
308
return current;
309
}
310
}
311
312
@SuppressWarnings("unchecked")
313
private BinarySearchTreeNode findNode(Comparable o,
314
BinarySearchTreeNode current)
315
{
316
if(o.compareTo((Comparable)current.getObject()) > 0)// right side
317
{
318
if(current.getBiggerRight() == null)
319
{
320
return null;
321
}
322
else
323
{
324
return findNode(o, current.getBiggerRight());
325
}
326
}
327
else if(o.compareTo((Comparable)current.getObject()) < 0)
328
// left side
329
{
330
if(current.getSmallerLeft() == null)
331
{
332
return null;
333
}
334
else
335
{
336
return findNode(o, current.getSmallerLeft());
337
}
338
}
339
else
340
// equal
341
{
342
return current;
343
}
344
}
345
346
private BinarySearchTreeNode findLeftestLeaf(BinarySearchTreeNode current,
347
FatherNode upper)
348
{
349
if(current.getSmallerLeft() == null)
350
{
351
return current;
352
}
353
else
354
{
355
upper.setBstn(current);
356
return findLeftestLeaf(current.getSmallerLeft(), upper);
357
}
358
}
359
360
/*
361
* (non-Javadoc)
362
*
363
* @see binarySearchTree.IBinarySearchTree#remove(java.lang.Comparable)
364
*/
365
@SuppressWarnings("unchecked")
366
public Object remove(Comparable o)
367
{
368
if(this.root == null)
369
{
370
return null;
371
}
372
FatherNode upper = new FatherNode();
373
Direction direction = new Direction();
374
BinarySearchTreeNode bstn = findNode(o, this.root, upper, direction);
375
if(bstn == null)
376
{
377
return null;
378
}
379
else
380
{
381
if(bstn.getSmallerLeft() == null && bstn.getBiggerRight() == null)// no
382
// children
383
{
384
if(upper.getBstn() == null)// root
385
{
386
Object object = this.root.getObject();
387
this.root = null;
388
this.size--;
389
return object;
390
}
391
else
392
// not root
393
{
394
if(direction.getNumber() == 1)// left side to the leaf
395
// node
396
{
397
Object object = bstn.getObject();
398
upper.getBstn().setSmallerLeft(null);
399
this.size--;
400
return object;
401
}
402
else
403
// right side to the leaf node
404
{
405
Object object = bstn.getObject();
406
upper.getBstn().setBiggerRight(null);
407
this.size--;
408
return object;
409
}
410
}
411
}
412
else if(bstn.getSmallerLeft() == null)// one child on the right
413
// side, not leaf
414
{
415
if(upper.getBstn() == null)// root
416
{
417
Object object = this.root.getObject();
418
this.root = this.root.getBiggerRight();
419
this.size--;
420
return object;
421
}
422
else
423
// not root
424
{
425
if(direction.getNumber() == 1)// goes left side to reach
426
// it
427
{
428
Object object = bstn.getObject();
429
upper.getBstn().setSmallerLeft(bstn.getBiggerRight());
430
this.size--;
431
return object;
432
}
433
else
434
// goes right side to reach it
435
{
436
Object object = bstn.getObject();
437
upper.getBstn().setBiggerRight(bstn.getBiggerRight());
438
this.size--;
439
return object;
440
}
441
}
442
}
443
else if(bstn.getBiggerRight() == null)// one child on the left
444
// side, not leaf
445
{
446
if(upper.getBstn() == null)// root
447
{
448
Object object = this.root.getObject();
449
this.root = this.root.getSmallerLeft();
450
this.size--;
451
return object;
452
}
453
else
454
// not root
455
{
456
if(direction.getNumber() == 1)// goes left side to reach
457
// it
458
{
459
Object object = bstn.getObject();
460
upper.getBstn().setSmallerLeft(bstn.getSmallerLeft());
461
this.size--;
462
return object;
463
}
464
else
465
// goes right side to reach it
466
{
467
Object object = bstn.getObject();
468
upper.getBstn().setBiggerRight(bstn.getSmallerLeft());
469
this.size--;
470
return object;
471
}
472
}
473
}
474
else
475
// two children
476
{
477
if(upper.getBstn() == null)// root
478
{
479
FatherNode subUpper = new FatherNode();
480
BinarySearchTreeNode newNode = findLeftestLeaf(bstn
481
.getBiggerRight(), subUpper);
482
Object object = this.root.getObject();
483
newNode.setSmallerLeft(this.root.getSmallerLeft());
484
newNode.setBiggerRight(this.root.getBiggerRight());
485
if(subUpper.getBstn() == null)// only one level
486
{
487
newNode.setBiggerRight(null);
488
}
489
else
490
// more then one level
491
{
492
subUpper.getBstn().setSmallerLeft(null);
493
}
494
this.root = newNode;
495
this.size--;
496
return object;
497
}
498
else
499
{
500
if(direction.getNumber() == 1)// goes left side to reach
501
// it
502
{
503
FatherNode subUpper = new FatherNode();
504
BinarySearchTreeNode newNode = findLeftestLeaf(bstn
505
.getBiggerRight(), subUpper);
506
Object object = bstn.getObject();
507
newNode.setSmallerLeft(bstn.getSmallerLeft());
508
newNode.setBiggerRight(bstn.getBiggerRight());
509
if(subUpper == null)// only one level
510
{
511
newNode.setBiggerRight(null);
512
}
513
else
514
// more then one level
515
{
516
subUpper.getBstn().setSmallerLeft(null);
517
}
518
upper.getBstn().setSmallerLeft(newNode);
519
this.size--;
520
return object;
521
}
522
else
523
// goes right side to reach it
524
{
525
FatherNode subUpper = new FatherNode();
526
BinarySearchTreeNode newNode = findLeftestLeaf(bstn
527
.getBiggerRight(), subUpper);
528
Object object = bstn.getObject();
529
newNode.setSmallerLeft(bstn.getSmallerLeft());
530
newNode.setBiggerRight(bstn.getBiggerRight());
531
if(subUpper == null)// only one level
532
{
533
newNode.setBiggerRight(null);
534
}
535
else
536
// more then one level
537
{
538
subUpper.getBstn().setSmallerLeft(null);
539
}
540
upper.getBstn().setBiggerRight(newNode);
541
this.size--;
542
return object;
543
}
544
}
545
}
546
}
547
}
548
549
/*
550
* (non-Javadoc)
551
*
552
* @see binarySearchTree.IBinarySearchTree#find(java.lang.Comparable)
553
*/
554
@SuppressWarnings("unchecked")
555
public Object find(Comparable o)
556
{
557
if(this.root == null)
558
{
559
return null;
560
}
561
BinarySearchTreeNode bstn = findNode(o, this.root);
562
if(bstn == null)
563
{
564
return null;
565
}
566
else
567
{
568
return bstn.getObject();
569
}
570
}
571
572
/*
573
* (non-Javadoc)
574
*
575
* @see binarySearchTree.IBinarySearchTree#iterator()
576
*/
577
@SuppressWarnings("unchecked")
578
public Iterator iterator()
579
{
580
DoublyLinkedList dll = getDLL();
581
return dll.iterator();
582
}
583
584
private DoublyLinkedList getDLL()
585
{
586
DoublyLinkedList dll = new DoublyLinkedList();
587
fillDLL(dll, this.root);
588
return dll;
589
}
590
591
private void fillDLL(DoublyLinkedList dll, BinarySearchTreeNode current)
592
{
593
if(current == null)
594
{
595
return;
596
}
597
if(current.getSmallerLeft() == null && current.getBiggerRight() == null)
598
{
599
dll.add(current.getObject());
600
return;
601
}
602
if(current.getSmallerLeft() != null)
603
{
604
fillDLL(dll, current.getSmallerLeft());
605
}
606
dll.add(current.getObject());
607
if(current.getBiggerRight() != null)
608
{
609
fillDLL(dll, current.getBiggerRight());
610
}
611
}
612
}
613

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

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613
