EECS 311 Chapter 4 Notes Chris Riesbeck EECS
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern
o Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3 rd ed, copyright © 2006 by Pearson Education, Inc.
Binary Search Trees Binary Search Tree Not a Binary Search Tree
BST with = left/right depths
AVL Trees AVL Tree Not AVL Tree
Right Rotation Parent pointer to k 2… changed to point to k 1 becomes left subtree Right subtree of of k 2 k 1… Rotate right when left subtree too deep
Left Rotation Rotate left when right subtree too deep
Left-Right Rotation When right subtree of left subtree too deep …then rotate k 2 right through k 3 Rotate k 2 left through k 1 first…
Red-Black Trees o o o AVL-style rotations to balance No numbers, just one red-black bit per node (or link) Use rotations and recoloring to guarantee: n n Never two red nodes in a row All leaves same depth counting black nodes
Rotations in Red-Black Trees
Recoloring in Red-Black Trees
Splay Tree o o o Balance on access, not insertion Use AVL-style rotations Move item just accessed to the root Typically makes tree shallower Complexity n n n o Can be O(N) worst case single access O(M log N) on for M accesses Amortized O(log N) No bookkeeping data needed
Zig-zag Rotation
Zig-zig Rotation
B-Trees o o No rotations N-way tree (N subtrees, N-1 keys) Keys sorted in each node When passing through a full node: n n Make a new node Move the keys and pointers on the right to the new node Move middle key and the pointer to the new node to parent node Note: parent can't be full. Why?
Adding and splitting now add 57
After adding 55 now add 55
After adding 57 now add 40
After adding 40
- Slides: 19