Download presentation
Presentation is loading. Please wait.
Published byMelvyn Harrington Modified over 9 years ago
1
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.
2
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.
3
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.
4
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.
5
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
6
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 than the number of entries in that node.
7
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 all entries in subtree I. E.g. entry [0] = 2 and maximum entry in subtree[0] = 1, and the minimum entry in subtree[1] = 3.
8
B-trees (MINIMUM = 1) 6 9 10531 2 and 4 7 and 8 R6: Every leaf node, is at the same depth.
9
B-trees (MINIMUM = 1) 6 9 10531 2 and 4 7 and 8 [0][1] 6 [0][1] 24
10
B-trees (MINIMUM = 1) 6 9 10531 2 and 4 7 and 8 [0][1] 6 [0][1] 24
11
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.
12
B-trees (Searching) 6 9 10531 2 and 4 7 and 8 Otherwise find the first value greater then the one we are searching for and search its subtree.
13
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.
14
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.
15
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.
16
B-trees (Searching for ‘3’) 3 If the value we are searching for is in the root, we are done.
17
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.
18
B-trees (Inserting items) Binary search trees become unbalanced by inserting new nodes as new leaves. Trees grow downward. B-trees remain balanced when adding new nodes because trees grow upward. This occurs by allowing nodes to split and promote their items to their root.
19
B-trees (Inserting ‘12’) 10 15 17531 2 and 4 11 and 14 Clearly, 12 belongs in the node with 11 and 14.
20
B-trees (Inserting ‘12’) 10 15 17531 2 and 4 11 and 14 12
21
B-trees (Inserting ‘12’) 10 15 17531 2 and 4 11 and 14 12 Violates rule 6: Every leaf is at the same depth.
22
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.
23
B-trees (Inserting ‘12’) 10 15 17531 2 and 4 11,12,14 Split child with excess entry.
24
B-trees (Inserting ‘12’) 10 12 and 15 531 2 and 4 And promote middle value. 171411
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.