1 / 24

Understanding B-Trees and Operations: Insertion, Deletion, and Rotation

Learn about B-Trees, their properties, insertion, deletion, and rotation operations with examples. Understand the key processes of handling B-Trees efficiently.

dmauricio
Download Presentation

Understanding B-Trees and Operations: Insertion, Deletion, and Rotation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mainly from Chapter 7 Adam Drozdek B-TREES

  2. WHAT ARE B-TREES??? To understand first, lets define a MULTI-WAY TREE • Multi-way tree of order m • Each node has at the most m children • If a node has k children then it has k-1 keys • Multi-way search tree of order m • Multi-way tree of order m • Keys are arranged in the manner of a search tree • Max keys for height h is mh-1 (Root is at height 1) REF: http://cis.stvincent.edu/carlsond/swdesign/btree/btree.html

  3. B-TREES For a tree to be a B-tree the following should hold: • A B-Tree is a multi-way search tree of order m • All leaves are at the last level • All internal nodes have at least ceil(m/2) children • The root node (if it is not the leaf node) is allowed to have minimum of two child nodes • Each leaf node must have at least ceil(m/2)-1 keys • All keys are sorted in a node What is the maximum number of keys we can have in order m and l levels??? REF: http://cis.stvincent.edu/carlsond/swdesign/btree/btree.html

  4. Insertion (example of a B-tree of order 5) Insert the following: 10 16 20 18 25 43 19 7 2 1 Insert 25 Insert 43 19 7 2 

  5. Insertion (contd…example of a B-tree of order 5) Insert the following: 10 16 20 18 25 43 19 7 2 1 Insert 1 Middle key inserted here This node was split in two 

  6. Insertion in general • 2 cases to handle • Insert in the leaf node if there is space. We are lucky. No further processing to do • If node is full then: • Loop till root node is reached or till empty space is found in a node (every time we go up one level) • {Split the node as follows: • Find the middle key (all keys including the new key to insert) • Keep all keys less than middle key in the older node • Keep all keys greater than middle key in the new node • Insert the middle key in the parent node • }

  7. Look at this example (B-tree of order 4) Insert 16 Insert the mid key 15 Split level This is parentless right now

  8. Example contd... Insert the mid key 15 Split level This is parentless right now Split level New node

  9. B-tree of order 5 Delete T Delete R http://cis.stvincent.edu/carlsond/swdesign/btree/btree.html

  10. Delete E http://cis.stvincent.edu/carlsond/swdesign/btree/btree.html

  11. root root root 30 42 30 17 40 15 27 12 17 30 27 27 15 35 12 17 40 12 40 15 24 14 24 35 14 35 24 14 M M M L L L M M M M M M L L L L L L AN EXAMPLE OF DELETION (order 3) delete 42 n s perform rotation a hole is created

  12. Delete 50 swap with largest value on left 40 50 62 31 62 31 37 58 37 65 58 65 22 22 34 61 12 25 55 64 67 34 40 61 12 25 55 64 67 40 40 62 62 31 58 58 65 65 22 31 22 61 61 12 25 55 64 67 12 25 55 64 67 34 37 34 37 DELETION MORE EXAMPLES… hole here hole here hole here

  13. 40 62 40 62 58 58 65 65 22 31 12 31 61 61 12 25 55 64 67 25 55 64 67 34 37 34 37 40 62 58 65 31 61 12 25 55 64 67 34 37 contd…Final tree: Notice how we propagated the hole upwards Delete 22 Replace with largest value on left sibling has 1 key parent has 2 keys

  14. DELETION • If it is not a terminal node then swap the value with a leaf node • Delete the value from the leaf node. There are three possibilities • Leaf node has lots of keys and deleting one key does not violate the properties of the tree • We need to perform rotation • We need to join two nodes

  15. INSERTION IN GENERAL When considering insertion at one level: Case 0: The node has space, we are done Case 1: The node curr is to be split and parent has space (can be done as one case while programming) Case 1a: The parent of curr has space for middle key of curr Case 1b: curr has all keys > than all keys of parent and parent has space for middle key of curr Case 2: The node curr is to be split (LMG are keys in curr) (M is mid key, L is preceding to M key, G is successor key of M in the same node curr). The mid key of parent of curr is M1. parent does not have space for M so it has to be split. Case 2a: The parent of A is to be split and LMG are smaller than M1 Case 2b: The parent of A is to be split and LMG are greater than M1 case 2c: The parent of A is to be split and M=M1 case 2d: Think of the extreme case

  16. Case 1a Case 1a INSERTIONCase 1a and 1b New key inserted B A New link curr (mid key is M) B A curr newNode Case 1b G is highest key in node Now M is highest key in node A A curr (mid key is M) newNode curr (mid key was M)

  17. Insert M here this node has no space Mid key is M1 (including M) INSERTION B A curr (mid key is M) CASE 2a: LMG are all < M1 newNode1 All keys are > M1 B A curr newNode

  18. INSERTION Insert M here this node has no space Mid key is M1 (including M) B A curr (mid key is M) CASE 2b: LMG are all > M1 All keys are < M1 newNode1 B A curr newNode

  19. INSERTION Insert M here this node has no space Mid key is M1 (including M) B A curr (mid key is M) CASE 2c: M=M1 All keys are < M newNode1 A B curr newNode

  20. DELETION 2 cases • Rotation: (one for general case and one for extreme position) • left rotate • right rotate • Join • hole on left • hole on right

  21. ROTATION(left) parent B A sibling Curr (hole) W Y Z X parent B A sibling Curr (hole) W Y Z X

  22. ROTATION(right) parent sibling B A Curr (hole) A1 A2 are the last (highest) keys in sibling W Y X Z parent B A sibling Empty slot Curr W Y Z X

  23. JOIN(hole on left) parent empty slots sibling Curr (min number of keys last key is A1) X W Y Z parent (A1 removed) sibling Curr (delete it) W Y Z X move all keys from Curr to sibling

  24. JOIN(hole on right) Do yourself

More Related