Splay Trees 3/20/14. Splay Trees. Splay Trees are Binary Search Trees. note that two keys of equal value may be wellseparated (7,T) (1,Q) (1,C) (5,H)

Similar documents
Splay Trees Goodrich, Tamassia, Dickerson. Splay Trees 1

Splay Trees. Splay Trees 1

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Search Trees. Chapter 11

AVL Trees. Reading: 9.2

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Trees. (Trees) Data Structures and Programming Spring / 28

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Algorithms. AVL Tree

Augmenting Data Structures

Ch04 Balanced Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Stores a collection of elements each with an associated key value

AVL Trees (10.2) AVL Trees

Binary Search Trees. Analysis of Algorithms

Binary Trees, Binary Search Trees

CHAPTER 10 AVL TREES. 3 8 z 4

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Chapter 10: Search Trees

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

CS350: Data Structures Red-Black Trees

Programming II (CS300)

Advanced Tree. Structures. AVL Tree. Outline. AVL Tree Recall, Binary Search Tree (BST) is a special case of. Splay Tree (Ch 13.2.

Self-Balancing Search Trees. Chapter 11

Chapter 2: Basic Data Structures

13.4 Deletion in red-black trees

Algorithms. Deleting from Red-Black Trees B-Trees

Data Structures and Algorithms

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Multiway Search Trees. Multiway-Search Trees (cont d)

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

CH 8. HEAPS AND PRIORITY QUEUES

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Search Trees - 1 Venkatanatha Sarma Y

Binary Trees. Binary Search Trees

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

CH. 8 PRIORITY QUEUES AND HEAPS

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Heaps. 2/13/2006 Heaps 1

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Friday Four Square! 4:15PM, Outside Gates

Advanced Set Representation Methods

Red-Black Trees Goodrich, Tamassia. Red-Black Trees 1

Search Trees (Ch. 9) > = Binary Search Trees 1

B + -trees. Kerttu Pollari-Malmi

Heaps Goodrich, Tamassia. Heaps 1

Binary Search Trees Treesort

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Dynamic Access Binary Search Trees

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Binary Search Trees > = 2014 Goodrich, Tamassia, Goldwasser. Binary Search Trees 1

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

Advanced Tree Structures

COMP171. AVL-Trees (Part 1)

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Red-black trees (19.5), B-trees (19.8), trees

ICS 691: Advanced Data Structures Spring Lecture 3

From (2,4) to Red-Black Trees

Balanced Search Trees

Dynamic Access Binary Search Trees

CS 314H Algorithms and Data Structures Fall 2012 Programming Assignment #6 Treaps Due November 11/14/16, 2012

13.4 Deletion in red-black trees

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Section 4 SOLUTION: AVL Trees & B-Trees

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

CS350: Data Structures B-Trees

CSCI2100B Data Structures Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Binary Search Trees (10.1) Dictionary ADT (9.5.1)

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

CS2210 Data Structures and Algorithms. Lecture 9: AVL TREES definition, properties, insertion

Multi-Way Search Tree

Trees 3/19/14. Mammal. Dog Pig Cat

CS 314H Honors Data Structures Fall 2017 Programming Assignment #6 Treaps Due November 12/15/17, 2017

Analysis of Algorithms

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CS 171: Introduction to Computer Science II. Binary Search Trees

Multi-Way Search Trees

Multi-Way Search Trees

CE 221 Data Structures and Algorithms

Comp 335 File Structures. B - Trees

Binary Trees

Algorithms and Data Structures

COMP Analysis of Algorithms & Data Structures

Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods

CS 206 Introduction to Computer Science II

Recall from Last Time: AVL Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

ECE250: Algorithms and Data Structures AVL Trees (Part A)

B-Trees and External Memory

CS 350 : Data Structures B-Trees

Data Structures Lesson 7

Lecture Overview. Readings. Recall: Binary Search Trees (BSTs) The importance of being balanced. AVL trees. Balance Insert. Other balanced trees

Transcription:

Spla Trees 3/20/14 Presentation for use with the tetbook Data Structures and Algorithms in Java, 6 th edition, b M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wile, 2014 Spla Trees v 6 3 8 4 2013 Goodrich, Tamassia, Goldwasser Spla Trees 1 Spla Trees are Binar Search Trees! BST Rules: n entries stored onl at internal nodes n kes stored at nodes in the left subtree of v are less than or equal to the ke stored at v n kes stored at nodes in the right subtree of v are greater than or equal to the ke stored at v! An inorder traversal will return the kes in order note that two kes of equal value ma be wellseparated Slide b Matt Dickerson all the kes in the blue region are 20 all the kes in the ellow region are 20 2013 Goodrich, Tamassia, Goldwasser Spla Trees 2 1

Spla Trees 3/20/14 Searching in a Spla Tree: Starts the Same as in a BST! Search proceeds down the tree to found item or an eternal node.! Eample: Search for time with ke 11. Slide b Matt Dickerson 2013 Goodrich, Tamassia, Goldwasser Spla Trees 3 Eample Searching in a BST, continued! search for ke 8, ends at an internal node. Slide b Matt Dickerson 2013 Goodrich, Tamassia, Goldwasser Spla Trees 4 2

Spla Trees 3/20/14 Spla Trees do Rotations after Ever Operation (Even Search)! new operation: spla n splaing moves a node to the root using rotations n right rotation n left rotation n makes the left child of a node n makes the right child of a node into s parent; becomes the into s parent; becomes the left right child of child of a right rotation about a left rotation about T 2 T 2 (structure of tree above is not modified) T 2 (structure of tree above is not modified) T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 5 start with node Splaing: is the root? no is a child of the root? es is the left child of the root? es ig es no no ig stop n is a left-left grandchild means is a left child of its parent, which is itself a left child of its parent n p is s parent; g is p s parent is a left-left grandchild? is a right-right grandchild? is a right-left grandchild? is a left-right grandchild? right-rotate about g, right-rotate about p left-rotate about g, left-rotate about p left-rotate about p, right-rotate about g right-rotate left-rotate about es right-rotate about p, about the root the root left-rotate about g 2013 Goodrich, Tamassia, Goldwasser Spla Trees Slide b Matt Dickerson 6 es es es ig-ig ig-ig ig-ag ig-ag 3

Spla Trees 3/20/14 Visualiing the Splaing Cases ig-ag T 2 ig-ig T 2 T 2 ig w w T 2 T 2 T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 7 Splaing Eample! let = n is the right child of its parent, which is the left child of the grandparent n left-rotate around p, then rightrotate around g p g 1. (before rotating) Slide b Matt Dickerson g p p g 2. (after first rotation) 3. is not et the root, so 2013 Goodrich, Tamassia, Goldwasser Spla Trees we spla again 8 (after second rotation) 4

Spla Trees 3/20/14 Splaing Eample, Continued! now is the left child of the root n right-rotate around root 1. (before appling rotation) 2. (after rotation) is the root, so stop 2013 Goodrich, Tamassia, Goldwasser Spla Trees Slide b Matt Dickerson 9 Slide b Matt Dickerson Eample Result of Splaing! tree might not be more balanced! e.g. spla n before, the depth of the shallowest leaf is 3 and the deepest is 7 n after, the depth of shallowest leaf is 1 and deepest is 8 before after first spla 2013 Goodrich, Tamassia, Goldwasser Spla Trees 10 after second spla 5

Spla Trees 3/20/14 Spla Tree Definition! a spla tree is a binar search tree where a node is splaed after it is accessed (for a search or update) n deepest internal node accessed is splaed n splaing costs O(h), where h is height of the tree which is still O(n) worst-case w O(h) rotations, each of which is O(1) 2013 Goodrich, Tamassia, Goldwasser Spla Trees 11 Spla Trees & Ordered Dictionaries! which nodes are splaed after each operation? method Search for k spla node if ke found, use that node if ke not found, use parent of ending eternal node Insert (k,v) Remove item with ke k use the new node containing the entr inserted use the parent of the internal node that was actuall removed from the tree (the parent of the node that the removed item was swapped with) 2013 Goodrich, Tamassia, Goldwasser Spla Trees 12 6

Spla Trees 3/20/14 Amortied Analsis of Spla Trees! Running time of each operation is proportional to time for splaing.! Define rank(v) as the logarithm (base 2) of the number of nodes in subtree rooted at v.! Costs: ig = $1, ig-ig = $2, ig-ag = $2.! Thus, cost for plaing a node at depth d = $d.! Imagine that we store rank(v) cber-dollars at each node v of the spla tree (just for the sake of analsis). 2013 Goodrich, Tamassia, Goldwasser Spla Trees 13 Cost per ig ig w w T 2 T 2! Doing a ig at costs at most rank () - rank(): n cost = rank () + rank () - rank() - rank() < rank () - rank(). 2013 Goodrich, Tamassia, Goldwasser Spla Trees 14 7

Spla Trees 3/20/14 Cost per ig-ig and ig-ag ig-ig T 2 T 2! Doing a ig-ig or ig-ag at costs at most 3(rank () - rank()) - 2 ig-ag T 2 T 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 15 Cost of Splaing! Cost of splaing a node at depth d of a tree rooted at r: n at most 3(rank(r) - rank()) - d + 2: n Proof: Splaing takes d/2 splaing substeps: cost d / 2 i= 1 d / 2 i= 1 cost i (3(rank ( ) rank = 3(rank( r) rank ( )) 2( d / d ) + 2 3(rank( r) rank( )) d + 2. ( )) 2) + 2 2013 Goodrich, Tamassia, Goldwasser Spla Trees 16 i 0 i 1 8

Spla Trees 3/20/14 Performance of Spla Trees! Recall: rank of a node is logarithm of its sie.! Thus, amortied cost of an spla operation is O(log n)! In fact, the analsis goes through for an reasonable definition of rank()! This implies that spla trees can actuall adapt to perform searches on frequentlrequested items much faster than O(log n) in some cases 2013 Goodrich, Tamassia, Goldwasser Spla Trees 17 Java Implementation 2013 Goodrich, Tamassia, Goldwasser Spla Trees 18 9

Spla Trees 3/20/14 Java Implementation 2013 Goodrich, Tamassia, Goldwasser Spla Trees 19 10