Lec-23 B-Trees

download Lec-23 B-Trees

of 17

Transcript of Lec-23 B-Trees

  • 8/3/2019 Lec-23 B-Trees

    1/17

    B-Trees

  • 8/3/2019 Lec-23 B-Trees

    2/17

    Operation on B-Trees

    Searching a B-Tree.

    Creating an empty B-Tree.

    Splitting a node in B-Tree.

    Inserting a Key into B-Tree.

    Deleting a Key from B-Tree.

  • 8/3/2019 Lec-23 B-Trees

    3/17

    Searching a B-Tree It is much like searching a BST, except that

    instead of making a binary or two way

    decision at each node, we make a multi waybranching decision according to number of

    children.

    In other words, at each internal nodex, wemake an (n[x]+1)-way branching decision.

  • 8/3/2019 Lec-23 B-Trees

    4/17

    Searching (Contd.) B-TREE-SEARCH takes as input a pointer to

    the root nodexof a sub tree and a key kto besearched for in that sub tree.

    The toplevel call is thus of the form B-TREE-SEARCH( root[T], k).

    Ifkis in the B-Tree, this procedure returns theordered pair (y, i), consisting of a node yand anindex i, such that keyi[y]=k.

  • 8/3/2019 Lec-23 B-Trees

    5/17

    The nodes encountered during the recursionforms a path downward from the root of the

    tree.

    The number of disk pages accessed by

    procedure is therefore

    Searching( Contd.)

    O(h)=O(logt(n))

    Since n[ x] 2t, thus time taken to searchwithin each node is O(t), and the total CPU

    time is O(t*h)=O(t logt(n))

  • 8/3/2019 Lec-23 B-Trees

    6/17

    Splitting a node in B-Tree Inserting a key into B-Tree is significantly more

    complicated than inserting a key into BST

    Fundamental operation used during insertion issplitting of a full node y(having 2t-1 keys)around its median key keyi [y]into two nodeshaving t-1 keys each.

    The median key moves up into ys parent.

  • 8/3/2019 Lec-23 B-Trees

    7/17

    Splitting (contd..)

    ys parent must be non-fullprior to splitting ofy.

    Ifyhas no parent, then the tree grows in heightby one.

    So splitting is the mean by which B-Tree grows.

  • 8/3/2019 Lec-23 B-Trees

    8/17

    If a node becomes full, it is necessary to

    perform a split operation.

    The B-TREE-SPLIT-CHILD algorithm will run in

    O(t), where t is constant.

    Splitting (contd..)

  • 8/3/2019 Lec-23 B-Trees

    9/17

    Splitting (contd..)

    N W

    P Q R S T U V

    N S W

    P Q R T U V

  • 8/3/2019 Lec-23 B-Trees

    10/17

    Insertion in a BInsertion in a B--TreeTree

    To perform an insertion in a B-tree, the

    appropriate node for the key must be located

    using an algorithm similar to B-Tree-Search

    Next, the key must be inserted into the node

    If the node is not fullprior to the insertion, no

    special action is required

  • 8/3/2019 Lec-23 B-Trees

    11/17

    Splitting the node results in moving one key tothe parent node, what if the parent node is full?

    Then parent has to be split too.

    This process may repeat all the way up to theroot and may require splitting the root node

    This approach requires two passes. The firstpass locates the node where the key should beinserted; the second pass performs anyrequired splits on the ancestor nodes

    Insertion in a BInsertion in a B--Tree (cont)Tree (cont)

  • 8/3/2019 Lec-23 B-Trees

    12/17

    Since each access to a node may correspond

    to a costly disk access, it is desirable to avoid

    the second pass by ensuring that the parent

    node is never full

    To accomplish this, the algorithm splits any full

    nodes encountered while descending the tree

    Is there a problem?

    Insertion in a BInsertion in a B--Tree (cont)Tree (cont)

  • 8/3/2019 Lec-23 B-Trees

    13/17

    This approach may result in unnecessary splitoperations

    But it guarantees that the parent never needs to

    be split and eliminates the need for a secondpass up the tree

    What is the penalty?

    Since a split runs in linear time, it has little

    effect on the O(t logt n) running time ofB-Tree-

    Insert.

    Insertion in a BInsertion in a B--Tree (cont)Tree (cont)

  • 8/3/2019 Lec-23 B-Trees

    14/17

    G M P Q X

    A C D E J K N O R S T U V Y Z

    Initial Tree and Assume t=3

    G M P X

    A B C D E J K N O R S T U V Y Z

    Inserting B

    Minimum Number of keys at any internal node =

    Maximum Number of keys at any node =

    t-1 = 2

    2t-1 = 5

  • 8/3/2019 Lec-23 B-Trees

    15/17

    G M P X

    A B C D E J K L N O R S U V Y Z

    Inserting L

    Inserting F

    Is it as much simple?Nodeis already full

    We haveto splitit

    first

  • 8/3/2019 Lec-23 B-Trees

    16/17

    G M P X

    A B C D E J K L N O R S U V Y Z

    Inserting F

    Median: Split here, C will move to parent node

    C G M P X

    J K L N O R S U V Y ZD E FA B

    What will happen if we want to insert T?

    What will happen if we want to insert Q?

  • 8/3/2019 Lec-23 B-Trees

    17/17

    Deleting a key from B-Tree Deletion from a B-tree is analogous to

    insertion but a little more complicated.

    Reading Assignment.

    We will discuss in the next class

    I will randomly call students to discussdifferent parts of deletion algorithm on board

    So understand the algorithm very well

    Quiz on B Trees in the next class