14-4. SplayTrees

# Before applying rotation 2 after rotation x is the

• Notes
• 17

This preview shows pages 9–15. Sign up to view the full content.

(before applying rotation) 2. (after rotation) x is the root, so stop

This preview has intentionally blurred sections. Sign up to view the full version.

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 10 Example Result of Splaying tree might not be more balanced e.g. splay (40,X) before, the depth of the shallowest leaf is 3 and the deepest is 7 after, the depth of shallowest leaf is 1 and deepest is 8 (20,Z) (37,P) (21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) (20,Z) (37,P) (21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) (20,Z) (37,P) (21,O) (14,J) (7,T) (35,R) (10,A) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (8,N) (7,P) (36,L) (10,U) (40,X) before after first splay after second splay
© 2004 Goodrich, Tamassia, Dickerson Splay Trees 11 Splay Tree Definition a splay tree is a binary search tree where a node is splayed after it is accessed (for a search or update) deepest internal node accessed is splayed splaying costs O(h), where h is height of the tree – which is still O(n) worst-case O(h) rotations, each of which is O(1)

This preview has intentionally blurred sections. Sign up to view the full version.

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 12 Splay Trees & Ordered Dictionaries which nodes are splayed after each operation? use the parent of the internal node that was actually removed from the tree (the parent of the node that the removed item was swapped with) erase(k) use the new node containing the entry inserted put(k,v) if key found, use that node if key not found, use parent of ending external node get(k) splay node method
© 2004 Goodrich, Tamassia, Dickerson Splay Trees 13 Amortized Analysis of Splay Trees Running time of each operation is proportional to time for splaying. Define rank(v) as the logarithm (base 2) of the number of nodes in subtree rooted at v. Costs: zig = \$1, zig-zig = \$2, zig-zag = \$2. Thus, cost for playing a node at depth d = \$d. Imagine that we store rank(v) cyber-dollars at each node v of the splay tree (just for the sake of analysis).

This preview has intentionally blurred sections. Sign up to view the full version.

© 2004 Goodrich, Tamassia, Dickerson Splay Trees 14 Cost per zig Doing a zig at x costs at most rank’(x) - rank(x): cost = rank’(x) + rank’(y) - rank(y) - rank(x) < rank’(x) - rank(x).
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern