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

Similar documents
AVL Trees. Version of September 6, AVL Trees Version of September 6, / 22

Advanced Tree Structures

Search Trees. Chapter 11

AVL Trees. Reading: 9.2

CS 380 ALGORITHM DESIGN AND ANALYSIS

Splay Trees. Splay Trees 1

CSC 263 Lecture 4. September 13, 2006

Splay Trees Goodrich, Tamassia, Dickerson. Splay Trees 1

Lecture 6: Analysis of Algorithms (CS )

Search Trees. Undirected graph Directed graph Tree Binary search tree

Binary Heaps in Dynamic Arrays

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)

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

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

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

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

CPSC 223 Algorithms & Data Abstract Structures

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

A red-black tree is a balanced binary search tree with the following properties:

Section 4 SOLUTION: AVL Trees & B-Trees

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

Balanced search trees

CPSC 223 Algorithms & Data Abstract Structures

Properties of red-black trees

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

Lecture: Analysis of Algorithms (CS )

Binary Trees. Binary Search Trees

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Algorithms. AVL Tree

Lecture 16 Notes AVL Trees

CS Transform-and-Conquer

Dynamic Access Binary Search Trees

Trees. Prof. Dr. Debora Weber-Wulff

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

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

CISC 235: Topic 4. Balanced Binary Search Trees

Lecture 5: Sorting Part A

Lecture Notes on AVL Trees

Trees. Eric McCreath

Dynamic Access Binary Search Trees

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

13.4 Deletion in red-black trees

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

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

Lesson 21: AVL Trees. Rotation

Partha Sarathi Manal

Balanced Binary Search Trees

Data Structure. Chapter 10 Search Structures (Part II)

CS711008Z Algorithm Design and Analysis

Algorithms. Red-Black Trees

CS711008Z Algorithm Design and Analysis

CSE 548: Analysis of Algorithms. Lectures 14 & 15 ( Dijkstra s SSSP & Fibonacci Heaps )

Red-Black-Trees and Heaps in Timestamp-Adjusting Sweepline Based Algorithms

9. Which situation is true regarding a cascading cut that produces c trees for a Fibonacci heap?

Balanced Search Trees. CS 3110 Fall 2010

Lecture 5: Scheduling and Binary Search Trees

A data structure and associated algorithms, NOT GARBAGE COLLECTION

Recall from Last Time: AVL Trees

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

BM267 - Introduction to Data Structures

Sorting and Searching

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

LECTURE 18 AVL TREES

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

On the power of structural violations in priority queues

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Heaps. Heapsort. [Reading: CLRS 6] Laura Toma, csci2000, Bowdoin College

Heaps and Priority Queues

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

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

Augmenting Data Structures

CS350: Data Structures AVL Trees

Lecture 13: AVL Trees and Binary Heaps

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

Balanced Binary Search Trees. Victor Gao

3137 Data Structures and Algorithms in C++

Sorting and Searching

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

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

Properties of a heap (represented by an array A)

The Heap Data Structure

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

Programming II (CS300)

ICS 691: Advanced Data Structures Spring Lecture 3

13.4 Deletion in red-black trees

Algorithm Theory, Winter Term 2015/16 Problem Set 5 - Sample Solution

Inf 2B: AVL Trees. Lecture 5 of ADS thread. Kyriakos Kalorkoti. School of Informatics University of Edinburgh

Level-Balanced B-Trees

Data Structures and Algorithms

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

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

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

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Graduate Algorithms CS F-07 Red/Black Trees

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

Transcription:

alanced inar Search Trees Lecture Overview The importance of being balanced VL trees Definition alance Insert Other balanced trees Data structures in general Readings LRS hapter. and. (but different approach: red-blac trees) Recall: inar Search Trees (STs) rooted binar tree each node has e left pointer right pointer parent pointer See Fig. 4 0 9 50 6 Figure : Heights of nodes in a ST

Figure : ST propert ST propert (see Fig. ). height of node = length ( edges) of longest downward path to a leaf (see LRS.5 for details). The Importance of eing alanced: STs support insert, min, delete, ran, etc. in O(h) time, where h = height of tree (= height of root). h is between lg(n) and n: Fig. ). vs. Perfectl alanced Path Figure : alancing STs balanced ST maintains h = O(lg n) all operations run in O(lg n) time.

Lecture 4 alanced inar Search Trees 6.006 Spring 008 VL Trees: Definition VL trees are self-balancing binar search trees. These trees are named after their two inventors G.M. del son-vel sii and E.M. Landis n VL tree is one that requires heights of left and right children of ever node to differ b at most ±. This is illustrated in Fig. 4) - Figure 4: VL Tree oncept In order to implement an VL tree, follow two critical steps: Treat nil tree as height. Each node stores its height. This is inherentl a DT STRUTURE UGMENTTION procedure, similar to augmenting subtree size. lternativel, one can just store difference in heights. good animation applet for VL trees is available at this lin. To compare inar Search Trees and VL balancing of trees use code provided here. Original Russian article: delson-velsii, G.; E. M. Landis (96). n algorithm for the organization of information. Proceedings of the USSR cadem of Sciences 46: 666. (English translation b Mron J. Ricci in Soviet Math. Dolad, :596, 96.)

Lecture 4 alanced inar Search Trees 6.006 Spring 008 alance: The balance is the worst when ever node differs b. Let N h = min ( nodes). N h = N h + N h + lternativel: > N h N h > h/ = h < lg h N h > F n (n th Fibonacci number) In fact,n h = F n+ (simple induction) h F h = 5 (rounded to nearest integer) + 5 where =.68 = mah log (n).440 lg(n) (golden ratio) VL Insert:. insert as in simple ST. wor our wa up tree, restoring VL propert (and updating heights as ou go). Each Step: suppose is lowest node violating VL assume is right-heav (left case smmetric) if s right child is right-heav or balanced: follow steps in Fig. 5 else follow steps in Fig. 6 then continue up to s grandparent, greatgrandparent... 4

Lecture 4 alanced inar Search Trees 6.006 Spring 008 - + Left-Rotate() - - - - z + Left-Rotate() + - Figure 5: VL Insert alancing + Right-Rotate(z) - z + Left-Rotate() z D - - - or - D - - or - Figure 6: VL Insert alancing 5

Lecture 4 alanced inar Search Trees 6.006 Spring 008 Eample: n eample implementation of the VL Insert process is illustrated in Fig. 7 Insert() 4 = 9: left-left case 4 0 0 9 50 50 9 6 Done 4 6 Insert(55) 4 0 0 50 6 50 6 9 9 =: left-right case 4 Done 4 0 0 55 6 9 50 55 50 6 9 Figure 7: Illustration of VL Tree Insert Process omment. In general, process ma need several rotations before an Insert is completed. omment. Delete(-min) harder but possible. 6

Lecture 4 alanced inar Search Trees 6.006 Spring 008 alanced Search Trees: There are man balanced search trees. VL Trees del son-velsii and Landis 96 -Trees/--4 Trees aer and Mcreight 97 (see LRS 8) [α] Trees Nievergelt and Reingold 97 Red-blac Trees LRS hapter Spla-Trees Sleator and Tarjan 985 Sip Lists Pugh 989 Scapegoat Trees Galperin and Rivest 99 Treaps Seidel and ragon 996 Note. Sip Lists and Treaps use random numbers to mae decisions fast with high probabilit. Note. Spla Trees and Scapegoat Trees are amortized : adding up costs for several operations = fast on average. 7

Lecture 4 alanced inar Search Trees 6.006 Spring 008 Spla Trees Upon access (search or insert), move node to root b sequence of rotations and/or doublerotations (just lie VL trees). Height can be linear but still O(lg n) per operation on average (amortized) Note: We will see more on amortization in a couple of lectures. Optimalit For STs, cannot do better than O(lg n) per search in worst case. In some cases, can do better e.g. in-order traversal taes Θ(n) time for n elements. put more frequent items near root onjecture: Spla trees are O(best ST) for ever access pattern. With fancier trics, can achieve O(lg lg u) performance for integers u [Van Ernde oas; see 6.854 or 6.85 (dvanced Data Structures)] ig Picture: bstract Data Tpe(DT): interface spec. e.g. Priorit Queue: Q = new-empt-queue() Q.insert() = Q.deletemin() vs. Data Structure (DS): algorithm for each op. There are man possible DSs for one DT. One eample that we will discuss much later in the course is the heap priorit queue. 8