Data Structures in Java
Lecture 11: B-Trees.
10/14/2015 Daniel Bauer
Data Structures in Java Lecture 11: B-Trees. 10/14/2015 Daniel - - PowerPoint PPT Presentation
Data Structures in Java Lecture 11: B-Trees. 10/14/2015 Daniel Bauer Homework, Midterm etc. Homework 3 is out! Due: Friday October 23rd. Jarvis tests in preparation. Homework 2 grading is almost done. Make sure to only submit
Lecture 11: B-Trees.
10/14/2015 Daniel Bauer
Jarvis tests in preparation.
markdown .md) for theory. Put the the main directory for each homework homework-<youruni>/3/ and not homework-<uni>/3/src/
findMax) efficient, we need to maintain a balanced tree:
between left and right subtree is at most 1.
r
Tl Tr
search trees.
1
20
7
21 23
9
10
2 3
. 4-ary search tree: Nodes have 1,2, or 3 data items and 0 to 4 children.
All leaves have the same depth. (height of the left and right subtree is always identical)
r
s
r r
s t x<r x>r <r r<x<s x>s <r r<x<s s<x<t >t
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65 75 73 79
contains(55)
53 60 55
at most O(height(T)) = O(log N) references.
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65 75 73 79
insert(34)
should contain X, add it.
53 38
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65 75 73 79
insert(34)
should contain X, add it.
53 38 34
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(72)
and right of it.
53 34
70
73 75 79
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(72)
and right of it.
53 34
70
72 75 79 73
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(72)
and right of it.
53 34
70
75 79
72 73
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(80)
53 34 75 79
73
72
80
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(90)
be found.
insertion is O(log N).
34 72 75 79 80 73
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(90)
be found.
insertion is O(log N).
34 72 73 75 79 80 90
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
insert(90)
be found.
insertion is O(log N).
34 72 73 75 90 80 79
70
53 38 27 60 25 16 36 33 46 41 48 59 55 68 65
insert(90)
be found.
insertion is O(log N).
34 72 73 75 90 80 79
70
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
remove(80)
34 72 73 75 80 90 79
53 38 27 70 60 25 16 36 33 46 41 48 59 55 68 65
remove(80)
34 72 73 75 90 79
53
38 27 70 60 25 16 36 33 46 41 48 59
55
68 65
remove(53)
item w. Replace v with w. Remove w from its original position recursively.
34 72 73 75 90 79
38 27 70 60 25 16 36 33 46 41 48 59
55
68 65
remove(53)
item w. Replace v with w. Remove w from its original position recursively.
34 72 73 75 90 79
38 27 70 60 25 16 36 33 46 41 48 59 68 65
remove(59)
well formed.
by moving item from one of t’s siblings.
34 72 73 75 90 79 55
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(59)
well formed.
by moving item from one of t’s siblings.
34 72 73 75 90 79 55
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(59)
well formed.
by moving item from one of t’s siblings.
34 72 73 75 90 79 55
What if no sibling is a 3 or 4 node?
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(72)
siblings:
34
72
73 75 90 79 55
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(72)
siblings:
34 73 75 90 79 55
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(72)
siblings:
34 73 75 90 79 55
38 27 70 60 25 16 36 33 46 41 48 68 65
remove(72)
siblings:
34 73 75 90 79 55
All modifications to fix the tree are local and therefore O(c). Remove runs in O(log N).
M-ary search trees.
children and contains values.
(databases, file systems)
38 27 25 16 36 33 46 41 48 34
CPU registers CPU caches Main Memory Disk Storage < 1KB 8MB 64GB (or less) >500GB 5 ns 10 ns 100 ns 5 ms = 5 x 106 ns Typical Memory Size Typical Access Times Memory access is much faster than disk access. 200 accesses/second
represented as a binary search tree:
access.
unbalanced BST:
will be around log N.
memory in single disk access. Find the next reference using binary search.
is about because the minimum number
38 25 16 36 46 41 48 27 33 34
that can be read from the disk.
512 byte (modern drives: 4096 byte)
sectors (defined by the operating system). Typical block sizes are 1KB, 2KB, 4KB, 8KB.
M block addresses of other trees (a 8 byte pointer each).
M * 8 bytes (M-1)*256 bytes …
6 accesses / 6 disk accesses per second = 1 second
right leaf.
book).
Weiss, Data Structures and Algorithms in Java, 3rd Ed.