Red-Black Trees 1/16/2019 1:56 PM Splay Trees 6 3 8 4 v z Splay Trees.

Slides:



Advertisements
Similar presentations
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Advertisements

Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CSC401 – Analysis of Algorithms Lecture Notes 7 Multi-way Search Trees and Skip Lists Objectives: Introduce multi-way search trees especially (2,4) trees,
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
Goodrich, Tamassia Search Trees1 Binary Search Trees   
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
CSC Analysis of Algorithms 3-1 CSC401 – Analysis of Algorithms Chapter 3 Search Trees and Skip Lists Objectives: Review binary search trees and present.
1 COMP9024: Data Structures and Algorithms Week Six: Search Trees Hui Wu Session 1, 2014
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
Part-D1 Binary Search Trees
Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000
AA Trees.
COMP9024: Data Structures and Algorithms
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Binary Search Trees < > =
Search Trees.
Binary Search Trees < > =
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Splay Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Tree Chapter 10.
Red-Black Trees 9/12/ :44 AM AVL Trees v z AVL Trees.
Section 8.1 Trees.
Binary Search Trees < > = Binary Search Trees
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Lecture 25 Splay Tree Chapter 10 of textbook
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Binary Search Trees < > =
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Binary Search Trees < > = Binary Search Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
Red-Black Trees 2018年11月26日3时46分 AVL Trees v z AVL Trees.
Heaps 11/27/ :05 PM Heaps Heaps.
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
11 Search Trees Hongfei Yan May 18, 2016.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Red-Black Trees.
Tree Rotations and AVL Trees
Binary Search Trees < > =
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Binary Search Trees < > =
Binary Search Trees 7/16/2009.
CMSC 341 Splay Trees.
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
Binary Search Trees < > =
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
CMSC 341 Splay Trees.
Red-Black Trees 5/19/2019 6:39 AM AVL Trees v z AVL Trees.
CMSC 341 Splay Trees.
Splay Trees Binary search trees.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Topic 10 Trees.
Heaps 9/29/2019 5:43 PM Heaps Heaps.
CS210- Lecture 19 July 18, 2005 Agenda AVL trees Restructuring Trees
Presentation transcript:

Red-Black Trees 1/16/2019 1:56 PM Splay Trees 6 3 8 4 v z Splay Trees

Splay Trees are Binary Search Trees all the keys in the yellow region are  20 all the keys in the blue region are  20 (20,Z) note that two keys of equal value may be well-separated (10,A) (35,R) BST Rules: entries stored only at internal nodes keys stored at nodes in the left subtree of v are less than or equal to the key stored at v keys stored at nodes in the right subtree of v are greater than or equal to the key stored at v An inorder traversal will return the keys in order (14,J) (7,T) (21,O) (37,P) (1,Q) (8,N) (36,L) (40,X) (1,C) (5,H) (7,P) (10,U) (2,R) (5,G) (5,I) (6,Y) Splay Trees

Searching in a Splay Tree: Starts the Same as in a BST (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) Search proceeds down the tree to found item or an external node. Example: Search for time with key 11. Splay Trees

Example Searching in a BST, continued (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) search for key 8, ends at an internal node. Splay Trees

Splay Trees do Rotations after Every Operation (Even Search) new operation: splay splaying moves a node to the root using rotations right rotation makes the left child x of a node y into y’s parent; y becomes the right child of x left rotation makes the right child y of a node x into x’s parent; x becomes the left child of y y x a right rotation about y a left rotation about x x T3 T1 y x y T1 T2 T2 T3 T1 y x T3 T2 T3 T1 T2 (structure of tree above y is not modified) (structure of tree above x is not modified) Splay Trees

Splaying: zig-zig zig-zig zig-zag zig zig zig-zag “x is a left-left grandchild” means x is a left child of its parent, which is itself a left child of its parent p is x’s parent; g is p’s parent start with node x is x a left-left grandchild? is x the root? zig-zig yes stop right-rotate about g, right-rotate about p yes no is x a right-right grandchild? is x a child of the root? zig-zig no left-rotate about g, left-rotate about p yes yes is x a right-left grandchild? is x the left child of the root? zig-zag no left-rotate about p, right-rotate about g yes is x a left-right grandchild? zig zig zig-zag yes right-rotate about the root left-rotate about the root right-rotate about p, left-rotate about g yes Splay Trees

Visualizing the Splaying Cases zig-zag x z z y z y T4 y T1 x T4 T1 T2 T3 T4 x T3 T2 T3 T1 T2 zig-zig y x zig T1 T4 x y x T2 w z w T3 y T1 T2 T3 T4 T3 T4 T1 T2 Splay Trees

Splaying Example 1. 2. 3. let x = (8,N) (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) let x = (8,N) x is the right child of its parent, which is the left child of the grandparent left-rotate around p, then right-rotate around g g 1. (before rotating) p x (10,A) (20,Z) (37,P) (21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x g p (10,A) (20,Z) (37,P) (21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x g p 2. (after first rotation) 3. (after second rotation) x is not yet the root, so we splay again Splay Trees

Splaying Example, Continued now x is the left child of the root right-rotate around root (10,A) (20,Z) (37,P) (21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x (10,A) (20,Z) (37,P) (21,O) (35,R) (36,L) (40,X) (7,T) (1,C) (1,Q) (5,G) (2,R) (5,H) (6,Y) (5,I) (14,J) (8,N) (7,P) (10,U) x 2. (after rotation) 1. (before applying rotation) x is the root, so stop Splay Trees

Example Result of Splaying (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) Example Result of Splaying before 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) after first splay after second splay Splay Trees

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) Splay Trees

Splay Trees & Ordered Dictionaries which nodes are splayed after each operation? method splay node if key found, use that node if key not found, use parent of ending external node get(k) put(k,v) use the new node containing the entry inserted 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) remove(k) Splay Trees

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). Splay Trees

Cost per zig Doing a zig at x costs at most rank’(x) - rank(x): w T1 T2 T3 y T4 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). Splay Trees

Cost per zig-zig and zig-zag y x T1 T2 T3 z T4 zig-zig Doing a zig-zig or zig-zag at x costs at most 3(rank’(x) - rank(x)) - 2 zig-zag y x T2 T3 T4 z T1 Splay Trees

Cost of Splaying Cost of splaying a node x at depth d of a tree rooted at r: at most 3(rank(r) - rank(x)) - d + 2: Proof: Splaying x takes d/2 splaying substeps: Splay Trees

Performance of Splay Trees Recall: rank of a node is logarithm of its size. Thus, amortized cost of any splay operation is O(log n) In fact, the analysis goes through for any reasonable definition of rank(x) This implies that splay trees can actually adapt to perform searches on frequently-requested items much faster than O(log n) in some cases Splay Trees