EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

19
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern

Transcript of EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Page 1: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

EECS 311: Chapter 4 Notes

Chris RiesbeckEECS Northwestern

Page 2: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3rd ed, copyright © 2006 by Pearson Education, Inc.

Page 3: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Binary Search Trees

Binary Search Tree

Not a Binary Search Tree

Page 4: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

BST with = left/right depths

Page 5: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

AVL Trees

AVL Tree

Not AVL Tree

Page 6: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

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

Page 7: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Left Rotation

Rotate left when right right

subtree too deep

Page 8: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Left-Right Rotation

Rotate k2 left through k1 first…

When right subtree of left

subtree too deep…then rotate k2 right through k3

Page 9: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

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

Page 10: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Rotations in Red-Black Trees

Page 11: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Recoloring in Red-Black Trees

Page 12: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

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

Page 13: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Zig-zag Rotation

Page 14: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Zig-zig Rotation

Page 15: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

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?

Page 16: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

Adding and splitting

now add 57

Page 17: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

After adding 55

now add 55

Page 18: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

After adding 57

now add 40

Page 19: EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.

After adding 40