Download presentation
Presentation is loading. Please wait.
Published byLeslie Gray Modified over 9 years ago
1
B-Trees and Red Black Trees
2
Binary Trees B Trees spread data all over – Fine for memory – Bad on disks
3
Disk Blocks Disk data stored in blocks/sectors – 512bytes – 4kB – Increment for all reds/writes – Like to maximize useful data
4
B Trees B Tree – Each node can store up to n values – Each value choses between two pointers Max Degree = num pointers = n + 1
5
B Trees B Tree – Node designed to fit block size – May have degree of 100+ First level 100 keys Second level 100,000 keys Third level 100,000,000 keys! – Still compare against keys one at a time
6
B Tree Simulation BTree Inserts – Always insert at leaves – Split at max size, median value moves to parent – Tree gets deeper when root splits
7
B Tree Simulation BTree Deletes – Remove from node – Empty node Steal from sibling if possible Else merge with sibling & steal key from parent Start Removed 104 Removed 112
8
So… Btrees self balance Can represent as a binary tree…
9
Red Black Tree Red Black tree can be seen as binary version of Btree – Red nodes are part of their parent 1 red + 1 black = node degree 3 2 red + black = node degree 4
10
Red Black Rules Two views of same tree
11
Red Black Rules Rules 1.The root is black
12
Red Black Rules Rules 1.The root is black – if it becomes red, turn it back to black 2.Null values are black 3.A red node must have black children 4.Every path from root to leaf must have same number of black nodes
13
Nodes vs Edges Note: Can think of edges or nodes as red/black
14
Guarantee Worst and best case in terms of red nodes for black height = 2 If L = num leaves, B = black height 2 B ≤ L ≤ 2 2B 2 B ≤ L ≤ 4 B
15
Guarantee
16
Height Black height is O(logL) Total height at most 2B – 2O(logL) = O(logL) – Height is O(logL) Total nodes (N) < 2L – 1 – O(log(n/2)) = O(logn) Guaranteed logN performance
17
Actual Work Insert as normal – New node is always red – Two red's in a row need to be fixed…
18
Fixes Red parent of red child with red sibling – Push up redness of siblings to grandparent Fix them if necessary If root becomes red, make it black Splitting a 4 node
19
Fixes Red parent of red child with black/no sibling – AVL style rotation to balance – New parent becomes black, new child red Turn a 3 node + child into 4 node
20
Binary Tree Comparisons BST – Hopefully O(logn) – Could be O(n) Splay – Amortized O(logn) – Ideal for consecutive accesses
21
Binary Tree Comparisons AVL – Guaranteed O(logn) – High constant factors on insert/delete – Height limited to ~1.44 log(n)
22
Binary Tree Comparisons Red/Black – Guaranteed O(logn) – Height limited to ~2 log(n) Less balanced than AVL – Faster insert/remove – Slower find – Standard implementation for most library BSTs
23
Tree Comparisons BTree – Not binary – can pick any node size – Still sorted – Self balancing – Ideal for slower storage – Model for red/black trees
24
Tree Comparisons Other trees – Represent tree structure – Not necessarily sorted
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.