B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It...

24
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, •It is not a binary search tree. •It is not a binary tree. •Each node contains more than just a single entry. •B-tree nodes may have many more than

Transcript of B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It...

B-trees (Balanced Trees)

A B-tree is a special kind of tree, similar to a binary tree.

However,

•It is not a binary search tree.

•It is not a binary tree.

•Each node contains more than just a single entry.

•B-tree nodes may have many more than two children.

B-trees rules

A given B-tree has a constant called MINIMUM, that determines how many entries are held in a single node.

B-tree Rule 1: The root may have as few as one entry (or even no entry if it also has no children); every other node has at least MINIMUM entries.

B-tree Rule 2: The maximum number entries in a node is twice the value of MINIMUM.

B-trees rules

B-tree Rule 3: The entries of each B-tree node are stored in a partially filled array, sorted from the smallest entry (at index 0) to the largest entry (at the final used position of the array).

The number of subtrees below a node depends on how many entries are in the node.

B-tree Rule 4: The number of subtrees below a non-leaf node is always one more than the number of entries in the node.

B-tree search rules

B-tree Rule 5: For any non-leaf node: • An entry at index I is greater than all the entries in the subtree number I of the node.•An entry at index I is less than all entries in a subtree number I+ of the node.

A B-tree is balanced.

B-tree Rule 6: Every leaf in a B-tree has the same depth.

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

R1: Root has one entry, every other node has at least one entry.R2: Maximum number of entries is 2*1 = 2

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

R4: The number of subtrees of a node is always one more thanthe number of entries in that node.

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

R5: For non-leaf nodes, an entry at index I is greater than allentries in subtree I. E.g. entry [0] = 2 and maximum entry insubtree[0] = 1, and the minimum entry in subtree[1] = 3.

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

R6: Every leaf node, is at the same depth.

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

[0] [1]

6[0] [1]

2 4

B-trees (MINIMUM = 1)

6

9

10531

2 and 4

7 and 8

[0] [1]

6[0] [1]

2 4

B-trees (Searching)

6

9

10531

2 and 4

7 and 8

If the value we are searching for is in the root, we are done.

B-trees (Searching)

6

9

10531

2 and 4

7 and 8

Otherwise find the first value greater then the one we aresearching for and search its subtree.

B-trees (Searching for ‘3’)

6

9

10531

2 and 4

7 and 8

Search the root and we find ‘6’. Since it is greater than ‘3’,search the first subtree.

B-trees (Searching)

Note:

Each new case is a smaller version of the original problem.This most clearly suggests a recursive solution.

Base case - Item found or search exhausted.

Recursive case: Search subtree for item.

B-trees (Searching for ‘3’)

531

2 and 4

Search for the first value greater than ‘3’. It is not ‘2’, but it is ‘4’. This is the second item, so search the second subtree.

B-trees (Searching for ‘3’)

3

If the value we are searching for is in the root, we are done.

B-trees (Searching)

Note:

Each new case is a smaller version of the original problem.This most clearly suggests a recursive solution.

Base case - Item found or search exhausted.

Recursive case: Search subtree for item.

B-trees (Inserting items)

Binary search trees become unbalanced by inserting new nodesas new leaves. Trees grow downward. B-trees remain balancedwhen adding new nodes because trees grow upward.

This occurs by allowing nodes to split and promote their itemsto their root.

B-trees (Inserting ‘12’)

10

15

17531

2 and 4

11 and 14

Clearly, 12 belongs in the node with 11 and 14.

B-trees (Inserting ‘12’)

10

15

17531

2 and 4

11 and 14

12

B-trees (Inserting ‘12’)

10

15

17531

2 and 4

11 and 14

12

Violates rule 6: Every leaf is at the same depth.

B-trees (Inserting ‘12’)

10

15

17531

2 and 4

11,12,14

Violates rule 2: the maximum number of entries is 2*1 = 2.

B-trees (Inserting ‘12’)

10

15

17531

2 and 4

11,12,14

Split child with excess entry.

B-trees (Inserting ‘12’)

10

12 and 15

531

2 and 4

And promote middle value.

171411