Dynamic Access Binary Search Trees

Similar documents
Dynamic Access Binary Search Trees

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

COMP171. AVL-Trees (Part 1)

Chapter 22 Splay Trees

Algorithms. AVL Tree

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

ICS 691: Advanced Data Structures Spring Lecture 3

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

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

Red-Black, Splay and Huffman Trees

CSE 326: Data Structures Splay Trees. James Fogarty Autumn 2007 Lecture 10

Ch04 Balanced Search Trees

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Balanced Binary Search Trees. Victor Gao

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Recall from Last Time: AVL Trees

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

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

Analysis of Algorithms

CS 261 Data Structures. AVL Trees

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

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

Data Structures Week #6. Special Trees

Self Adjusting Data Structures

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

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

Search Trees. Chapter 11

CMPE 160: Introduction to Object Oriented Programming

Balanced BST. Balanced BSTs guarantee O(logN) performance at all times

Data Structures Week #6. Special Trees

CS350: Data Structures AVL Trees

AVL Trees Heaps And Complexity

8. Binary Search Tree

Trees. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Binary Search Trees. Analysis of Algorithms

CSI33 Data Structures

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

lecture17: AVL Trees

Section 1: True / False (1 point each, 15 pts total)

CISC 235: Topic 4. Balanced Binary Search Trees

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

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

Data Structures Lesson 7

CSCI2100B Data Structures Trees

Recall: Properties of B-Trees

8.1. Optimal Binary Search Trees:

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

CS Transform-and-Conquer

AVL Trees. See Section 19.4of the text, p

Advanced Tree Data Structures

Data Structures Week #6. Special Trees

Binary Trees, Binary Search Trees

CHAPTER 10 AVL TREES. 3 8 z 4

Splay tree, tree Marko Berezovský Radek Mařík PAL 2012

Overview. Tree data structure Binary search trees. STL set and map classes B-trees for accessing secondary storage Applications

CS350: Data Structures Red-Black Trees

Part 2: Balanced Trees

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

A Splay Tree Implementation

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Balanced Search Trees. CS 3110 Fall 2010

Trees. R. J. Renka 10/14/2011. Department of Computer Science & Engineering University of North Texas. R. J. Renka Trees

Programming II (CS300)

DDS Dynamic Search Trees

Data Structures and Algorithms(12)

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

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

Lecture 23: Binary Search Trees

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Splay tree, tree Marko Berezovský Radek Mařík PAL 2012

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

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

The Complexity of Splay Trees and Skip Lists

CS 310: Tree Rotations and AVL Trees

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Balanced Binary Search Trees

More Binary Search Trees AVL Trees. CS300 Data Structures (Fall 2013)

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

More BSTs & AVL Trees bstdelete

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

AVL trees and rotations

CS 61B Data Structures and Programming Methodology. Aug 11, 2008 David Sun

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Lecture 6: Analysis of Algorithms (CS )

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Algorithms. Deleting from Red-Black Trees B-Trees

Self-Balancing Search Trees. Chapter 11

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

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

Advanced Set Representation Methods

Analysis of Algorithms

Randomized Splay Trees Final Project

Trees. Eric McCreath

9. Heap : Priority Queue

Lesson 21: AVL Trees. Rotation

INF2220: algorithms and data structures Series 1

CSE 326: Data Structures Lecture #8 Binary Search Trees

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Transcription:

Dynamic Access Binary Search Trees 1 * are self-adjusting binary search trees in which the shape of the tree is changed based upon the accesses performed upon the elements. When an element of a splay tree is accessed the node with the element is percolated, (i.e. splayed), up to the root by applying AVL rotations. The more frequently accessed items will conglomerate near the top of the tree. This results in excellent performance when a subset of the tree elements are accessed much more frequently then other elements. If all elements of the tree have equal access probability than a balanced binary search tree should be used. Splay trees do NOT store any balancing information in the nodes. *D. D. Sleator and R. E. Tarjan, 1985.

Amortized Analysis 2 Amortized analysis: the complexity is analyzed based upon a sequence of operations instead of single individual operations. Amortized analysis takes into account the impact of operations upon following operations. Infrequently occurring costly individual operations may not have much effect on the overall performance if they are far outweighed by the less inexpensive frequently executed operations. Formally, if a sequence of M operations has total worst-case running time O(M f(n)), we say that the amortized running time is O(f(N)) [Weiss]. A series of splay tree M operations takes at most O(M log N). Thus a splay tree has an amortized cost of O(log N). A single operation may take (N) time, but be outweighed by other operations in a series. The amortized bound of O(log N) is not as strong of a bound as a worst-case O(log N) per operation bound. *G. M. Adelson-Velskii and E. M. Landis, 1962.

Zig-Zag Consider accessing the value 65 in the BST tree: 3 Zig-Zag: left child of parent right child of grandparent right rotation(child, parent) left rotation(child, grandparent) 65 75 55 68 The zig-zag case occurs when the accessed item is the left child of its parent and the parent is a right child. The item is rotated with its parent, and then its grandparent. In this example the rotations are accomplished by a right rotation followed by a left rotation. The symmetric case is zag-zig in which the accessed item is the right child of its parent and the parent is a left child.

Zig-Zag 4 Right rotation with parent. 65 55 68 75 65 Left rotation with grandparent. 55 68 75 Splaying approximately halves the depth of other nodes on the access path.

Zag-Zig Consider accessing the value in the BST tree: 5 Zag-Zig: right child of parent left child of grandparent left rotation(child, parent) right rotation(child, grandparent) 65 75 55 68 The zag-zig case occurs when the accessed item is the right child of its parent and the parent is a left child. The item is left rotated with its parent, and then right rotated with its grandparent.

Zag-Zig 6 Left rotation with parent. Right rotation with grandparent. 65 75 55 68 65 75 55 68 Splaying approximately halves the depth of other nodes on the access path.

Zig-Zig Consider accessing the value in the BST tree: 65 Zig-Zig: left child of parent left child of grandparent right rotation(parent,grandparent) right rotation(child, parent) 7 55 68 75 The zig-zig case occurs when the accessed item is the left child of its parent and the parent is also a left child. The parent is first rotated with the grandparent, and then the accessed items node is rotated with its parent. In this example the rotations are accomplished by a right rotation followed by another right rotation. The symmetric case is zag-zag in the accessed item is the right child of its parent and the parent is also a right.

Zig-Zig 8 Right rotation between parent and grandparent. 65 Right rotation with parent. 55 68 75 55 65 Splaying rotations continue until the root is reached. If the node is one level below the root only one rotation (zig/right or zag/left) is performed. 68 75

Zag-Zag Consider accessing the value 75 in the BST tree: 65 Zag-Zag: right child of parent right child of grandparent left rotation(parent) left rotation(grandparent) 9 55 68 75 The zag-zag case occurs when the accessed item is the right child of its parent and the parent is also a right child. The parent is first left rotated with the grandparent, and then the accessed items node is left rotated with its parent.

Zag-Zag 10 Left rotation between parent and grandparent. 65 68 75 Left rotation with parent. 75 55 65 68 55

Modification Deletion First access the node to force it to percolate to the root. Remove the root. 11 Determine the largest item in the left subtree and rotate it to the root of the left subtree which results in the left subtree new root having no right child. The right subtree is then added as the right child of the new root. Insertion Starts the same as a BST with the new node created at a leaf. The new leaf node is splayed until it becomes the root. Splaying also occurs on unsuccessful searches. The last item on the access path is splayed in this case.

Pros & Cons Pros: Simpler coding than AVL trees Average amortized cost is roughly the same as a balanced BST. No balance bookkeeping information need be stored. Rotations on long access paths tend to reduce search cost in the future. Most accesses occur near the root minimizing the number of rotations. 12 Cons: Height can degrade to be linear. (QTP: when would this occur?) Tree changes when only a search is performed. In a concurrent system multiple processes accessing the same splay tree can result in critical region problems.

Top-Down Bottom-Up Splaying The previous splay tree description is known as a bottom-up splay tree since the tree restructuring is performed back up the access towards the root. 13 Top-Down Splaying An alternate implementation involves performing the tree restructuring as one proceeds down the access path. Alleviates the need to maintain the parent pointers using the recursive runtime stack. As the search down the access path occurs three trees are maintained: left, middle and right. The left subtree holds nodes in the tree that are less than the target, but not in the middle tree. The right subtree holds nodes that are greater than the target, but not in the middle tree. The middle tree holds the current root of the access path which contains the target if it exists in the tree. There are three cases for maintaining the left, right and middle trees: zig, zig-zig, zigzag. (There are also three symmetric cases: zag, zag-zag, zag-zig.)

Top-Down Splaying: initialization 14 L M R 65 75 55 68 Consider accessing the value 20 in the BST tree:

Top-Down Splaying: zig 15 Zig case: when searching for an item smaller than the left subroot (in the middle tree) and the left subroot has no left child, but does have a right child. L M R The previous middle tree is connected to the right tree as the left child of the smallest item in R, (or as the root if the right tree is empty as is the case here). 65 75 55 68

Top-Down Splaying: zig-zig 16 Zig-Zig case: when searching for an item smaller than the left subroot (in the middle tree) and the left subroot has a left child. Consider an initial insertion of item 20 into the previous tree which yields the following the initial starting position: L M R 20 65 75 55 68

Top-Down Splaying: zig-zig 17 Zig-Zig case: occurs when search determines target is smaller than the left subroot item in the middle tree and the middle tree left subroot is not null. L M 20 R The previous middle tree is connected to the right tree as the right child of the smallest item in R, (or as the root if the right tree is empty as is the case here). 65 75 55 68

Top-Down Splaying: zig-zag 18 Zig-Zag case: when searching for an item greater than the left subroot (in the middle tree). Consider a tree with the initial starting position: L M R 20 65 75 55 68

Top-Down Splaying: zig-zag 19 Zig-Zag case: when searching for an item greater than the left subroot (in the middle tree). Consider a tree with the initial starting position: L M R 20 The left child of the previous middle tree is detached & becomes the root of the middle tree. The previous root of the middle tree is connected to the right tree as the left child of the smallest item in R, (or as the root if the right tree is empty as is the case here). 65 75 55 68

Top-Down Splaying: reconnection 20 Final Tree: once the last splaying dissection step has occurred the tree is reconnected. Consider the possible state below that occurs from the last splaying step: L M R x

Top-Down Splaying: reconnection Final Tree: reconnection results in the following state: root 21 x