b+ tree insertion & deletion

37
B + -Tree COMP171 Tutorial 9

Upload: subhash-pal-singh

Post on 16-Nov-2015

25 views

Category:

Documents


0 download

DESCRIPTION

data structure

TRANSCRIPT

  • B+-TreeCOMP171 Tutorial 9

  • Deficiency of AVL TreePerforms really bad when the data is too huge and cannot be put in the main memoryToo much disk access if the data is stored in the diskAccess on disk is much slower than access on main memoryNo. of disk access is proportional to the depth of AVL tree

  • Alternative Solution M-ary TreeEvery node has multiple children (M-ary means M branches)Depth decreases as branching increasesDepth = O(logMn) instead of O(log2n)Therefore, no. of disk access also decreases

  • B+-Tree Basic InformationAn M-ary tree (M>3)Leaves contain data itemsall are at the same deptheach node has L/2 to L data (usually L
  • B+-Tree ExampleM = L = 4Records are at the leavesNode are at least half-full, so that the tree will not degenerate into simple binary tree or even link listLeft child pointer & right child pointer and also left subtree & right subtree are defined

  • B+-Tree In PracticalEach internal node & leaf has the size of one I/O block of dataminimize tree depth and so as no. of disk accessFirst one or two levels of the tree are stored in main memory to speed up searchingMost internal nodes have less than (m-1) searching keys most of the timehuge space wastage for main memory, but not a big problem for disk

  • B+-Tree Searching Example 1Search G

    PM = L = 4F JUABCDFG

    JKLMUV

    PQ

    Found!!

  • B+-Tree Searching Example 2Search H

    PF JUABCDFG

    JKLMUV

    PQ

    Not Found!!M = L = 4

  • B+-Tree Searching AlgorithmSearching KEY:Start from the rootIf an internal node is reached:Search KEY among the keys in that nodelinear search or binary searchIf KEY < smallest key, follow the leftmost child pointer downIf KEY >= largest key, follow the rightmost child pointer downIf Ki
  • B+-Tree Insertion Example 1Insert H

    PF JUABCDFG

    JKLMUV

    PQ

    Done!!HM = L = 4

  • B+-Tree Insertion AlgorithmInsert KEY:Search for KEY using search operationwe will reach a leaf and get Not FoundInsert KEY into that leafIf the leaf contains
  • B+-Tree Insertion Example 2Insert E

    PF JUABCDFGH

    JKLMUV

    PQ

    Oops!!Splitting is neededM = L = 4E

  • B+-Tree Insertion Algorithm (Cont)Splitting leaf:Cut the node out, insert KEY into itSplit it into 2 new leaves Lleft and LrightLleft has the (L+1)/2 smallest keysLright has the remaining (L+1)/2 keysMake a copy of the smallest key in Lright, say J, to be the parent of Lleft and LrightInsert J, together with Lleft and Lright, into the original parent node

  • B+-Tree Insertion Example 2 (Cont)Insert E

    P F JUCDE

    FGH

    JKLMUV

    PQ

    M = L = 4AB

    CDone!!ABCDE

  • B+-Tree Insertion Example 3Insert N

    PC F JUCDE

    FGH

    JKLMUV

    PQ

    M = L = 4AB

    Leaf Splitting!!N

  • B+-Tree Insertion Example 3 (Cont)Insert N

    PC F JUCDE

    FGH

    UV

    PQ

    M = L = 4AB

    JKLMNLMN

    JK

    LDone?? No. of keys= 4 > (M-1)!!

  • B+-Tree Insertion Algorithm (Cont)Splitting internal node:Cut the node outSplit it into 2 new internal nodes Nleft and NrightNleft has the smallest (M/2-1) keysNright has the largest M/2 keysNote that the (M/2)th key is not in either node!because (M/2-1) + (M/2) = M/2 + M/2 - 1 = M - 1Make the (M/2)th key, say J, to be the parent of Nleft and NrightInsert J, together with Nleft and Nright, into the original parent node

  • B+-Tree Insertion Example 3 (Cont)Insert N

    PCUCDE

    FGH

    UV

    PQ

    M = L = 4AB

    LMN

    JK

    Done!!C F J LJ LF

  • B+-Tree Insertion Algorithm (Cont)Splitting root:Follow exactly the same procedure as splitting an internal nodeJ, the parent of Nleft and Nright, is now set to be the root of the treebecause the original root is destroyed by splittingAfter splitting the root, the depth of the tree is increased by 1

  • B+-Tree Deletion Example 1Delete H

    F PCUCDE

    FGH

    UV

    PQ

    AB

    LMN

    JK

    Done!!J LM = L = 4

  • B+-Tree Deletion AlgorithmDelete KEY:Search for KEY using search operationwe will reach a leaf and get FoundDelete KEY from that leafIf KEY is included in an ancestor (internal node), replace it by the new smallest key in that leafIf that leaf finally contains < L/2 keys, borrowing of child is necessary

  • B+-Tree Deletion Algorithm (Cont)Lending of child:Case 1: if the right sibling of the current node contains >= (L/2+1) keysborrow the leftmost child from it (no return!)Case 2: if the left sibling of the current node contains >= (L/2+1) keysborrow the rightmost child from it (no return!)Update the searching key in the parent node separating the current node and the sibling accordingly

  • B+-Tree Deletion Example 2Delete B

    F PCUCDE

    FG

    UV

    PQ

    AB

    LMN

    JK

    Done!!J LCDM = L = 4

  • B+-Tree Deletion Algorithm (Cont)What if both left & right sibling have only L/2 keys, so that borrowing is not possible? since the minimum no. of key of an internal node is L/2We need to merge two leaves

  • B+-Tree Deletion Example 3Delete G

    F PDUDE

    FG

    UV

    PQ

    AC

    LMN

    JK

    Oops!!Cant Borrow!!J LM = L = 4

  • B+-Tree Deletion Algorithm (Cont)Merging two leaves:Move all keys in the current leaf to the sibling leafDelete the child pointer at the parent node which is pointing to the current leafDelete the separating key between the two leaves from the parent node

  • B+-Tree Deletion Example 3 (Cont)Delete G

    F PDUDE

    F

    UV

    PQ

    AC

    LMN

    JK

    Done!!J LM = L = 4F

  • B+-Tree Deletion Algorithm (Cont)How about the case of internal node?First, try borrowing of childIf not possible, merge two internal nodes

  • B+-Tree Deletion Example 4Delete C

    F PDUDE

    UV

    PQ

    AC

    LMN

    FJK

    Oops! We need tomerge two leaves!!LM = L = 4

  • B+-Tree Deletion Example 4 (Cont)Delete C

    F PDU

    DE

    UV

    PQ

    A

    LMN

    FJK

    Can we do that?? No!!An internal node will become empty!!LM = L = 4AWe need to merge two internal nodes first!!

  • B+-Tree Deletion Algorithm (Cont)Merging two internal nodes(i.e. no sibling has excesschild to be borrowed)Move the separating key between the current node and the sibling node in the parent node down to the siblingMove the keys and child pointers in the current node to the sibling nodeRemove the pointer to the current node in the parent node

  • B+-Tree Deletion Example 4 (Cont)Delete C

    F PDU

    DE

    UV

    PQ

    LMN

    FJK

    Done!! LM = L = 4FAA

  • B+-Tree Deletion Example 5Delete P

    PUADE

    UV

    PQ

    LMN

    FJK

    F LM = L = 4Oops! We need tomerge two leaves!!But can we?? No!!We have to borrow a child first!!

  • B+-Tree Deletion Algorithm (Cont)Borrow a child (i.e. an siblinghas excess child to be borrowed)Move the separating key between the current node and the sibling node in the parent node down to the siblingMake the leftmost (/rightmost) child of sibling node to be the rightmost (/leftmost) child of the current nodeMove the leftmost (/rightmost) key of the sibling node to be the new separating key between the current node and the sibling in the parent node

  • B+-Tree Deletion Example 5 (Cont)Delete P

    P UADE

    UV

    PQ

    LMN

    FJK

    F LM = L = 4Done!!PLQQ

  • B+-Tree Deletion Algorithm (Cont)What if the root becomes empty during merging of nodes?make the only-one child of the original root to be the new root

  • B+-Tree Final Output

    LQADE

    QUV

    LMN

    FJK

    FM = L = 4