Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Similar presentations


Presentation on theme: "EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern."— Presentation transcript:

1 EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern

2  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.

3 Binary Search Trees Binary Search Tree Not a Binary Search Tree

4 BST with = left/right depths

5 AVL Trees AVL Tree Not AVL Tree

6 Right Rotation Rotate right when left left subtree too deep Right subtree of k1… becomes left subtree of k2 Parent pointer to k2… changed to point to k1

7 Left Rotation Rotate left when right right subtree too deep

8 Left-Right Rotation Rotate k2 left through k1 first… When right subtree of left subtree too deep …then rotate k2 right through k3

9 Red-Black Trees  AVL-style rotations to balance  No numbers, just one red-black bit per node (or link)  Use rotations and recoloring to guarantee: Never two red nodes in a row All leaves same depth counting black nodes

10 Rotations in Red-Black Trees

11 Recoloring in Red-Black Trees

12 Splay Tree  Balance on access, not insertion  Use AVL-style rotations  Move item just accessed to the root  Typically makes tree shallower  Complexity Can be O(N) worst case single access O(M log N) on for M accesses Amortized O(log N)  No bookkeeping data needed

13 Zig-zag Rotation

14 Zig-zig Rotation

15 B-Trees  No rotations  N-way tree (N subtrees, N-1 keys)  Keys sorted in each node  When passing through a full node: 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?

16 Adding and splitting now add 57

17 After adding 55 now add 55

18 After adding 57 now add 40

19 After adding 40


Download ppt "EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern."

Similar presentations


Ads by Google