Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

Similar documents
CSE 100: RED-BLACK TREES

Self-Adjusting Binary Search Trees. Andrei Pârvu

5 AVL trees: deletion

Topic 23 Red Black Trees

Module 3 Greedy Strategy

RBT Operations. The basic algorithm for inserting a node into an RBT is:

Binary Search Tree (Part 2 The AVL-tree)

MITOCW 6. AVL Trees, AVL Sort

CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

and 6.855J. Network Simplex Animations

MITOCW ocw lec11

The tenure game. The tenure game. Winning strategies for the tenure game. Winning condition for the tenure game

Past questions from the last 6 years of exams for programming 101 with answers.

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

Chapter 7: Sorting 7.1. Original

( n) Lower bounds for Selection (cont)

Module 3 Greedy Strategy

((( ))) CS 19: Discrete Mathematics. Please feel free to ask questions! Getting into the mood. Pancakes With A Problem!

Lecture5: Lossless Compression Techniques

SORTING BY REVERSALS. based on chapter 7 of Setubal, Meidanis: Introduction to Computational molecular biology

Skip Lists S 3 S 2 S 1. 2/6/2016 7:04 AM Skip Lists 1

Launchpad Maths. Arithmetic II

Algorithms and Data Structures

Game Theory and Randomized Algorithms

LECTURE VI: LOSSLESS COMPRESSION ALGORITHMS DR. OUIEM BCHIR

Lectures: Feb 27 + Mar 1 + Mar 3, 2017

From Shared Memory to Message Passing

100 IDEAS FOR USING A HUNDRED SQUARE

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

AI Approaches to Ultimate Tic-Tac-Toe

FACTORS, PRIME NUMBERS, H.C.F. AND L.C.M.

Notes on 4-coloring the 17 by 17 grid

COS 226 Algorithms and Data Structures Fall Midterm Exam

Online Frequency Assignment in Wireless Communication Networks

COS 226 Algorithms and Data Structures Fall Midterm Exam

Class 8: Square Roots & Cube Roots (Lecture Notes)

Answer keys to the assessment tasks 61 Answer keys to the challenge questions 63 Achievement Profile 64

CPS331 Lecture: Search in Games last revised 2/16/10

Section II.9. Orbits, Cycles, and the Alternating Groups

Binary trees. Application: AVL trees and the golden ratio. The golden ratio. φ 1=1/φ 1. φ =1+

IMOK Maclaurin Paper 2014

ARTIFICIAL INTELLIGENCE (CS 370D)

mywbut.com Two agent games : alpha beta pruning

Informatica Universiteit van Amsterdam. Performance optimization of Rush Hour board generation. Jelle van Dijk. June 8, Bachelor Informatica

SOLUTIONS TO PROBLEM SET 5. Section 9.1

Previous Lecture. How can computation sort data faster for you? Sorting Algorithms: Speed Comparison. Recursive Algorithms 10/31/11

CMPUT 396 Tic-Tac-Toe Game

Manipulative Mathematics Using Manipulatives to Promote Understanding of Math Concepts

Some results on Su Doku

12. 6 jokes are minimal.

Random Binary Search Trees. EECS 214, Fall 2017

Stack permutations and an order relation for binary trees

PRIORITY QUEUES AND HEAPS

4. Non Adaptive Sorting Batcher s Algorithm

MITOCW watch?v=krzi60lkpek

Huffman Coding - A Greedy Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

Written Exam Information Transmission - EIT100

DICOM Correction Item

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

!!"!#$#!%!"""#&#!%!""#&#"%!"# &#!%!# # ##$#!%!"'###&#!%!"(##&#"%!"!#&#!%!""# #!!"!#$#!%)# &#!%*# &#"%(##&#!%!# Base or

MITOCW R9. Rolling Hashes, Amortized Analysis

GENERALIZATION: RANK ORDER FILTERS

CSE 21 Practice Final Exam Winter 2016

Design of Parallel Algorithms. Communication Algorithms

Hypercube Networks-III

VP3: Using Vertex Path and Power Proximity for Energy Efficient Key Distribution

Design Automation for IEEE P1687

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Algorithmic Game Theory Date: 12/6/18

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

Merge Sort. Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

Problem 1 (15 points: Graded by Shahin) Recall the network structure of our in-class trading experiment shown in Figure 1

6.034 Quiz 2 20 October 2010

Permutation Editing and Matching via Embeddings

THEME: COMMUNICATION

CS/COE 1501

COMP 2804 solutions Assignment 4

Practice Midterm 2 Solutions

Tutorial 1. (ii) There are finite many possible positions. (iii) The players take turns to make moves.

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

Lecture Notes 3: Paging, K-Server and Metric Spaces

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Algorithms and Game Theory Date: 12/4/14

7.4 Permutations and Combinations

CS : Data Structures

Logic Restructuring Revisited. Glitching in an RCA. Glitching in Static CMOS Networks

COSC 3213: Communication Networks Chapter 5: Handout #6

CSI33 Data Structures

Quarter Turn Baxter Permutations

Maxima and Minima. Terminology note: Do not confuse the maximum f(a, b) (a number) with the point (a, b) where the maximum occurs.

Math Fundamentals for Statistics (Math 52) Unit 2:Number Line and Ordering. By Scott Fallstrom and Brent Pickett The How and Whys Guys.

THE AIR FORCE SCHOOL SUBROTO PARK : DELHI CANTT Class UKG Sub:MATH Weekly Syllabus Academic Session

Clock Synchronization

BBS: Lian et An al. Energy Efficient Localized Routing Scheme. Scheme for Query Processing in Wireless Sensor Networks

Global State and Gossip

CS 771 Artificial Intelligence. Adversarial Search

Fibonacci Numbers ANSWERS Lesson 1 of 10, work individually or in pairs

Team Name: 1. Remember that a palindrome is a number (or word) that reads the same backwards and forwards. For example, 353 and 2112 are palindromes.

2048 IS (PSPACE) HARD, BUT SOMETIMES EASY

SF2972: Game theory. Mark Voorneveld, February 2, 2015

Entropy Coding. Outline. Entropy. Definitions. log. A = {a, b, c, d, e}

Problem Set 10 Solutions

Transcription:

4.5 Splay trees Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations 43 Splay trees splay tree is a balanced ST built using an amortization approach do more work at every insertion or search to save subsequent search time. More work : What: ring the accessed (i.e., inserted or searched) node to the root. Why: It pays off if the accessed node is accessed again soon. How: ouble-rotate called splaying. 44 1

Splaying operation Splay(k): move a node k to the root through double rotation operations. Single rotation is not used in the splay tree. s a result, halve the depth of most nodes (excluding the root) encountered on the path from the root to the accessed node. Splaying tends to (not always) keep the splay tree more balanced than the ST. 45 Splay double-rotations Two cases: Zig-zig: left left or right right The order of rotations: top-down, which guarantees that the distance to every node encountered (except the root) is reduced to half. This case is different from that of the VL tree, and brings the positive effect of splaying. Zig-zag: left-right or right-left The order of rotations: bottom-up, which guarantees that the tree height is reduced by one at each zig-zag rotation. This case is the same as that of the VL tree. 46 2

Splay(): zig-zig case I rotr() rotr() fter splaying at, the depth of is reduced from 2 to 0 (and the depth of is the same). 47 Splay(): zig-zig case II 1 2 rotl() rotl() (o it yourself!) 48 3

Splay(): zig-zag case I rotl() rotr() fter splaying at (hence at the root as a result), the tree height has been reduced by one. 49 Splay(): zig-zag case II 2 1 rotr() rotl() (o it yourself!) 50 4

Summary: rotations in VL and splay ase Tree type VL tree Splay tree zig-zig (left-left or right-right) zig-zag (otherwise) single rotation bottom-up double rotation top-down double rotation (the roots) bottom-up double rotation (the current node) 51 Why double rotations for splaying? ouble rotations tend to reduce the height more quickly than single rotations. Example: E single rotation double rotation (PTO) 52 5

53 What if the access path length is an odd number? One rotation must be a single rotation. o it either at the bottom or at the top. The resulting tree structures may be different. oth are fine! 54 6

Splay insertion algorithm insert(node) { 1. Insert the new node. 2. Let n be the length of the path traversed for the insertion. Then, a. If n = 2m (i.e., even number) then perform m double rotations. b. If n = 2m+1 (i.e., odd number) then perform one single rotation followed by m double rotations. } 55 Exercise: splay tree insertion onstruct a splay tree by inserting, Y, U,, G in sequence. What if the node already exists? Splay from the existing node. 56 7

57 Exercise: splay tree search Search the tree below with the following keys in sequence: E,, S, Y, Q, U, T, I, O, and N. E I N O Q S T U Y What if the node is not found? Splay from the last node visited. 58 8

59 Splay deletion: remove(node) 1. Splay-search the node. (That is, find the node and rotate it up to the root.) 2. Find the largest node in the left subtree of the root (or find the smallest node in the right subtree) and rotate it up to replace the root. 60 9

Splay deletion: remove(node) side note Q. What is the point of bringing the node to the root after finding it, only to remove it? The rationale for splaying is that a node accessed now is likely to be accessed again soon. ut, in the case of remove(node), the node is removed (so never accessed again) after all the work done to splay it up to the root.. Splaying tends to shorten the access path to every node encountered while finding the node. Recall the demo of Figures 4.47 to 4.56 in the textbook and see how the depth of each node is reduced at each splaying, especially in the beginning with a bunch of zig-zig cases. Of course, this depth reduction does not happen all the time, but overall we see the trend. 61 Top-down splay tree ottom-up splay tree walks the access path twice once for searching and once for splaying. Top-down splay tree does the splaying while walking down the access path. Section 12.1 62 10

Top-down splay tree -- side note The splay tree has been shown to be faster than the VL tree in benchmark experiments performed using "real" applications. ll balanced STs we are studying are main-memory data structures and, thus, the run-time is very sensitive to the code implementation. Splay tree operations can be coded more efficiently (i.e., the top-down splay tree) than the VL tree operations. This could be one reason for the benchmark results being in favor of the splay tree. 63 Running time of splay tree operations 1/2 single operation: O(logN) average, O(N) worst. ut, the worst case occurs less frequently than the ST. Searching for M nodes: O(M logn) for both average and worst. Inserting N nodes: O(N logn) for both average and worst. We say, the amortized running time of a single operation takes O(logN). Note. ompare with the ST case, where searching for M nodes and inserting N nodes take O(M N) and O(N 2 ), respectively. 64 11

Running time of splay tree operations (2/2) nalyzing the running time of splay operations involves amortized algorithm analysis. This analysis appears in Section 11.5, but is beyond our scope here. What s in Section 11.5? onsider the potential function Φ of a splay tree T as: Φ(T) = x T R(x), where R(x) = log S(x) where S(x) is the number of descendants of a node x (including x itself) in T. Then, the amortized run-time to splay a tree at node x is at most 3(R(T) R(x))+1 = 3(logN R(x))+1 = O(logN). Proof of this claim appears on page 533. 65 12