Algorithms
  • Introduction
  • Algorithms
    • Insertion Sort
    • Shell Sort
    • Selection Sort
    • Heap Sort
    • Merge Sort
    • Quick Sort
    • Dual Pivot QuickSort
    • Radix Exchange Sort
  • Data Structures
    • Basic Datastructures
      • Table or Array
      • List
      • Linked List
      • Stack
      • Queue
      • Deque
      • Dictionary
      • HashTables
    • Trees
      • Tree
      • Binary Search Tree
      • Heap
      • Red-Black Trees
      • Splay Trees
      • Randomized Search Trees
    • Dynamic Programming
      • Fibonacci
      • Knapsack
      • Optimal Binary Search Trees
      • Longest Common Sub sequence
    • External Data Structures
      • B-Trees
      • Variants
    • Multi-dimensional Data Structures
      • Point-Region Quadtree
      • Point-Quadtrees
    • Priority Queues
      • Binomial Queues
  • Graphs
    • Basics
    • Depth First Search
    • Applications of Depth First Search
    • Breadth First Search
    • Shortest Paths
    • Transitive Closure
    • Flow Networks
    • Matching
  • Strings
    • Data Structures for Strings
      • Tries
        • Binary Tries
        • Multiway Trie
        • Patricia Tries
      • Variable Length-code
        • Elias Code
        • Gamma Code
        • Fibonacci Code
      • Huffmancode
      • Ternary Search Tree
    • Searching in Strings
      • Prefix Function
      • Knuth-Morris-Pratt
      • Boyer-Moore
      • Monte-Carlo Algorithms
      • Karp-Rabin
      • Automatons
      • Shift-AND
    • Indexing Text
      • Suffix Trees
      • Suffix Arrays
      • Search Engines & Inverted Files
  • P and NP
    • Introduction
    • SAT and 3-SAT
    • Graph Problems
    • Problems with Collections
    • Networks
    • Data storage
  • Metaheuristics
    • Trajectory Methods
      • Local Search
      • Simulated Annealing
      • Tabu Search
    • Population Based
      • Ant Colony
      • Evolutionary Computation
  • Sources
Powered by GitBook
On this page
  • Searching
  • Insertion
  • Removal
  • Examples

Was this helpful?

  1. Data Structures
  2. External Data Structures

B-Trees

B-Trees are a continuation on the 2-3 trees, with every node pointing towards a datablock. These trees have some special properties:

  • They have a magnitude m

  • The amount of keys is dependant on this magnitude

    • the root can have between 1 and m - 1 keys.

    • a node can have between (m/2) - 1 and m - 1 keys.

  • Every leaf is on the same level

  • The max amount of child nodes at a node is the #maximum_number_of_keys + 1

  • Every key of a B-Tree is ordered ascending

Node properties:

  • Every node has a number k that shows the current number of keys in the node

  • It has a table with maximal m pointers towards children of the node

  • It has a table for maximal m - 1 keys sorted ascending

  • It has a boolean showing if it's a leaf or not

The performance of a B-tree is O(log n) for searching, removal and insertion. Because these operations are a bit more length than normal trees, they are explained in the subsections below.

Searching

Searching is the easiest case in a B-tree, to search we just work our way down by following the pointers towards the child nodes.

Note that a pointer towards a child node is inbetween 2 values. For example if we want to go to a child with the key 5, we would need to find the pointer between 4 and 6.

Insertion

To insert a new element, we have to find the place where we want to insert it. Once we found this place, we need to check the number of elements:

If the number of elements currently in the node + 1 < maximum elements allowed, then we can insert a new node without problems.

However when we want to add an element to a node that is full, then we have to split it. To split this node, we take the median of the elements, and put this as our new parent node. Then we put all the values smaller in a new left node and all the other values in the new right node.

Note that since we put the median in the parent element that this can also create a split, so make sure to recursively check this.

Removal

Removing a node is a little bit harder than inserting one since we have to take merging into account. To remove a node, we first where it should be removed. Now we replace the node with its inorder successor and remove the node.

This removal can cause a underflow, because the minimum amount of elements should be equal to m/2. To fix this we need to borrow an element from one of the siblings, or remove the leaf if borrowing is not possible. See the examples below for more information on how this works.

Examples

  • B-tree of magnitude 4

    • The number of keys is between 2 and 3

  • B-tree of magnitude 6

    • The number of keys is between 3 and 5

  • B-tree of magnitude 5

    • The number of keys is between 2 and 4

Now try this yourself and insert the keys: 1, 2, 3, 4, 5, 6 for a B-tree with magnitude m = 3

PreviousExternal Data StructuresNextVariants

Last updated 6 years ago

Was this helpful?