The document discusses B+ trees and threaded trees. It provides examples of how keys are inserted and deleted from B+ trees, including splitting and merging nodes. It also describes how threaded trees use unused pointers in leaf nodes to efficiently traverse the tree in inorder without recursion. Traversal follows the thread to the right node or links to the leftmost child.
1 of 32
Downloaded 27 times
More Related Content
B+tree by Prabhat Raghav
1. Prabhat Raghav 08/07/16 1
B+ Trees
Similar to B trees, with a few slight
differences
All data is stored at the leaf nodes (leaf
pages); all other nodes (index pages) only
store keys
Leaf pages are linked to each other
Keys may be duplicated; every key to the
right of a particular key is >= to that key
3. 3
B+ Tree Insertion
Insert at bottom level
If leaf page overflows, split page and copy
middle element to next index page
If index page overflows, split page and move
middle element to next index page
Prabhat Raghav 08/07/16
10. 10
B+ Tree Insertion Example 2
Split index
page, move 13
16, 18
93, 4, 6 14
9
13
16, 17 18, 20
Prabhat Raghav 08/07/16
11. 11
B+ Tree Deletion
Delete key and data from leaf page
If leaf page underflows, merge with sibling
and delete key in between them
If index page underflows, merge with sibling
and move down key in between them
Prabhat Raghav 08/07/16
14. 14
B+ Tree Deletion Example
Leaf page underflow,
so merge with sibling
and remove 9
3, 4, 6
13
16, 18
14 16, 17 18, 20
Prabhat Raghav 08/07/16
15. 15
B+ Tree Deletion Example
Index page underflow,
so merge with sibling
and demote 13
13, 16, 18
3, 4, 6 14 16, 17 18, 20
Prabhat Raghav 08/07/16
16. 16
Threaded Trees
Binary trees have a lot of wasted space: the
leaf nodes each have 2 null pointers
We can use these pointers to help us in
inorder traversals
We have the pointers reference the next
node in an inorder traversal; called threads
We need to know if a pointer is an actual link
or a thread, so we keep a boolean for each
pointer
Prabhat Raghav 08/07/16
17. 17
Threaded Tree Code
Example code:
class Node {
Node left, right;
boolean leftThread, rightThread;
}
Prabhat Raghav 08/07/16
19. 19
Threaded Tree Traversal
We start at the leftmost node in the tree, print
it, and follow its right thread
If we follow a thread to the right, we output
the node and continue to its right
If we follow a link to the right, we go to the
leftmost node, print it, and continue
Prabhat Raghav 08/07/16
29. 29
Threaded Tree Traversal Code
Node leftMost(Node n) {
Node ans = n;
if (ans == null) {
return null;
}
while (ans.left != null) {
ans = ans.left;
}
return ans;
}
void inOrder(Node n) {
Node cur = leftmost(n);
while (cur != null) {
print(cur);
if (cur.rightThread) {
cur = cur.right;
} else {
cur = leftmost(cur.right);
}
}
}
Prabhat Raghav 08/07/16
30. 30
Threaded Tree Modification
Were still wasting pointers, since half of our
leafs pointers are still null
We can add threads to the previous node in
an inorder traversal as well, which we can
use to traverse the tree backwards or even to
do postorder traversals
Prabhat Raghav 08/07/16