Presentation is loading. Please wait.

Presentation is loading. Please wait.

(edited by Nadia Al-Ghreimil)

Similar presentations


Presentation on theme: "(edited by Nadia Al-Ghreimil)"— Presentation transcript:

1 (edited by Nadia Al-Ghreimil)
B-Trees By Samira Lahmar (edited by Nadia Al-Ghreimil)

2 B-Trees A B-tree is a special type of tree that has properties that make it useful for storing and retrieving information. Like a binary search tree, finding an element stored in B-tree requires searching only a single path between the root and a leaf. Like an AVL tree, the insertion and deletion algorithms guarantee that the longest path between the root and a leaf is O(log2n). The insertion and deletion algorithms are however, quite different from those used for an AVL tree. Unlike a binary tree, each node may contain many elements and may have many children. Because of this and because it is well balanced (like an AVL tree), a B-tree provides the possibility of very short path lengths for accessing very large collections of elements. Samira Lahmar

3 B-Tree Example Samira Lahmar

4 Insertion illustrated
Each node in a a B-tree contains between m/2 and m-1 elements where m is the order of the tree. The root node is the only exception to this rule, and it may contain between 1 and m-1 elements. The number of children of a B-tree node either is 0 or one greater than the number of elements in that node. Keep in mind that we use a small order 5 so as to make illustrations feasible. In practice, B-trees of order 100 or more are common. We observe that the elements in the node appear in sorted order. This is always true of a B-tree node and is part of the reason that finding an element only requires searching a single path from the root to a leaf node. Samira Lahmar

5 Algorithmic Description of insertion
The first step is always to find the leaf node where the element would be if it were in the tree. After the correct leaf node is found, a sequence of insertion processes begins. Each process adds one element to a leaf of the tree and, if a node split is required, passes an element to the level above. (we call these processes insert1, insert2 and insert3). An insertion in a B-tree always begins by adding an element to a leaf node. The height of tree increases only if the nodes along the path are full. Since the height of tree is increased by adding a new root, the tree remains well balanced Samira Lahmar

6 Insertion Algorithm Insert1: The node contains fewer than m-1 elements and so has room to accommodate an additional element. The element is added so as to maintain sorted order, and the insertion process terminates. Insert2: The node is full ( it contains m-1 elements) so a node split is required. A new node is created that receives the largest m/2 elements from the node that is being split. The smallest m/2 elements remain in their original node, and the middle element is passed to the parent of the split node. The insertion process continues. Insert3: The root node is split, a new root node is created, and the element passed up becomes its only element. The insertion process terminates. Samira Lahmar

7 Insertion Example (I) Samira Lahmar

8 Insertion Example (II)
Samira Lahmar

9 Insertion Example (III)
Samira Lahmar

10 Deletion Algorithm If the element to be deleted is not in a leaf node, it is replaced by the smallest element in the leftmost node in its right subtree (or the largest element in the rightmost node in its left subtree). This replacement requires removing an element from a leaf node, so we are back to starting with a leaf node. As with insertion, there are three cases to consider. The process begins with a leaf node, but these processes apply to a node at any level. We refer to the node from which the element is being removed as the target node. Samira Lahmar

11 Deletion Algorithm Delete1: The target node contains more than m/2 elements. The target element is removed and the process terminates. Delete2: The target node contains exactly m/2 elements, so removing one causes an underflow. If either the right or left sibling of the target node contains more than m/2 elements, then elements are borrowed to prevent the underflow. This causes the element in the parent that separates these two children to change. This is usually done so as to balance the number of elements in the two children. The process then terminates. If both the right and left siblings contain exactly m/2 elements, then the target node is combined with one of them to form a new node containing m-1 elements one of these is the element from the parent node that separated the two nodes that are combined. The parent node becomes the target node and the process continues. Samira Lahmar

12 Deletion Algorithm Delete3: The target node is the root node. As long as at least one element remains, the height of the tree does not change. If the last element was removed, then the one remaining child becomes the root of the tree. Samira Lahmar

13 Deletion Example (I) Samira Lahmar

14 Deletion Example (II) Samira Lahmar

15 Delete 77 from this B-Tree of order 5
50 80 55 70 33 45 52 54 77 78 87 88 96 97 59 61 91 93 Target node It is a leaf node Samira Lahmar

16 50 80 55 70 33 45 52 54 78 87 88 96 97 59 61 91 93 Underflow Siblings have m/2 items only Can not borrow  combine Samira Lahmar

17 50 80 55 70 33 45 52 54 78 87 88 96 97 59 61 91 93 Underflow Siblings have m/2 items only Can not borrow  combine You might think: Why not borrow 81? Samira Lahmar

18 Incorrect 50 81 55 70 33 45 52 54 78 80 87 88 96 97 59 61 82 83 91 93 You might think: Why not borrow 81? Just like this Answer is simple: This is NOT a sibling Samira Lahmar

19 Correct 50 80 55 33 45 52 54 87 88 96 97 91 93 New Target node Underflow Right Sibling has > m/2 items  borrow Samira Lahmar

20 50 80 55 33 45 52 54 87 88 96 97 91 93 New Target node You might think: Why not combine with left sibling? Answer is simple: We follow steps, first check if can we borrow, if NOT then combine Samira Lahmar

21 Borrowing from non-leaf
50 85 55 80 33 45 52 54 87 88 96 97 91 93 What happens to the children? Samira Lahmar

22 50 85 55 80 33 45 52 54 87 88 96 97 91 93 What happens to the children? Some are ok Some need adjustment (the ones between the moved values) Samira Lahmar

23 Correct final solution
50 85 55 80 33 45 52 54 87 88 96 97 91 93 What happens to the children? Some are ok Some need adjustment (the ones between the moved values) Samira Lahmar


Download ppt "(edited by Nadia Al-Ghreimil)"

Similar presentations


Ads by Google