Skip to content

Instantly share code, notes, and snippets.

@myui
Last active August 19, 2020 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myui/e2424bcd2b11741f68581a6c80b5e438 to your computer and use it in GitHub Desktop.
Save myui/e2424bcd2b11741f68581a6c80b5e438 to your computer and use it in GitHub Desktop.
Deletion from a leaf node
 1. search for the key to delete
 2. if the key in leaf nodes, simply delete it
 3. if underflow happens, rebalancing
    Rebalancing starts from a leaf toward the root until the tree is balanced.
Triggered when underflow happens through remove()

typeswitch(node) {

  case when match(Leaf) {

   if canMerge(R to L)
     merge a deficient node R into left-sibling node L (append)
     remove R's separator from parent
   else 
     merge a deficient node L onto right-sibling node R (prepend)
     remove L's seperator from parent
	 
   # It may be propagated to branch's rebalancing
  }

  case when match(Root) {

     if root has only 1 child
        mark the child as root

  }

  case when match(Branch) {
  
    if canMerge(R to L)
       move R's seperator S from parent to L (append)
       merge entries in R into L (append)
    else merge a deficient node L onto right-sibling node R
       move L's seperator S from parent to R (prepend)
       merge entries in L into R (prepend) 
  }

}
@myui
Copy link
Author

myui commented Aug 19, 2020

Marge Leaf (R → L)

delete_leaf-merge-forward

Marge Leaf (L → R)

delete_leaf-merge-backward

Delete root

delete_branch-root

Merge branch (R → L)

delete_branch-merge-forward

images are taken from https://hazm.at/mox/algorithm/structural-algorithm/b-plus-tree.html

@myui
Copy link
Author

myui commented Aug 19, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment