miriam b+ tree

25
Chapter 12 Chapter 12 B+ Trees B+ Trees CS 157B CS 157B Spring 2003 Spring 2003 By: Miriam Sy By: Miriam Sy

Upload: rajesh-talluri

Post on 28-Sep-2015

236 views

Category:

Documents


1 download

TRANSCRIPT

  • Chapter 12B+ TreesCS 157BSpring 2003By: Miriam Sy

  • B+ Tree The B+ Tree index structure is the most widely used of several index structures that maintain their efficiency despite insertion and deletion of data.

    It takes the form of a balanced tree in which every path from the root of the tree to a leaf is of the same length.

  • B Trees & B+ TreesB Trees:Multi-way treesDynamic growthContains only data pagesB+ Trees:Contains features from B TreesContains index and data pagesDynamic growth

  • Fill FactorB+ Trees use a fill factor to control the growth and the shrinkage.50% fill factor is the minimum for a B+ Tree. For n=4, the following guidelines must be met:

  • B+ TreesB+ Tree with n=4

  • Inserting RecordsThe key value determines a records placement in a B+ TreeThe leaf pages are maintained in sequential order AND a doubly linked list (usually not shown) ,which connects each leaf page with its sibling page(s). This doubly linked list speeds data movement as the pages grow and contract.

  • Insertion Algorithm

  • Insertion Algorithm cont

  • Insertion 1st Case: leaf page & index page NOT FULLOriginal Table:

    Insert 28:

  • Insertion 2nd Case: leaf page is Full but index page is Not FullNext, were going to insert a record with a key value of 70. This record should go in the leaf page with 50, 55, 60, and 65. Unfortunately, this page is already full so we need to split nodes into the following:

    The middle key (60) is placed in the index page between 50 and 75.

  • B+ Tree after insertion of 70

  • Insertion 3rd Case: leaf page & index page is FULLFor our last example, we need to add 95This record belongs in the page containing 75, 80, 85, and 90. Unfortunately, this is full so we need to split it into two pages:

    The middle key, 85, rises to the index page. Unfortunately, the index page is also full so we must split it up:

  • B+ Tree after insertion of 95

  • RotationB+ Trees can incorporate rotation to reduce the number of page splits.A rotation occurs when a leaf page is full, but one of its siblings pages is not full. Rather than splitting the leaf page, we move a record to its sibling, adjusting indices as necessary.The left sibling is usually checked first, then the right sibling .

  • RotationFor example, consider the B+ Tree before the addition of 70. As previously stated, this record belongs in the leaf node containing 50 55 60 65. Notice how this node is full but its left sibling is not.

    Using rotation, we shift the record with the lowest key to its sibling. We must also modify the index page since this key also appears there.

  • RotationThis is what the new B+ Tree looks like with the rotation:

  • Deletion AlgorithmLike the insertion algorithm, there are three scenarios we must consider when deleting a record from a B+ Tree.First Scenario:

  • Deletion Algorithm cont

  • DeletionTo refresh our memories, heres our B+ Tree right after the insertion of 95:

  • Deletion (1st case)Delete record with Key 70

  • Deletion (2nd case)Delete 25 from the B+ TreeBecause 25 appears in the index page, when we delete 25, we must replace it with 28 in the index page.

  • Deletion (3rd case)Deleting 60 from the B+ TreeThe leaf page containing 60 (60 65) will be below the fill factor after the deletion. Thus, we must combine leaf pages.With recombined pages, the index page will be reduced by one key. Hence, it will also fall below the fill factor. Thus, we must combine index pages.60 appears as the only key in the root index page. Obviously, it will be removed with the deletion.

  • Deletion (3rd case)B+ Tree after deletion of 60

  • B+ Trees in PracticeTypical Order: 100Typical Fill Factor: 67%Typical Capacities:Height 4: 133^4 = 312,900,700 recordsHeight 3: 133^3 = 2,352,637 records

  • Concluding RemarksTree structured indexes are ideal for range-searches, also good for equality searchesB+ Tree is a dynamic structureInsertions and deletions leave tree height balancedHigh fanout means depth usually just 3 or 4Almost always better than maintaining a sorted file