Heaps and Multi-way Search Trees

 
Prepared By Keshav Tambre
 
Dept. Of  IT
 
Data Structures
 
1
 
Heaps & Multi-way Search Trees
 
Unit Objectives:
To understand basic concepts of heaps, types of
heaps & applications.
To understand implementation of different
heaps.
To understand various indexing techniques.
To understand difference between B trees & B+
trees.
 
Dept. Of  IT
 
Data Structures
 
2
 
Binary Heap
A priority queue data structure
 
A 
binary heap
 is a binary tree with two
properties
Heap Structure property
Heap Order property
 
Dept. Of  IT
 
Data Structures
 
3
 
Dept. Of  IT
 
Data Structures
 
4
 
Structure Property
 
A binary heap is a complete binary tree
Each level (except possibly the bottom most level) is
completely filled
The bottom most level may be partially filled
(from left to right)
 
Height of a complete binary tree with N
elements is
 
4
 
Heap-order Property
 
Heap-order property (for a “MinHeap”)
For every node X, key(parent(X)) ≤ key(X)
Except root node, which has no parent
Thus, minimum key always at root
Alternatively, for a “MaxHeap”, always keep the
maximum key at the root
Insert and deleteMin must maintain heap-
order property
 
Dept. Of  IT
 
Data Structures
 
5
Heap Order Property
 
 
 
 
 
 
 
Duplicates are allowed
Dept. Of  IT
Data Structures
6
Minimum
element
Binary Heap Example
Dept. Of  IT
Data Structures
7
Every level
(except last)
saturated
 
Array representation:
N=10
Implementing Complete Binary
Trees as Arrays
Given element at position i in the array
Left child(i) = at position 2i
Right child(i) = at position 2i + 1
Parent(i) = at position
Dept. Of  IT
Data Structures
8
 
2i
 
2i + 1
 
i
 
Heap Insert
 
Insert new element into the heap at the next
available slot (“hole”)
According to maintaining a complete binary tree
Then, “percolate” the element up the heap
while heap-order property not satisfied
 
Dept. Of  IT
 
Data Structures
 
9
Heap Insert: Example
Dept. Of  IT
Data Structures
10
Insert 14:
h
o
l
e
 
14
P
e
r
c
o
l
a
t
i
n
g
 
U
p
 
Dept. Of  IT
 
Data Structures
 
11
 
Heap Insert: Example
 
11
 
Insert 14:
 
(1)
14 vs. 31
 
h
o
l
e
 
14
 
P
e
r
c
o
l
a
t
i
n
g
 
U
p
 
14
 
Dept. Of  IT
 
Data Structures
 
12
 
Heap Insert: Example
 
12
 
Insert 14:
 
(1)
14 vs. 31
 
(2)
14 vs. 21
 
P
e
r
c
o
l
a
t
i
n
g
 
U
p
 
h
o
l
e
 
14
 
14
 
14
Dept. Of  IT
Data Structures
13
Heap Insert: Example
13
Insert 14:
(1)
14 vs. 31
(3)
14 vs. 13
 
Heap order prop
 
Structure prop
h
o
l
e
14
P
e
r
c
o
l
a
t
i
n
g
 
U
p
14
(2)
14 vs. 21
Path of percolation up
 
Heap DeleteMin
 
Minimum element is always at the root
Heap decreases by one in size
Move last element into hole at root
Percolate down
 while heap-order property
not satisfied
 
Dept. Of  IT
 
Data Structures
 
14
Heap DeleteMin: Example
Dept. Of  IT
Data Structures
15
Make this
position
empty
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
Dept. Of  IT
Data Structures
16
Heap DeleteMin: Example
16
 
Is 31 > min(14,16)?
Yes - swap 31 with min(14,16)
Make this
position
empty
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
Heap DeleteMin: Example
Dept. Of  IT
Data Structures
17
 
Is 31 > min(19,21)?
Yes - swap 31 with min(19,21)
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
31
Dept. Of  IT
Data Structures
18
Heap DeleteMin: Example
18
 
Is 31 > min(65,26)?
Yes - swap 31 with min(65,26)
Is 31 > min(19,21)?
Yes - swap 31 with min(19,21)
Percolating down…
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
31
31
 
Heap DeleteMin: Example
 
Dept. Of  IT
 
Data Structures
 
19
 
Percolating down…
 
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
 
31
Dept. Of  IT
Data Structures
20
Heap DeleteMin: Example
20
 
Heap order prop
 
Structure prop
P
e
r
c
o
l
a
t
i
n
g
 
d
o
w
n
31
 
Building a Heap
 
Construct heap from initial set of N items
Method 1
Perform N inserts
O(N log
2 
N) worst-case
Method 2
 (use 
buildHeap()
)
Randomly populate initial heap with structure property
Perform a percolate-down from each internal node
(H[size/2] to H[1])
To take care of heap order property
 
Dept. Of  IT
 
Data Structures
 
21
 
Binary Heap
 
Example:
 
Data arrives to be heaped in the order:
 54, 87, 27, 67, 19, 31, 29, 18, 32
 
Dept. Of  IT
 
Data Structures
 
22
 
Binary Heap
 
54, 87, 27, 67, 19, 31, 29, 18, 32
Method 1:
 
23
54
 
Binary Heap
 
54, 87, 27, 67, 19, 31, 29, 18, 32
 
24
31
 
Binary Heap
 
54, 87, 27, 67, 19, 31, 29, 18, 32
 
25
18
32
BuildHeap Example
Dept. Of  IT
Data Structures
26
 Arbitrarily assign elements to heap nodes
 Structure property satisfied
 Heap order property violated 
 Leaves are all valid heaps (implicit)
Input:
  { 150, 80, 40, 30,10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130 }
Method 2
:
Leaves are all
valid heaps
(implicitly)
So, let us look at each
internal node,
from bottom to top,
and fix if necessary
Dept. Of  IT
Data Structures
27
BuildHeap Example
27
 Randomly initialized heap
 Structure property satisfied
 Heap order property violated 
 Leaves are all valid heaps (implicit)
Nothing
to do
Swap
with left
child
Dept. Of  IT
Data Structures
28
BuildHeap Example
28
Dotted lines show path of percolating down
Swap
with right
child
Nothing
to do
BuildHeap Example
Dept. Of  IT
Data Structures
29
Dotted lines show path of percolating down
Nothing
to do
Swap with
right child
& then with 60
BuildHeap Example
Dept. Of  IT
Data Structures
30
Dotted lines show path of percolating down
 
F
i
n
a
l
 
H
e
a
p
 
Applications of Heaps.
 
Operating system scheduling / Priority Queue.
Process jobs by priority
Graph algorithms.
Find shortest path
Selection Problem.
Heap Sort.
 
Dept. Of  IT
 
Data Structures
 
31
 
An Application:
The Selection Problem
 
Given a list of n elements, find the k
th
 smallest
element
 
Algorithm 1:
Sort the list
Pick the k
th
 element
A better algorithm:
Use a binary heap (minheap)
 
Dept. Of  IT
 
Data Structures
 
32
 
Selection using a MinHeap
 
Input: n elements
Algorithm:
1.
buildHeap(n)
2.
Perform k deleteMin() operations
3.
Report the k
th
 deleteMin output
 
 
Dept. Of  IT
 
Data Structures
 
33
Heapsort
 
(1)   Build a binary heap of N elements
the minimum element is at the top of the heap
 
(2)   Perform N 
DeleteMin 
operations
the elements are extracted in sorted order
 
Dept. Of  IT
Data Structures
34
 
Build Heap
 
Input: N elements
Output: A heap with heap-order property
Method 1: obviously, N successive insertions
Complexity: 
O(NlogN)
 worst case
 
Dept. Of  IT
 
Data Structures
 
35
Heapsort – Running Time Analysis
 
(1) Build a binary heap of N elements
repeatedly insert N elements 
 O(N log N) time
 
(2) Perform N 
DeleteMin 
operations
Each 
DeleteMin 
operation takes O(log N) 
 
O(N log N)
 
 
Total time complexity: O(N log N)
 
Dept. Of  IT
Data Structures
36
Heapsort
 
Observation: after each deleteMin, the size of heap shrinks by
1
We can use the last cell just freed up to store the element that was
just deleted
    
 after the last deleteMin, the array will contain the elements in
decreasing sorted order
 
To sort the elements in the 
decreasing order
, use a 
min heap
To sort the elements in the 
increasing order
, use a 
max heap
Dept. Of  IT
Data Structures
37
 
Heap-Sort Example
 
Dept. Of  IT
 
Data Structures
 
38
 
S
o
r
t
 
i
n
 
i
n
c
r
e
a
s
i
n
g
 
o
r
d
e
r
:
 
u
s
e
 
m
a
x
 
h
e
a
p
 
D
e
l
e
t
e
 
9
7
 
Another Heapsort Example
 
Dept. Of  IT
 
Data Structures
 
39
 
D
e
l
e
t
e
 
1
6
 
D
e
l
e
t
e
 
1
4
 
D
e
l
e
t
e
 
1
0
 
D
e
l
e
t
e
 
9
 
D
e
l
e
t
e
 
8
 
Example (cont’d)
 
Dept. Of  IT
 
Data Structures
 
40
 
Other Types of Heaps
 
Binomial Heaps
d-Heaps
Leftist Heaps
Skew Heaps
Fibonacci Heaps
 
Dept. Of  IT
 
Data Structures
 
41
B-Trees
 
Dept. Of  IT
 
Data Structures
 
42
 
Definition of a B-tree
 
A B-tree of order 
m
 is an 
m
-way tree (i.e., a tree where each
node may have up to 
m
 children) in which:
1.
 
the number of keys in each non-leaf node is one less than
the number of its children and these keys partition the
keys in the children in the fashion of a search tree
2.
 
all leaves are on the same level
3.
 
all non-leaf nodes except the root have at least 
m 
/ 2
children
4.
 
the root is either a leaf node, or it has from two to 
m
children
5.
 
a leaf node contains no more than 
m
 – 1 keys
The number 
m
 should always be odd
 
Dept. Of  IT
 
Data Structures
 
43
 
An example B-Tree
 
Dept. Of  IT
 
Data Structures
 
44
51
62
42
6
12
26
55
60
70
64
90
45
1
2
4
7
8
13
15
18
25
27
29
46
48
53
 
A
 
B
-
t
r
e
e
 
o
f
o
r
d
e
r
 
5
c
o
n
t
a
i
n
i
n
g
 
2
6
i
t
e
m
s
 
Note that all the leaves are at the same level
Note that all the leaves are at the same level
 
Constructing a B-tree
 
Suppose we start with an empty B-tree and
keys arrive in the following order:1  12  8  2  25
5  14  28  17  7  52  16  48  68  3  26  29  53  55
45
We want to construct a B-tree of order 5
The first four items go into the root:
 
To put the fifth item in the root would violate
condition 5
Therefore, when 25 arrives, pick the middle
key to make a new root
 
Dept. Of  IT
 
Data Structures
 
45
1
2
8
12
Constructing a B-tree (contd.)
1  12  8  2  25  5  14  28  17  7  52  16  48  68  3  26  29  53  55  45
Dept. Of  IT
Data Structures
46
1
2
8
12
25
Constructing a B-tree (contd.)
1  12  8  2  25  5  14  28  17  7  52  16  48  68  3  26  29  53  55  45
Dept. Of  IT
Data Structures
47
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
8
17
12
14
25
28
1
2
6
Constructing a B-tree (contd.)
1  12  8  2  25  5  14  28  17  7  52  16  48  68  3  26  29  53  55  45
Dept. Of  IT
Data Structures
48
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
3
8
17
48
52
53
55
68
25
26
28
29
1
2
6
7
12
14
16
 
Constructing a B-tree (contd.)
1  12  8  2  25  5  14  28  17  7  52  16  48  68  3  26  29  53  55  45
 
Dept. Of  IT
 
Data Structures
 
49
17
3
8
28
48
1
2
6
7
12
14
16
52
53
55
68
25
26
29
45
 
Inserting into a B-Tree
 
If Attempt to insert the new key into a leaf
 this would result in that leaf becoming too big, split
the leaf into two, promoting the middle key to the
leaf’s parent
If this would result in the parent becoming too big,
split the parent into two, promoting the middle key
This strategy might have to be repeated all the way
to the top
If necessary, the root is split in two and the middle
key is promoted to a new root, making the tree one
level higher
 
Dept. Of  IT
 
Data Structures
 
50
 
Exercise in Inserting a B-Tree
 
Insert the following keys to a 5-way B-tree:
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4,
31, 35, 56
 
 
Dept. Of  IT
 
Data Structures
 
51
 
Removal from a B-tree
 
During insertion, the key always goes 
into
 a 
leaf
.  For
deletion we wish to remove 
from
 a leaf.  There are
three possible ways we can do this:
1 - If the key is already in a leaf node, and removing it
doesn’t cause that leaf node to have too few keys,
then simply remove the key to be deleted.
2 - If the key is 
not
 in a leaf then it is guaranteed (by
the nature of a B-tree) that its predecessor or
successor will be in a leaf -- in this case we can delete
the key and promote the predecessor or successor
key to the non-leaf deleted key’s position.
 
Dept. Of  IT
 
Data Structures
 
52
 
Removal from a B-tree (2)
 
If (1) or (2) lead to a leaf node containing less than the
minimum number of keys then we have to look at the siblings
immediately adjacent to the leaf in question:
3: if one of them has more than the min. number of keys
then we can promote one of its keys to the parent and
take the parent key into our lacking leaf
4: if neither of them has more than the min. number of
keys then the lacking leaf and one of its neighbours can be
combined with their shared parent (the opposite of
promoting a key) and the new leaf will have the correct
number of keys; if this step leave the parent with too few
keys then we repeat the process up to the root itself, if
required
 
Dept. Of  IT
 
Data Structures
 
53
Type #1: Simple leaf deletion
Dept. Of  IT
Data Structures
54
Delete 2:  Since there are enough
keys in the node, just delete it
Assuming a 5-way
B-Tree, as before...
Note when printed: this slide is animated
Type #2: Simple non-leaf deletion
Dept. Of  IT
Data Structures
55
Delete 52
Borrow the predecessor
or (in this case) successor
56
56
Note when printed: this slide is animated
Type #4: Too few keys in node and
its siblings
Dept. Of  IT
Data Structures
56
Delete 72
Too few
keys!
Note when printed: this slide is animated
 
Type #4: Too few keys in node and
its siblings
 
Dept. Of  IT
 
Data Structures
 
57
 
Note when printed: this slide is animated
Type #3: Enough siblings
Dept. Of  IT
Data Structures
58
Delete 22
Note when printed: this slide is animated
 
Type #3: Enough siblings
 
Dept. Of  IT
 
Data Structures
 
59
12
12
29
29
7
7
9
9
15
15
31
31
 
Note when printed: this slide is animated
 
Exercise in Removal from a B-Tree
 
Given 5-way B-tree created by these data (last
exercise):
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4,
31, 35, 56
 
Add these further keys: 2, 6,12
 
Delete these keys: 4, 5, 7, 3, 14
 
 
Dept. Of  IT
 
Data Structures
 
60
Slide Note
Embed
Share

Explore the concepts of heaps, types of heaps, and their applications, along with the implementation of different heaps and indexing techniques. Learn about the differences between B-trees and B+-trees in the context of data structures.

  • Heaps
  • Search Trees
  • Data Structures
  • B-trees
  • B+-trees

Uploaded on Aug 05, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Heaps & Multi-way Search Trees Prepared By Keshav Tambre Dept. Of IT Data Structures 1

  2. Unit Objectives: To understand basic concepts of heaps, types of heaps & applications. To understand implementation of different heaps. To understand various indexing techniques. To understand difference between B trees & B+ trees. Dept. Of IT Data Structures 2

  3. Binary Heap A priority queue data structure A binary heap is a binary tree with two properties Heap Structure property Heap Order property Dept. Of IT Data Structures 3

  4. Structure Property A binary heap is a complete binary tree Each level (except possibly the bottom most level) is completely filled The bottom most level may be partially filled (from left to right) Height of a complete binary tree with N elements is N 2 log Dept. Of IT Data Structures 4 4

  5. Heap-order Property Heap-order property (for a MinHeap ) For every node X, key(parent(X)) key(X) Except root node, which has no parent Thus, minimum key always at root Alternatively, for a MaxHeap , always keep the maximum key at the root Insert and deleteMin must maintain heap- order property Dept. Of IT Data Structures 5

  6. Heap Order Property Minimum element Duplicates are allowed Dept. Of IT Data Structures 6

  7. Binary Heap Example N=10 Every level (except last) saturated Array representation: Dept. Of IT Data Structures 7

  8. Implementing Complete Binary Trees as Arrays Given element at position i in the array Left child(i) = at position 2i Right child(i) = at position 2i + 1 Parent(i) = at position / i 2 i 2i 2i + 1 i/2 Dept. Of IT Data Structures 8

  9. Heap Insert Insert new element into the heap at the next available slot ( hole ) According to maintaining a complete binary tree Then, percolate the element up the heap while heap-order property not satisfied Dept. Of IT Data Structures 9

  10. Percolating Up Heap Insert: Example Insert 14: hole 14 Dept. Of IT Data Structures 10

  11. Percolating Up Heap Insert: Example (1) Insert 14: 14 vs. 31 14 hole 14 Dept. Of IT Data Structures 11 11

  12. Percolating Up Heap Insert: Example (1) Insert 14: 14 vs. 31 14 hole 14 (2) 14 vs. 21 14 Dept. Of IT Data Structures 12 12

  13. Percolating Up Heap Insert: Example (1) Insert 14: 14 vs. 31 hole 14 (2) 14 vs. 21 Heap order prop Structure prop (3) 14 14 vs. 13 Path of percolation up Dept. Of IT Data Structures 13 13

  14. Heap DeleteMin Minimum element is always at the root Heap decreases by one in size Move last element into hole at root Percolate down while heap-order property not satisfied Dept. Of IT Data Structures 14

  15. Percolating down Heap DeleteMin: Example Make this position empty Dept. Of IT Data Structures 15

  16. Percolating down Heap DeleteMin: Example Copy 31 temporarily here and move it down Make this position empty Is 31 > min(14,16)? Yes - swap 31 with min(14,16) Dept. Of IT Data Structures 16 16

  17. Percolating down Heap DeleteMin: Example 31 Is 31 > min(19,21)? Yes - swap 31 with min(19,21) Dept. Of IT Data Structures 17

  18. Percolating down Heap DeleteMin: Example 31 31 Is 31 > min(65,26)? Yes - swap 31 with min(65,26) Is 31 > min(19,21)? Yes - swap 31 with min(19,21) Percolating down Dept. Of IT Data Structures 18 18

  19. Percolating down Heap DeleteMin: Example 31 Percolating down Dept. Of IT Data Structures 19

  20. Percolating down Heap DeleteMin: Example 31 Heap order prop Structure prop Dept. Of IT Data Structures 20 20

  21. Building a Heap Construct heap from initial set of N items Method 1 Perform N inserts O(N log2 N) worst-case Method 2 (use buildHeap()) Randomly populate initial heap with structure property Perform a percolate-down from each internal node (H[size/2] to H[1]) To take care of heap order property Dept. Of IT Data Structures 21

  22. Binary Heap Example: Data arrives to be heaped in the order: 54, 87, 27, 67, 19, 31, 29, 18, 32 Dept. Of IT Data Structures 22

  23. Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32 Method 1: 54 87 87 54 54 54 27 87 87 87 87 54 27 67 27 67 27 67 54 54 19 23

  24. Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32 87 87 67 27 67 27 19 54 19 31 54 24

  25. Binary Heap 54, 87, 27, 67, 19, 31, 29, 18, 32 87 87 67 31 67 31 19 27 29 54 19 27 29 54 18 32 25

  26. BuildHeap Example Input: { 150, 80, 40, 30,10, 70, 110, 100, 20, 90, 60, 50, 120, 140, 130 } Method 2: Leaves are all valid heaps (implicitly) So, let us look at each internal node, from bottom to top, and fix if necessary Arbitrarily assign elements to heap nodes Structure property satisfied Heap order property violated Leaves are all valid heaps (implicit) Dept. Of IT Data Structures 26

  27. BuildHeap Example Swap with left child Nothing to do Randomly initialized heap Structure property satisfied Heap order property violated Leaves are all valid heaps (implicit) Dept. Of IT Data Structures 27 27

  28. BuildHeap Example Swap with right child Nothing to do Dotted lines show path of percolating down Dept. Of IT Data Structures 28 28

  29. BuildHeap Example Swap with right child & then with 60 Nothing to do Dotted lines show path of percolating down Dept. Of IT Data Structures 29

  30. BuildHeap Example Swap path Final Heap Dotted lines show path of percolating down Dept. Of IT Data Structures 30

  31. Applications of Heaps. Operating system scheduling / Priority Queue. Process jobs by priority Graph algorithms. Find shortest path Selection Problem. Heap Sort. Dept. Of IT Data Structures 31

  32. An Application: The Selection Problem Given a list of n elements, find the kth smallest element Algorithm 1: Sort the list Pick the kth element A better algorithm: Use a binary heap (minheap) Dept. Of IT Data Structures 32

  33. Selection using a MinHeap Input: n elements Algorithm: 1. buildHeap(n) 2. Perform k deleteMin() operations 3. Report the kth deleteMin output Dept. Of IT Data Structures 33

  34. Heapsort (1) Build a binary heap of N elements the minimum element is at the top of the heap (2) Perform N DeleteMin operations the elements are extracted in sorted order Dept. Of IT Data Structures 34

  35. Build Heap Input: N elements Output: A heap with heap-order property Method 1: obviously, N successive insertions Complexity: O(NlogN) worst case Dept. Of IT Data Structures 35

  36. Heapsort Running Time Analysis (1) Build a binary heap of N elements repeatedly insert N elements O(N log N) time (2) Perform N DeleteMin operations Each DeleteMin operation takes O(log N) O(N log N) Total time complexity: O(N log N) Dept. Of IT Data Structures 36

  37. Heapsort Observation: after each deleteMin, the size of heap shrinks by 1 We can use the last cell just freed up to store the element that was just deleted after the last deleteMin, the array will contain the elements in decreasing sorted order To sort the elements in the decreasing order, use a min heap To sort the elements in the increasing order, use a max heap Dept. Of IT Data Structures 37

  38. Heap-Sort Example Sort in increasing order: use max heap Delete 97 Dept. Of IT Data Structures 38

  39. Another Heapsort Example Delete 16 Delete 14 Delete 10 Delete 9 Delete 8 Dept. Of IT Data Structures 39

  40. Example (contd) Dept. Of IT Data Structures 40

  41. Other Types of Heaps Binomial Heaps d-Heaps Leftist Heaps Skew Heaps Fibonacci Heaps Dept. Of IT Data Structures 41

  42. B-Trees Dept. Of IT Data Structures 42

  43. Definition of a B-tree A B-tree of order m is an m-way tree (i.e., a tree where each node may have up to m children) in which: 1. the number of keys in each non-leaf node is one less than the number of its children and these keys partition the keys in the children in the fashion of a search tree 2. all leaves are on the same level 3. all non-leaf nodes except the root have at least m / 2 children 4. the root is either a leaf node, or it has from two to m children 5. a leaf node contains no more than m 1 keys The number m should always be odd Dept. Of IT Data Structures 43

  44. An example B-Tree A B-tree of order 5 containing 26 items 26 6 12 42 51 62 1 2 4 7 8 13 15 18 25 27 29 45 46 48 53 55 60 64 70 90 Note that all the leaves are at the same level Dept. Of IT Data Structures 44

  45. Constructing a B-tree Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 We want to construct a B-tree of order 5 The first four items go into the root: 1 2 8 12 To put the fifth item in the root would violate condition 5 Therefore, when 25 arrives, pick the middle key to make a new root Dept. Of IT Data Structures 45

  46. Constructing a B-tree (contd.) 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 8 1 2 12 25 6, 14, 28 get added to the leaf nodes: 8 1 2 6 12 14 25 28 Dept. Of IT Data Structures 46

  47. Constructing a B-tree (contd.) 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 8 17 1 2 6 12 14 25 28 7, 52, 16, 48 get added to the leaf nodes 8 17 1 2 6 7 12 14 16 25 28 48 52 Dept. Of IT Data Structures 47

  48. Constructing a B-tree (contd.) 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves 3 8 17 48 1 2 6 7 12 14 16 25 26 28 29 52 53 55 68 Adding 45 causes a split of 25 26 28 29 and promoting 28 to the root then causes the root to split Dept. Of IT Data Structures 48

  49. Constructing a B-tree (contd.) 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45 17 3 8 28 48 1 2 6 7 12 14 16 25 26 29 45 52 53 55 68 Dept. Of IT Data Structures 49

  50. Inserting into a B-Tree If Attempt to insert the new key into a leaf this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf s parent If this would result in the parent becoming too big, split the parent into two, promoting the middle key This strategy might have to be repeated all the way to the top If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher Dept. Of IT Data Structures 50

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#