4.4 Balanced Trees: Symbol Table
4.4 Balanced Trees: Symbol Table
Randomized BST.
! O(log N) time per op. [unless you get ridiculously unlucky]
! Store subtree count in each node.
! Generate random numbers for each insert/delete op.
2-3-4 Tree
2-3-4 Trees 2-3-4 tree. Generalize node to allow multiple keys; keep tree balanced.
Perfect balance. Every path from root to leaf has same length.
C E M O W
A D F G J L N Q S V Y Z
Search. Insert.
! Compare search key against keys in node. Search to bottom for key.
!
4-node at bottom: ??
!
K R K R
C E M O W C E M O W
A D F G J L N Q S V Y Z A D F G J L N Q S V Y Z
5 6
2-3-4 Tree: Splitting Four Nodes 2-3-4 Tree: Splitting a Four Node
Transform tree on the way down. Ex. To split a four node, move middle key up.
! Ensures last node is not a 4-node.
! Local transformation to split 4-nodes:
D D Q
K Q W K W
A-C A-C
7 8
2-3-4 Tree 2-3-4 Tree: Balance
Tree grows up from the bottom. Property. All paths from root to leaf have same length.
X A
Tree height.
! Worst case: lg N [all 2-nodes]
! Best case: log4 N = 1/2 lg N [all 4-nodes]
M P
! Between 10 and 20 for a million nodes.
! Between 15 and 30 for a billion nodes.
L E
9 10
Unsorted list N 1 1 N 1 1
Hashing N 1 N 1* 1* 1*
private void insert(Key key, Val val) { BST N N N log N † log N † log N †
Node x = root;
Randomized BST log N ‡ log N ‡ log N ‡ log N log N log N
while (x.getChild(key) != null) {
x = x.getChild(key); Splay log N § log N § log N § log N § log N § log N §
if (x.is4Node()) x.split();
2-3-4 log N log N log N log N log N log N
}
if (x.is2Node()) x.make3Node(key, val);
else if (x.is3Node()) x.make4Node(key, val); * assumes hash map is random for all keys
† N is the number of nodes ever inserted
} ‡ probabilistic guarantee
§ amortized guarantee
fantasy code
11 12
Red-Black Tree
Two easy cases. Switch colors. Two easy cases. Switch colors.
do single rotation
do double rotation
15 16
Red-Black Tree: Splitting Nodes Red-Black Tree: Insertion
inserting G
E
change colors
X A
right rotate R !
M P
left rotate E !
L E
17 18
Property A. Every path from root to leaf has same number of black links.
Worst Case Average Case
Property B. At most one red link in-a-row. Implementation Search Insert Delete Search Insert Delete
Hashing N 1 N 1* 1* 1*
19 20
Red-Black Trees: Practice
In the wild. Red-black trees are widely used as system symbol tables.
! Java: java.util.TreeMap, java.util.TreeSet.
! C++ STL: map, multimap, multiset.
! Linux kernel: linux/rbtree.h.
3 or 4 in practice! M=5
23 24
B-Tree Example (cont) Symbol Table: Implementations Cost Summary
Unsorted list N N N N N N
Hashing N 1 N 1* 1* 1*
B-Tree 1 1 1 1 1 1
page accesses
File systems.
! Windows: HPFS. Abstraction extends to give search algorithms for huge files.
! Mac: HFS, HFS+. !B-tree.
! Linux: ReiserFS, XFS, Ext3FS, JFS.
Databases.
! Most common index type in modern databases.
! ORACLE, DB2, INGRES, SQL, PostgreSQL, …
27 28
Splay Trees
Splay.
! Check two links above current node.
! ZIG-ZAG: if orientations differ, same as root insertion.
! ZIG-ZIG: if orientations match, do top rotation first.
x y
z ZIG-ZAG x z
A
y
D A B C D
B C
31 32
Splay Trees Splay Trees
Splay. Splay.
Check two links above current node.
! ! Check two links above current node.
ZIG-ZAG: if orientations differ, same as root insertion.
! ! ZIG-ZAG: if orientations differ, same as root insertion.
ZIG-ZIG: if orientations match, do top rotation first.
! ! ZIG-ZIG: if orientations match, do top rotation first.
z x
y y
D A
x ZIG-ZIG z
C B
ZAG-ZAG
A B C D
Root = Splay Root Insertion Splay Insertion
33 34
9 9
8 8
7 7
6 ZIG-ZIG 6 ZIG-ZIG
5 5
4 4
3 1
2 2
1 3
35 36
Splay Example Splay Example
9 9
8 8
7 1
6 ZIG-ZIG 6 ZIG-ZIG
1 4 7
4
2 5
2 5
3
37 38
1 10
8 8
6 9 6 9
4 7 ZIG 4 7
2 5 2 5
3 3
39 40
Splay Example Splay Trees
10 1 8
insert 1, 2, …, 40 insert 1, 2, …, 40
8 4
10
6 9 3 6 9
Splay(2)
4 7 search for
5 7 search 1 random key
2 5
search 2
3
search 3
search 4
41 42
Unsorted list N 1 1 N 1 1
Hashing N 1 N 1* 1* 1*
43