TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Lecture 8 – Overview Splay trees Page 1 J. Maluszynski, IDA, Linköpings Universitet, 2004. [Lewis/Denenberg 7.3] TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Splay operation 5 9 3 1 5 Page 2 9 Page 4 T1 T2 3 1 9 3 5 9 J. Maluszynski, IDA, Linköpings Universitet, 2004. 1 9 21 33 J. Maluszynski, IDA, Linköpings Universitet, 2004. Splay around missing key 4 5 is the key in T where the search of K succeded or failed. Given: BST T and key K ( may not be in T ) Splay T around K gives BST T such that: Root T 3 1 Concatenate 5 Conc T1 T2 defined if all keys of T1 smaller than any key of T2: Splay T1 around ∞; attach T2 as right child of the root. Dictionary operations on splay tree (2) TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Splay around 5 T has the same keys as T an alternative to binary search tree balancing, [Lewis/Denenberg 9.1] [Lewis/Denenberg 9.2] J. Maluszynski, IDA, Linköpings Universitet, 2004. used for implementation of ADT Dictionary, guarantees good amortized efficiency. Lab 3: Binary Search Trees Priority Queue ADT extension of ADT Dictionary used e.g. in shortest paths algorithm for graphs implementations by balanced trees, heaps, leftist trees Up-trees implementing union of disjoint sets Page 3 3 21 3 1 K concatenate LC T and RC T 33 TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. 4 Insert 4 3 1 9 Dictionary operations on splay tree (1) 3 1 9 5 up K T : Splay T around K; check if the root is K 1 9 5 Look 9 Insert K T : Splay T around K; add K as the root. 3 5 5 1 Splay K T , If Root T Delete K T : T TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. How to splay Splay A T , Page 5 1. A has no g-parent: rotate around parent B (Fig.7.21) Three cases: J. Maluszynski, IDA, Linköpings Universitet, 2004. General idea: lift A by rotating T around parent B of A (and g-parent C) 2. key A key B key C or key A key B key C : rotate around C, rotate around B (Fig. 7.22) B T2 T3 C T4 A B T1 C A A has predecessors in line : (1) gp rotation, (2)p rotation Page 7 Repeat until A becomes the root. TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. A Splay step: case 2 T1 T1 T2 T3 T4 T2 B T3 C T4 J. Maluszynski, IDA, Linköpings Universitet, 2004. 3. otherwise : rotate around B, rotate around C (Fig. 7.23) TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. A T2 B T3 T4 T3 T1 Page 6 C T2 A T3 B T1 T4 T1 A predecessors not in line : (1) p rotation, (2)gp rotation Page 8 A has no grandparent: Right Rotation C Splay step: case 1 T1 TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. C A Splay step: case 3 T1 T2 C T3 J. Maluszynski, IDA, Linköpings Universitet, 2004. T2 A T3 B T4 J. Maluszynski, IDA, Linköpings Universitet, 2004. A C T2 J. Maluszynski, IDA, Linköpings Universitet, 2004. TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Page 10 J. Maluszynski, IDA, Linköpings Universitet, 2004. Lab 3 Introduction Page 9 Splay Tree Theorem An interactive system for manipulating BST’s with character keys. A skeleton code of the system is provided TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Consider ADT Dictionary implemented as splay tree. J. Maluszynski, IDA, Linköpings Universitet, 2004. The skeleton gives access to some operations on BST trees. The code of the operations is missing. The assignment: Implement the operations Any sequence of m dictionary operations such that the splay tree is initially empty never has more than n nodes uses O m log n time. Page 12 delete with auxiliary function deletemin preorder, postorder, inorder Proof: Lewis & Denenberg 7.3 levelorder TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. J. Maluszynski, IDA, Linköpings Universitet, 2004. Page 11 TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. ADT Priority Queue ADT Priority Queue: Motivation Linearly ordered set K of keys Commonly encountered situation: We store pairs k i (as in Dictionary), but multiple pairs with same key are allowed. Waiting list (tasks, passengers, vehicles entering a ferry, phone calls) A frequent operation is retrieving pairs with minimal key. If a resource is freed choose an element from the waiting list PQ with minimal k, return i Operations on a Priority Queue PQ: The choice is based on some partial ordering: some tasks are more essential to achieve the goal, some passengers should be served before the others (children,...) fire dept. and first aid vehicles have priority How to organize prioritized service? MakeEmptyPQ IsEmpty PQ Insert PQ k i DeleteMin PQ delete k i ... TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. 5 Page 13 Page 15 2 1 0 10 I3 5 7 5 I1 I0 I2 3 12 I4 J. Maluszynski, IDA, Linköpings Universitet, 2004. 12 5 13 J. Maluszynski, IDA, Linköpings Universitet, 2004. TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. 5 5 4 10 I0 5 I1 7 7 5 12 0 4 3 2 5 12 I4 10 I3 4 I2 5 I5 1 4 TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. 7 12 Heap Insert Example 10 6 7 5 m Page 14 2 1 0 10 I3 5 7 5 I2 I1 I0 Page 16 3 4 12 I4 12 I5 4 7 5 6 7 10 4 m 5 5 3 2 1 0 5 12 I4 10 I3 5 7 4 I0 I1 I5 I2 4 7 6 5 J. Maluszynski, IDA, Linköpings Universitet, 2004. m J. Maluszynski, IDA, Linköpings Universitet, 2004. Insert [L/D Fig. 9.3, Algorithm 9.1] insert the new node as the “next” leaf restore partial ordering by swapping the nodes (upwards) DeleteMin = deletion of the root [L/D Fig. 9.2, Algorithm 9.1] replace the root by the “last” leaf restore partial ordering by swapping the nodes (downwards) Priority Queue operations on complete partially ordered trees 0 5 2 4 9 Implementing Priority Queues 1 7 4 12 5 6 7 56 Given a complete binary partially ordered tree: 8 22 10 Searching for a minimal element in a search tree (BST, AVL, 2-3,...) 16 18 20 32 BST: does not admit repeated keys, extension needed Another idea: keep the minimal element as the root of the tree partially ordered tree 3 10 (implicit representation with size info). A heap is a complete partially ordered tree represented as a table Heap TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. the key of a parent is less than or equal to the key of each child Partially ordered binary tree: TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. 7 10 DeleteMin: 12 6 7 5 0 6 7 I2 I1 12 I4 6 1 10 I3 12 2 12 I4 3 4 5 0 7 5 I2 I1 I0 Page 17 1 6 10 I3 2 3 12 I4 7 4 5 6 7 m 10 Page 19 6 m=0 K=12 Size=4 12 4 3 2 1 0 12 I4 10 I3 12 I4 7 6 I1 I2 5 6 7 J. Maluszynski, IDA, Linköpings Universitet, 2004. m J. Maluszynski, IDA, Linköpings Universitet, 2004. TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Leftist Tree T binary tree, v node D LC v Page 18 D RC v DT v distance to closest external node T is a leftist tree iff TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Page 20 Consider partially ordered leftist trees. Heap DeleteMin example 10 6 7 TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. J. Maluszynski, IDA, Linköpings Universitet, 2004. for every v J. Maluszynski, IDA, Linköpings Universitet, 2004. How to represent such data for efficient implementation of these operations? Example: Check to which group a student belong. Groups may be merged. Disjoint Sets with Union: Motivation Implementing ADT Priority Queue with Leftist Trees Division into disjoint sets appears often in practice. T, T1, T2 partially ordered leftist trees. LUnion T 1 T 2 produces a leftist tree with all keys of T 1 T 2 (definition of LUnion p.306) LUnion LC T RC T LUnion is logarithmic: it is accomplished by traversing the shortest path(s) of arguments. For a tree of size n the shortest path has at most log2 n 1 nodes. DeleteMin T Insert X T LUnion TX T TX - one node tree with record X TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Page 21 of disjoint sets is maintained: The ADT Operations on Disjoint Sets A database MakeSet X returns a new set with single element X such that X Up-tree Implementation Operations: Page 23 S [L/D 9.2] J. Maluszynski, IDA, Linköpings Universitet, 2004. . J. Maluszynski, IDA, Linköpings Universitet, 2004. Union S T root pointer of S directed to the root of T or vice versa; O 1 To avoid unbalance attach smaller tree to larger. (Algorithm 9.3) no restriction on number of children one pointer per node Union S T returns S T , which replaces S and T in Find X returns S in TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Find X starting from pointer to X follow the pointers until the root is reached; O log n TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Page 22 Up-Tree Representation of Sets A H M L F C B M A H Page 24 B L F D C H A F L M Elements of a set represented by nodes of a tree. Sets identified by roots B L F D TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. H A Union of Up-Trees B J. Maluszynski, IDA, Linköpings Universitet, 2004. D J. Maluszynski, IDA, Linköpings Universitet, 2004. M C TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Locating element by a key B Page 25 H A F F L Create a dictionary of element pointers. A B H M L J. Maluszynski, IDA, Linköpings Universitet, 2004. M TDDB57 DALG – Lecture 8: Splay trees. Priority Queues. Good News Page 26 J. Maluszynski, IDA, Linköpings Universitet, 2004. Up-trees constructed from singleton sets by Algorithm 9.3 are balanced (why?) (see p.309) Union takes constant time, Find takes logarithmic time, Path compression on Find (p.311) makes amortized time sublogarithmic (p.314)