forked from rahuldeve/Diversification-Dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
32BinarySearchTrees.txt
2008 lines (1179 loc) · 17.7 KB
/
32BinarySearchTrees.txt
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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Algorithms
ROBERT SEDGEWICK | KEVIN WAYNE
3.2 BINARY SEARCH TREES
BSTs
ordered operations
deletion
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNEFOURTH EDITIONAlgorithms3.2 BINARY SEARCH TREES
BSTs
ordered operations
deletion
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNEAlgorithmsBinary search trees
Definition. A BST is a binary tree in symmetric order.
A binary tree is either:
(cid:638)Empty.(cid:638)Two disjoint binary trees (left and right).
Symmetric order. Each node has a key,
and every nodes key is:
(cid:638)Larger than all keys in its left subtree.
(cid:638)Smaller than all keys in its right subtree.
root
a left link
a subtree
right child
of root
null links
Anatomy of a binary tree
parent of A and R
left link
of E
E
key
S
X
A
C
R
9
H
value
associated
with R
keys smaller than E
keys larger than E
Anatomy of a binary search tree
3
Binary search tree demo
Search. If less, go left; if greater, go right; if equal, search hit.
successful search for H
S
E
X
A
R
C
H
M
4
Binary search tree demo
Insert. If less, go left; if greater, go right; if null, insert.
insert G
S
E
X
A
R
C
H
G
M
5
BST representation in Java
Java definition. A BST is a reference to a root Node.
A Node is composed of four fields:
(cid:638)A Key and a Value.
(cid:638)A reference to the left and right subtree.
smaller keys
larger keys
private class Node
{
private Key key;
private Value val;
private Node left, right;
public Node(Key key, Value val)
{
this.key = key;
this.val = val;
}
}
Key and Value are generic types; Key is Comparable
BST
Node
key
val
left
right
BST with smaller keys
BST with larger keys
Binary search tree
6
BST implementation (skeleton)
root of BST
7
public class BST<Key extends Comparable<Key>, Value>{ private Node root; private class Node { /* see previous slide */ } public void put(Key key, Value val) { /* see next slides */ } public Value get(Key key) { /* see next slides */ } public void delete(Key key) { /* see next slides */ } public Iterable<Key> iterator() { /* see next slides */ }}BST search: Java implementation
Get. Return value corresponding to given key, or null if no such key.
public Value get(Key key)
{
Node x = root;
while (x != null)
{
int cmp = key.compareTo(x.key);
if (cmp < 0) x = x.left;
else if (cmp > 0) x = x.right;
else if (cmp == 0) return x.val;
}
return null;
}
Cost. Number of compares is equal to 1 + depth of node.
8
BST insert
Put. Associate value with key.
inserting L
Search for key, then two cases:
(cid:638)Key in tree reset value.
(cid:638)Key not in tree add new node.
S
X
R
E
A
C
H
M
P
search for L ends
at this null link
S
X
S
X
R
R
E
A
C
H
create new node
L
M
P
E
A
C
H
reset links
on the way up
M
L
P
Insertion into a BST
9
BST insert: Java implementation
Put. Associate value with key.
public void put(Key key, Value val)
{ root = put(root, key, val); }
private Node put(Node x, Key key, Value val)
{
if (x == null) return new Node(key, val);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else if (cmp == 0)
x.val = val;
return x;
}
Cost. Number of compares is equal to 1 + depth of node.
10
concise, but tricky, recursive code;read carefully!Tree shape
(cid:638)Many BSTs correspond to same set of keys.
(cid:638)Number of compares for search/insert is equal to 1 + depth of node.
typical case
best case
X
R
A
E
H
S
X
best case
H
C
S
A
E
R
X
typical case
E
A
R
C
H
S
X
C
S
A
E
R
X
typical case
A
A
S
X
E
R
C
H
worst case
C
E
H
R
S
H
C
S
E
A
A
R
C
H
C
E
worst case
H
R
S
X
BST possibilities
Bottom line. Tree shape depends on order of insertion.
worst case
A
X
C
E
H
R
BST possibilities
11
BST insertion: random order visualization
Ex. Insert keys in random order.
12
Sorting with a binary heap
Q. What is this sorting algorithm?
0. Shuffle the array of keys.
1. Insert all keys into a BST.
2. Do an inorder traversal of BST.
A. It's not a sorting algorithm (if there are duplicate keys)!
Q. OK, so what if there are no duplicate keys?
Q. What are its properties?
13
Correspondence between BSTs and quicksort partitioning
P
H
T
D
O
S
U
A
E
I
Y
C
M
L
Remark. Correspondence is 11 if array has no duplicate keys.
14
BSTs: mathematical analysis
Proposition. If N distinct keys are inserted into a BST in random order,
the expected number of compares for a search/insert is ~ 2 ln N.
Pf. 11 correspondence with quicksort partitioning.
Proposition. [Reed, 2003] If N distinct keys are inserted in random order,
expected height of tree is ~ 4.311 ln N.
How Tall is a Tree?
How Tall is a Tree?
Bruce Reed
CNRS, Paris, France
reed@moka.ccr.jussieu.fr
Bruce Reed
CNRS, Paris, France
reed@moka.ccr.jussieu.fr
ABSTRACT
Let H~ be the height of a random binary search tree on n
nodes. We show that there exists constants a = 4.31107...
and/3 = 1.95... such that E(H~) = c~logn -/31oglogn +
O(1), We also show that Var(H~) = O(1).
Categories and Subject Descriptors
But... Worst-case height is N.
E.2 [Data Structures]: Trees
1. THE RESULTS
[ exponentially small chance when keys are inserted in random order ]
A binary search tree is a binary tree to each node of which
we have associated a key; these keys axe drawn from some
totally ordered set and the key at v cannot be larger than
the key at its right child nor smaller than the key at its left
child. Given a binary search tree T and a new key k, we
ABSTRACT
Let H~ be the height of a random binary search tree on n
nodes. We show that there exists constants a = 4.31107...
and/3 = 1.95... such that E(H~) = c~logn -/31oglogn +
O(1), We also show that Var(H~) = O(1).
Categories and Subject Descriptors
E.2 [Data Structures]: Trees
1. THE RESULTS
A binary search tree is a binary tree to each node of which
we have associated a key; these keys axe drawn from some
totally ordered set and the key at v cannot be larger than
the key at its right child nor smaller than the key at its left
THEOREM 1. E(H~) = ~logn -/31oglogn + O(1) and
Var(Hn) = O(1) .
Remark By the definition of a,
3~
7"g~" The first defi-
nition given is more suggestive of why this value is correct,
as we will see.
For more information on random binary search trees, one
may consult [6],[7], [1], [2], [9], [4], and [8].
Remark After I announced these results, Drmota(unpublished)
developed an alternative proof of the fact that Var(Hn) =
O(1) using completely different techniques. As our two
proofs illuminate different aspects of the problem, we have
decided to submit the journal versions to the same journal
Var(Hn) = O(1) .
Remark By the definition of a,
nition given is more suggestive of why this value is correct,
/3 =
as we will see.
For more information on random binary search trees, one
may consult [6],[7], [1], [2], [9], [4], and [8].
Remark After I announced these results, Drmota(unpublished)
developed an alternative proof of the fact that Var(Hn) =
O(1) using completely different techniques. As our two
proofs illuminate different aspects of the problem, we have
decided to submit the journal versions to the same journal
and asked that they be published side by side.
purpose of this note to prove that for /3 -- 12 + ~ ,
3
have:
purpose of this note to prove that for /3 -- 12 + ~ ,
have:
THEOREM 1. E(H~) = ~logn -/31oglogn + O(1) and
15
ST implementations: summary
implementation
implementation
sequential search
(unordered list)
binary search
(ordered array)
BST
guarantee
guarantee
average case
average case
search
insert
search hit
insert
12 N
lg N
N
12 N
N
lg N
N
N
N
N
operations
operations
on keys
on keys
equals()
compareTo()
1.39 lg N
1.39 lg N
compareTo()
Why not shuffle to ensure a (probabilistic) guarantee of 4.311 ln N?
16
3.2 BINARY SEARCH TREES
BSTs
ordered operations
deletion
http://algs4.cs.princeton.edu
ROBERT SEDGEWICK | KEVIN WAYNEAlgorithmsMinimum and maximum
Minimum. Smallest key in table.
Maximum. Largest key in table.
min()
min
E
S
max()max
X
A
C
H
R
M
Examples of BST order queries
Q. How to find the min / max?
18
Floor and ceiling
Floor. Largest key a given key.
Ceiling. Smallest key a given key.
floor(G)
min()
A
S
max()
X
ceiling(Q)
E
C
H
R
M
floor(D)
Examples of BST order queries
Q. How to find the floor / ceiling?
19
Computing the floor
Case 1. [k equals the key in the node]
The floor of k is k.
Case 2. [k is less than the key in the node]
The floor of k is in the left subtree.
Case 3. [k is greater than the key in the node]
The floor of k is in the right subtree
(if there is any key k in right subtree);
otherwise it is the key in the node.
finding floor(G)
finding floor(G)
finding floor(G)
A
A
A
A
A
A
E
E
E
C
C
C
H
H
H
E
E
E
C
C
C
H
H
H
G is greater than E so
G is greater than E so
G is greater than E so
floor(G) could be
floor(G) could be
floor(G) could be
on the right
on the right
on the right
E
E
E
A
A
A
C
C
C
H
H
H
floor(G)in left
floor(G)in left
floor(G)in left
subtree is null
subtree is null
subtree is null
E
E
E
A
A
A
result
result
result
H
H
H
C
C
C
S
S
S
X
X
X
R
R
R
M
M
M
G is less than S so
G is less than S so
G is less than S so
floor(G) must be
floor(G) must be
floor(G) must be
on the left
on the left
on the left
S
S
S
X
X
X
S
S
S
X
X
X
S
S
S
X
X
X
R
R
R
R
R
R
R
R
R
M
M
M
M
M
M
M
M
M
Computing the floor function
Computing the floor function
Computing the floor function
20
Computing the floor
public Key floor(Key key)
{
Node x = floor(root, key);
if (x == null) return null;
return x.key;
}
private Node floor(Node x, Key key)
{
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp == 0) return x;
if (cmp < 0) return floor(x.left, key);
Node t = floor(x.right, key);
if (t != null) return t;
else return x;
}
finding floor(G)
A
A
E
C
H
E
C
H
G is greater than E so
floor(G) could be
on the right
E
A
C
H
floor(G)in left
subtree is null
E