Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Search Trees CSE 331 Section 2 James Daly.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
AVL Tree Iris Jiaonghong Shi. AVL tree operations AVL find : – Same as BST find AVL insert : – First BST insert, then check balance and potentially.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
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:
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
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):
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
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.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
Binary Search Trees CSE 331 Section 2 James Daly.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
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.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
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.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
1 CompSci 105 SS 2006 Principles of Computer Science Lecture 17: Heaps cont.
AVL Trees It’s a balancing act. Binary Tree Problems If you get either sorted or reverse-sorted input, you essentially get a linked list (always following.
CompSci 100E 41.1 Balanced Binary Search Trees  Pathological BST  Insert nodes from ordered list  Search: O(___) ?  The Balanced Tree  Binary Tree.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
Jim Anderson Comp 750, Fall 2009 Splay Trees - 1 Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there.
Red-Black Trees Definitions and Bottom-Up Insertion.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
1 CSC TREES AVL & BALANCED TREES. 2 Balanced Trees The advantage of balanced trees is that we can perform most operation in time proportional to.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
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.
Advanced Tree Structures Binary Trees, AVL Tree, Red-Black Tree, B-Trees, Heaps SoftUni Team Technical Trainers Software University
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
AVL Tree: Balanced Binary Search Tree 9.
AA Trees.
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
CMSC 341 Splay Trees.
Lecture 9: Self Balancing Trees
AVL Tree By Rajanikanth B.
CMSC 341 Splay Trees.
Red-black tree properties
Presentation transcript:

Splay Trees CSE 331 Section 2 James Daly

Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight

Review: Binary Tree Every node has at most 2 children Left and right child Variation: n-ary trees have at most n children

Review: Binary Search Tree For every node Left descendents smaller (l ≤ k) Right descendents bigger (r ≥ k) k <k>k

Review: AVL Trees Named for Adelson-Velskii & Landis Self-balancing binary search tree Goal: Keep BST in balance Ability to check / track balance

Review: AVL Trees For every node, the height of both subtrees differs by at most 1.

RotateRight(&t) // Rotates t down to the right // Brings up left child left ← t.left temp = left.right left.right ← t t.left ← temp t ← left

Left-Left / Right-Right Case D B a6a6 c5c5 e5e5 B D e5e5 c5c5 a6a6

Left-Right / Right-Left Case F B a5a5 e4e4 g5g5 D c5c5 D B a5a5 c5c5 F e4e4 g5g5 B a5a5 c5c5 e4e4 D F g5g5

Motivation Frequently we care more about how long it takes to do a string of operations than any one O(n) isn’t too bad – if we do it only once Want O(m log n) for m searches Recently used items are more likely to be called again Basis for caches

Splay Tree Not kept rigorously balanced When a node is accessed, rotate it to the top Next search for the same item will be very quick No need for extra information AVL Tree: Node height Red-Black Tree: Node color

Wrong way k1 k2 k3 k4 k5 A B C D E F

Wrong way k2 k3 k4 k5 k1 A B C D E F

Wrong way k2 k3 k4 k5 k1 A B C D E F

Wrong way k2 k3 k4 k5 k1 A B C D E F

Wrong way k2 k3 k4 k5 k1 A B C D E F

Problem K3 was pushed down almost as far as K2 came up. Easy to show that you could keep selecting bad nodes O(n 2 ) time total. Need a smarter way to do this

Splaying Find X If root, done If parent(X) = root, rotate up Otherwise, X has both parent and grandparent Two cases: Zig-Zag and Zig-Zig

Zig-Zag X P G A B C D X PG A C B D

Zig-Zig X P G A B C D X P G A B C D

Splaying Tends to reduce the height of the tree Many items will be half as deep as before Some items may be at most 2 deeper than before

Right way k1 k2 k3 k4 k5 A B C D E F

Right way k1 k2 k3 k4 k5 A C B D E F

Right way k1 k2 k3 k4 k5 A C B D E F

Insertion Insert X as normal for a BST Splay X to the top

Deletion Remove X as normal for a BST Splay its parent to the top