CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 10 / 2009 Instructor: Michael Eckmann.
Advertisements

AVL-Trees (Part 2) COMP171. AVL Trees / Slide 2 A warm-up exercise … * Create a BST from a sequence, n A, B, C, D, E, F, G, H * Create a AVL tree for.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
CPSC 252 AVL Trees Page 1 AVL Trees Motivation: We have seen that when data is inserted into a BST in sorted order, the BST contains only one branch (it.
CS261 Data Structures AVL Trees. Goals Pros/Cons of a BST AVL Solution – Height-Balanced Trees.
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture27.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
CS202 - Fundamental Structures of Computer Science II
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Lecture 13 AVL Trees King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
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):
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
AVL Search Trees What is an AVL Tree? AVL Tree Implementation. Why AVL Trees? Rotations. Insertion into an AVL tree Deletion from an AVL tree.
CSC 2300 Data Structures & Algorithms February 13, 2007 Chapter 4. Trees.
AVL Trees ITCS6114 Algorithms and Data Structures.
AVL Trees Data Structures Fall 2006 Evan Korth Adopted from a presentation by Simon Garrett and the Mark Allen Weiss book.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Binary Search Trees CSE 331 Section 2 James Daly.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
CS 146: Data Structures and Algorithms July 14 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
AVL Trees An AVL tree is a binary search tree with a balance condition. AVL is named for its inventors: Adel’son-Vel’skii and Landis AVL tree approximates.
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
CSIT 402 Data Structures II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
CMPE 180A Data Structures and Algorithms in C++ April 26 Class Meeting
AVL Trees CENG 213 Data Structures.
AVL Trees: AVL Trees: Balanced binary search tree
CS202 - Fundamental Structures of Computer Science II
AVL Trees CSE 373 Data Structures.
CSE 332: Data Abstractions AVL Trees
CS202 - Fundamental Structures of Computer Science II
ITCS6114 Algorithms and Data Structures
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CS 146: Data Structures and Algorithms June 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 2 Binary Search Trees  A binary search tree has these properties for each of its nodes: All the values in the node's left subtree is less than the value of the node itself. All the values in the node's right subtree is greater than the value of the node itself.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 3 AVL Trees  An AVL tree is a binary search tree (BST) with a balance condition. Named after its inventors, Adelson-Velskii and Landis.  For each node of the BST, the heights of its left and right subtrees can differ by at most 1. Remember that the height of a tree is the length of the longest path from the root to a leaf. The height of the root = the height of the tree. The height of an empty tree is -1.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 4 AVL Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 5 Balancing AVL Trees  We need to rebalance the tree whenever the balance condition is violated. We need to check after every insertion and deletion. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 6 Balancing AVL Trees, cont’d  Assume the tree was balanced before an insertion.  If it became unbalanced due to the insertion, then the inserted node must have caused some nodes between itself and the root to be unbalanced.  An unbalanced node must have the height of one of its subtrees exactly 2 greater than the height its other subtree.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 7 Balancing AVL Trees, cont’d  Let the deepest unbalanced node be α.  Any node has at most two children.  A new height imbalance means that the heights of α ’s two subtrees now differ by 2. α 1234 α

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 8 Balancing AVL Trees, cont’d  Therefore, one of the following had to occur: Case 1 (outside left-left): The insertion was into the left subtree of the left child of α. Case 2 (inside left-right): The insertion was into the right subtree of the left child of α. Case 3 (inside right-left): The insertion was into the left subtree of the right child of α. Case 4 (outside right-right): The insertion was into the right subtree of the right child of α α Cases 1 and 4 are mirrors of each other, and cases 2 and 3 are mirrors of each other.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 9 Balancing AVL Trees: Case 1  Case 1 (outside left-left): Rebalance with a single right rotation. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 10 Balancing AVL Trees: Case 1, cont’d  Case 1 (outside left-left): Rebalance with a single right rotation. Node A is unbalanced. Single right rotation: A's left child B becomes the new root of the subtree. Node A becomes the right child and adopts B's right child as its new left child. Node 8 is unbalanced. Single right rotation: 8's left child 7 becomes the new root of the subtree. Node 8 is the right child. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 11 Balancing AVL Trees: Case 4  Case 4 (outside right-right): Rebalance with a single left rotation. Node A is unbalanced. Single left rotation: A's right child C becomes the new root of the subtree. Node A becomes the left child and adopts C's left child as its new right child. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 12 Balancing AVL Trees: Case 2  Case 2 (inside left-right): Rebalance with a double left-right rotation. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 13 Balancing AVL Trees: Case 2, cont’d  Case 2 (inside left-right): Rebalance with a double left-right rotation. Node A is unbalanced. Double left-right rotation: E becomes the new root of the subtree after two rotations. Step 1 is a single left rotation between B and E. E replaces B as the subtree root. B becomes E's left child and B adopts E's left child F as its new right child. Step 2 is a single right rotation between E and A. E replaces A is the subtree root. A becomes E's right child and A adopts E's right child G as its new left child.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 14 Balancing AVL Trees: Case 3  Case 3 (inside right-left): Rebalance with a double right-left rotation. Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 15 Balancing AVL Trees: Case 3, cont’d  Case 3 (inside right-left): Rebalance with a double right-left rotation. Node A is unbalanced. Double right-left rotation: D becomes the new root of the subtree after two rotations. Step 1 is a single right rotation between C and C. D replaces C as the subtree root. C becomes D's right child and C adopts D's right child G as its new left child. Step 2 is a single left rotation between D and A. D replaces A is the subtree root. A becomes D's left child and A adopts D's left child F as its new right child.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 16 AVL Tree Implementation  Since an AVL tree is just a BST with a balance condition, it makes sense to make the AVL tree class a subclass of the BST class.  Both classes can share the same BinaryNode class. public class AvlTree extends BinarySearchTree

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 17 The AVL Tree Node  With so many height calculations, it makes sense to store each node's height in the node itself. public class BinaryNode { private int data; // data in this node private int height; // height of this node private BinaryNode left; // left child private BinaryNode right; // right child... }

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 18 AVL Tree Implementation, cont’d  Class AVLTree overrides the insert() and remove() methods of class BinarySearchTree. Each method calls the superclass's method and wraps the result in a call to the balance() method. protected BinaryNode insert(int data, BinaryNode node) { return balance(super.insert(data, node)); } protected BinaryNode remove(int data, BinaryNode node) { return balance(super.remove(data, node)); }

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 19 AVL Tree Implementation, cont’d  The private AVLTree method balance() checks whether the balance condition still holds, and rebalances the tree with rotations whenever necessary.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 20 AVL Tree Implementation, cont’d private BinaryNode balance(BinaryNode node) {... if (height(node.getLeft()) - height(node.getRight()) > 1) { if (height(node.getLeft().getLeft()) >= height(node.getLeft().getRight())) { node = singleRightRotation(node); } else { node = doubleLeftRightRotation(node); } } else if (height(node.getRight()) - height(node.getLeft()) > 1) { if (height(node.getRight().getRight()) >= height(node.getRight().getLeft())) { node = singleLeftRotation(node); } else { node = doubleRightLeftRotation(node); } }... return node; } Case 1 Case 2 Case 4 Case 3

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 21 AVL Tree Implementation, cont’d private BinaryNode singleRightRotation(BinaryNode k2) { BinaryNode k1 = k2.getLeft(); k2.setLeft(k1.getRight()); k1.setRight(k2); k2.setHeight(Math.max(height(k2.getLeft()), height(k2.getRight())) + 1); k1.setHeight(Math.max(height(k1.getLeft()), k2.getHeight()) + 1); return k1; } private BinaryNode singleLeftRotation(BinaryNode k1) { BinaryNode k2 = k1.getRight(); k1.setRight(k2.getLeft()); k2.setLeft(k1); k1.setHeight(Math.max(height(k1.getLeft()), height(k1.getRight())) + 1); k2.setHeight(Math.max(height(k2.getRight()), k1.getHeight()) + 1); return k2; } Case 1 Case 4

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 22 AVL Tree Implementation, cont’d private BinaryNode doubleLeftRightRotation(BinaryNode k3) { k3.setLeft(singleLeftRotation(k3.getLeft())); return singleRightRotation(k3); } private BinaryNode doubleRightLeftRotation(BinaryNode k1) { k1.setRight(singleRightRotation(k1.getRight())); return singleLeftRotation(k1); } Case 2 Case 3

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Break 23

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 24 Assignment #3  This assignment will give you practice with binary search trees (BST) and AVL trees.  You are provided a TreePrinter class that has a print() method that will print any arbitrary binary tree. A template for how it prints a tree: xx /\ / \ xx xx /\ /\ / \ / \ xx xx xx xx /\ /\ /\ /\ / \ / \ / \ / \ xx xx xx xx xx xx xx xx /\ /\ /\ /\ /\ /\ /\ /\ / \ / \ / \ / \ / \ / \ / \ / \ xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 25 Assignment #3, cont’d  TreePrinter is able to print trees with height up to 5, i.e., 32 node values on the bottom row. An example of an actual printed tree: 24 /\ / \ /\ /\ / \ / \ / /\ \ / / \ \ \ 64 TreePrinter.java

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: First Part  The first part of the assignment makes sure that you can successfully insert nodes into, and delete nodes from, a binary search tree (BST) and an AVL tree. 26

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 27 Assignment #3: First Part, cont’d  First generate a random BST that has height 5 and contains random values from 10 through 99. You may have to generate dozens of trees until you get one that's exactly height 5. Don't worry that the tree is unbalanced. Print the tree.  Now repeatedly delete the root of the tree. Print the tree after each deletion to verify that you did the deletion correctly. Stop when the tree becomes empty.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 28 Assignment #3: First Part, cont’d  Second, create an AVL tree node by node. Generate 35 unique random integers to insert into the tree. Print the tree after each insertion to verify that you are keeping it balanced. Each time you do a rebalancing, print a message indicating which rotation operation and which node.  Example:  As you did with the BST, repeatedly delete the root of your AVL tree. Print the tree after each deletion to verify that you are keeping it balanced. Double left-right rotation: 76

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 29 Assignment #3: First Part, cont’d  A handy AVL tree balance checker: private int checkBalance(BinaryNode node) throws Exception { if (node == null) return -1; if (node != null) { int leftHeight = checkBalance(node.getLeft()); int rightHeight = checkBalance(node.getRight()); if ( (Math.abs(height(node.getLeft()) - height(node.getRight())) > 1) || (height(node.getLeft()) != leftHeight) || (height(node.getRight()) != rightHeight)) { throw new Exception("Unbalanced trees."); } return height(node); } Demo

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: Second Part  The second part of the assignment compares the performance of a BST vs. an AVL tree. 30

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: Second Part  First, generate n random integers. n is some large number, explained on the next slide.  Time and print how long it takes to insert the random integers one at a time into an initially empty BST. Do not print the tree after each insertion.  Time and print how long it takes to insert the same random integers one at a time into an initially empty AVL tree. Do not print the tree after each insertion. 31

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: Second Part, cont’d  Choose a value of n large enough to give you consistent timings that you can compare. Try values of n = 1,000 10, ,000 1,000,000  If T(n) is the time function, how does the growth of T BST (n) compare with the growth of T AVL (n) ? 32

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: Second Part, cont’d  Second, generate k random integers. k is some large value.  Time how long it takes to search your n -node BST for all k random integers. It doesn’t matter whether or not the search succeeds.  Time how long it takes to search your n -node AVL tree for the same k random integers.  Compare the grow rates of these two time functions. 33

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak Assignment #3: Second Part, cont’d  Third, perform a random mixture of m insertions and searches on your BST and then on your n -node AVL tree. m is some large number. Perform the same sequence of insertions and searches on both trees.  Try different ratios of insertions vs. searches.  Empirically estimate the ratio where an AVL tree has better performance than a BST for a mixture of insertions and searches. 34

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 35 Assignment #3, cont’d  You can use any code from the lectures or from the textbook.  You do not have to use parameterized generic types. You can use raw (nongeneric) types, or.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 36 Assignment #3, cont’d  You may choose a partner to work with you on this assignment. Both of you will receive the same score.  Create a zip file containing: Your Java source files. Any instructions on how to build and run your code. Text files containing your outputs. A 1- or 2-page that briefly describes your conclusions from doing this assignment.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 37 Assignment #3, cont’d  the zip file to Subject line: CS 146 Assignment #3: Your Name(s)  If you work with a partner, both of you turn in one assignment. CC your partner's address so I can “reply all” to you both.  Due Friday, July 3.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 38 Splay Trees  Not a new type of tree, but a reimplementation of the BST insert, delete, and search methods. The goal is to improve their performance.  No single operation on a splay tree is guaranteed to have better performance. But a series of m operations will take O(m log n) time for a tree of n nodes, whenever m > n.  Not highly balanced like an AVL tree. Lowering the cost of an entire series of operations is more important then keeping the tree balanced.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 39 Splay Trees, cont’d  Whenever a splay tree node is accessed, the tree performs splaying operations that moves the accessed node to the root of the tree.  Splaying a node consists of a series of rotations. Similar to AVL tree rotations.  The goal is to move the accessed node to the root.  A side benefit is to make the tree more balanced.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 40 Splay Trees, cont’d  The theory is that once a node has been accessed, it will soon be accessed again.  Future accesses are fast if the node is the root.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 41 Splay Trees, cont’d  How is a worst-case BST created ? When all the nodes are entered in sorted order. Suppose the bottom node is accessed in such a tree: Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 42 Splay Trees, cont’d  If a node hasn’t been accessed in a while, then the next time it’s accessed, you pay the performance penalty of splaying.  But accesses of that node in the near future will be very fast.  And so we amortize the cost of splaying over future operations.

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 43 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 44 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 45 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 46 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 47 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 48 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 49 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 50 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012

Computer Science Dept. Summer 2015: June 23 CS 146: Data Structures and Algorithms © R. Mak 51 Splay Trees, cont’d Data Structures and Algorithms in Java, 3 rd ed. by Mark Allen Weiss Pearson Education, Inc., 2012