Presentation is loading. Please wait.

Presentation is loading. Please wait.

Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight.

Similar presentations


Presentation on theme: "Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight."— Presentation transcript:

1 Splay Trees CSE 331 Section 2 James Daly

2 Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight

3 Review: Binary Tree Every node has at most 2 children Left and right child Variation: n-ary trees have at most n children

4 Review: Binary Search Tree For every node Left descendents smaller (l ≤ k) Right descendents bigger (r ≥ k) k <k>k

5 Review: AVL Trees Named for Adelson-Velskii & Landis Self-balancing binary search tree Goal: Keep BST in balance Ability to check / track balance

6 Review: AVL Trees For every node, the height of both subtrees differs by at most 1.

7 RotateRight(&t) // Rotates t down to the right // Brings up left child left ← t.left temp = left.right left.right ← t t.left ← temp t ← left

8 Left-Left / Right-Right Case D B a6a6 c5c5 e5e5 B D e5e5 c5c5 a6a6

9 Left-Right / Right-Left Case F B a5a5 e4e4 g5g5 D c5c5 D B a5a5 c5c5 F e4e4 g5g5 B a5a5 c5c5 e4e4 D F g5g5

10 Motivation Frequently we care more about how long it takes to do a string of operations than any one O(n) isn’t too bad – if we do it only once Want O(m log n) for m searches Recently used items are more likely to be called again Basis for caches

11 Splay Tree Not kept rigorously balanced When a node is accessed, rotate it to the top Next search for the same item will be very quick No need for extra information AVL Tree: Node height Red-Black Tree: Node color

12 Wrong way k1 k2 k3 k4 k5 A B C D E F

13 Wrong way k2 k3 k4 k5 k1 A B C D E F

14 Wrong way k2 k3 k4 k5 k1 A B C D E F

15 Wrong way k2 k3 k4 k5 k1 A B C D E F

16 Wrong way k2 k3 k4 k5 k1 A B C D E F

17 Problem K3 was pushed down almost as far as K2 came up. Easy to show that you could keep selecting bad nodes O(n 2 ) time total. Need a smarter way to do this

18 Splaying Find X If root, done If parent(X) = root, rotate up Otherwise, X has both parent and grandparent Two cases: Zig-Zag and Zig-Zig

19 Zig-Zag X P G A B C D X PG A C B D

20 Zig-Zig X P G A B C D X P G A B C D

21 Splaying Tends to reduce the height of the tree Many items will be half as deep as before Some items may be at most 2 deeper than before

22 Right way k1 k2 k3 k4 k5 A B C D E F

23 Right way k1 k2 k3 k4 k5 A C B D E F

24 Right way k1 k2 k3 k4 k5 A C B D E F

25 Insertion Insert X as normal for a BST Splay X to the top

26 Deletion Remove X as normal for a BST Splay its parent to the top


Download ppt "Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight."

Similar presentations


Ads by Google