Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.

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

CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or black.
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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:
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
1 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Red Black Trees Colored Nodes Definition Binary search tree.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Deletion algorithm – Phase 2: Remove node or replace its with successor TreeNode deleteNode(TreeNode n) { // Returns a reference to a node which replaced.
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.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Balanced Binary Search Trees
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
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)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Red-Black Tree Algorithm : Design & Analysis [12].
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
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.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables II.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Binary Search Trees
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Balancing Binary Search Trees
Red Black Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
Red-Black Trees.
Red-Black Trees Motivations
TCSS 342, Winter 2006 Lecture Notes
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Algorithms and Data Structures Lecture VIII
CS202 - Fundamental Structures of Computer Science II
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Overview Dynamic set ADT implemented with BST Balanced search trees AVL trees –Definition –Dynamic set implementation –Complexity of the operation Red-Black trees –Definition –Dynamic set implementation –Complexity of the operation

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Balanced BSTs AVL Trees] (Adelson-Velskii and Landis, 1963). –Guaranteed worst-case Red-Black Trees (Bayer, 1972). –Guaranteed worst- case –Superior to AVL trees in amortized sense (running time on a sequence of set operations). Rotations are used to balance the AVL and RB trees. –Balancing is achieved by applying one or more times LR and/or RL basic rotations. –The rotations do not destroy BST property of the subtree on which they are performed.

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Basic Single Rotations TaTb Tc Ta TbTc v p p v LtoR RtoL Rotate right Rotate left

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms AVL Trees An AVL tree is a BST with –AVL property: the heights of the left and the right subtrees of any vertex differ at most by 1 –The height of an AVL tree containing n vertices is TlTr v h h, h-1, or h+1

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms AVL tree vertices As with BST nodes contain key, data and pointers to parent, left and right children (the latter need to keep track of the link structure) In addition, they have a field, called balance. –For a vertex v, the balance is the difference between the heights of the right and left subtree balance(v)=height(right(v)) - height(left(v)) –For every vertex v in an AVL tree, balance(v) is -1, 0, or 1 –If the balance of a vertex is different than 0,1,-1, the tree is not an AVL tree.

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT with AVL trees The search is a search on a BST – thus time complexity is O(h) – Height of any AVLT with n vertices is log n – Worst-case complexity is Same thing true for min, max, predecessor, successor Insert and Delete –may destroy the balance, thus the must be rebalanced using single or double rotations

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms AVLT Insert 1.Insert as in BST, new nodes are inserted as leaves, this takes O(log n) 2.Retrace back the path from the new node to the root updating balances. This takes O(log n) –If a vertex x is encountered, balance(x)=2, a djust the tree rooted at the vertex by applying rotations so the balance becomes 0, -1 or 1. –Rebalance by a single or a double rotation –The single rotation pattern is given on figure 11.5 in the AVLT handout. –The double rotation pattern is given on figure 11.6 in the AVLT handout – Simplification: –The first vertex during the retrace whose balance was 1 prior the BST insert is the only vertex that can possibly get a new balance of 2. We call it potential pivot. –Only if the balance of the pivot becomes 2 after BST insert, the single or double rotation has to be performed only on the subtree rooted in the potential pivot, and this will rebalance the whole AVLT. 3.AVLT insert is O(log n)

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Single left-rotation (RtoL) pattern application Ta Tc Tb Ta TcTbTcTb h h h 0 +1 BST insert h+1 h h +1 h+2 +2 h+1 0 0

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Double right-left-rotation (LtoR followed by RtoL) BC CB C B B C D A D A D A D A h h-1

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms AVLT Delete 1.Perform delete on BST. This takes O(log n). 2.This could actually delete the predecessor or the successor vertex of the original vertex that had to be deleted 3.Retrace back the path from the parent of the actually deleted vertex to the root updating balances. If a vertex with balance 2 found, adjust by rotations. More then one rebalancing may be needed. Since the total number of rebalances is bounded by the length of the path, the total number of rebalancing steps is O(log n) Simplification: If the balance of the parent of the deleted vertex changed to 1, the height of the subtree rooted at the parent is the same, and the algorithm may terminate (Figure, 11.7). In case the parent's balance went from 1 to 0, a rebalance may or may not be necessary (Figure 11.8). 4.AVLT delete is total O(log n).

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Red-Black Trees: augmenting the BST RBT is an augmented BST that satisfies RBT property. The augmented tree is defined as follows –External leaves are added to all null pointers. –This creates an augmented BST by adding n+1 leaves to the n vertex BST. –The external leaves are used only for the analysis, in practice it is not necessary to allocate memory for them.

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms BST: example E V x N T J L

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Augmented BST: example E V x N T J L

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Red-Black Trees: Definition A Red-Black three is an augmented BST in which every vertex is colored red or black in such a way that the RBT property is satisfied: –Black rule (BR): every leaf is black –Red rule (RR): if a vertex is red, its children are black –Path rule (PR): every path from the root to a leaf contains the same number of black vertices. The path rule is satisfied for any vertex in RBT. Thus it makes sense to define black-height of a vertex bh(v) of v –bh(v) is the number of black nodes on the path from v to a leaf (but excluding v)

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT: example E V x N T J L BR: every leaf is black RR: if a vertex is red, its children are black PR: every path from the root to a leaf contains the same number of black vertices bh(v) is the number of black nodes on the path from v to a leaf (count the leaf, do not count v)

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Red-Black Trees: tree height What bound is the RBT property putting on the height of the three? –The height of RBT with n internal vertices is <= 2log(n+1), since 1.For any v, the subtree rooted in v contains at least vertices. Proof: by induction on the black-height, bh(v) 2. From RR, at least half of the vertices on any path from the root to a leaf must be black, so Since 2log(n+1)<=2log(2n)=2log(n)+2log(2), we have h<=2log(n) + Const, thus h = O(log n)

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT vertices As with BST nodes contain key, data, and pointers to parent, left and right children (the latter need to keep track of the link structure) In addition, a vertex has a field color (1 bit is enough to store it) The color is used to compute the black-height and to check the RBT property

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT with RB trees Search – Perform search on BST. Since the height is O(log n), the search on RBT is O(log n) Min, Max, Predecessor, Successor similarly are O(log n) Insert and Delete may destroy the RBT property, thus to recover the RBT property, the tree has to be adjusted by recoloring and/or rotations

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert 1.Perform insert on BST Nodes inserted as leaves The tree height is O(log n), thus this takes O(log n) For the purpose of the analysis, think that instead of inserting a single node in BST, insert a subtree consisting of a red root (the vertex to be inserted), and two black children (with no data). The BST insert will replace an external leaf in the augmented tree by a new subtree consisting of red root and two black children. This insertion procedure preserves BR and PR rules, but may violate RR

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT: example, insert G E V x N T J L G

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Ex: The tree after BST inserted G E V x N T J L G BST insert violated RR

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert Recover the RBT property. –Let v be is the new insertion in the RBT T v is red Denote: p is parent of v, g = grand parent of v, u = uncle of v (u and p descend from g) 1.If v is the root, the new tree is RBT, done 2.If p is black, the new tree is RBT, done 3.If p is red, then recover RR by recoloring and/or rotations 3.1 If p is the root make v black, done 3.2 Otherwise, p is not the root: –Since p is red, g must be black (by RR). – Since T is augmented BST u exists – The color of u determines the method by which the RR will be recovered v p g u

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert: cont 3.1 Recover the RBT property. –3.2, p is red, v is red, g is black, color of u will determine the method by which to recover RBT If u is red, recolor: –If, g's parent is black, recolor as follows g red, p black, u black; done. –If g’s parent is red, »recolor again g red, p black, u black ; »now the problem that we had with the coloring of v and p (both red) is moved up at g and g’s parent (now these two are both red after the recoloring). »recursively apply the recoloring algorithm to (g, g’s parent) »In the worst case the recoloring will propagate all the way back to the root, O(log n) v p g u RR violated

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert: cont 3.1 Recover the RBT property. –3.2, p is red, v is red, g is black, color of u will determine the method by which to recover RBT If u is red, recolor: –If, g's parent is black, recolor as follows g red, p black, u black; done. –If g’s parent is red, »recolor again g red, p black, u black ; »now the problem that we had with the coloring of v and p (both red) is moved up at g and g’s parent (now these two are both red after the recoloring). »recursively apply the recoloring algorithm to (g, g’s parent) »In the worst case the recoloring will propagate all the way back to the root, O(log n) v p g u

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert: cont 3.1 Recover the RBT property. –3.2, p is red, v is red, g is black, color of u will determine the method by which to recover RBT If u is red, recolor: –If, g's parent is black, recolor as follows g red, p black, u black; done. –If g’s parent is red, »recolor again g red, p black, u black ; »now the problem that we had with the coloring of v and p (both red) is moved up at g and g’s parent. »recursively apply the recoloring algorithm to (g, g’s parent) »In the worst case the recoloring will propagate all the way back to the root v p g u

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert: cont Recover the RBT property. –3.2 v is red, p is red, g is black, color of u will determine the method by which to recover RBT If u is black –If v is left child of left parent (or right of right parent) 1. single LR (or RL) rotation at g (left-left shown in picture) 2. Recoloring »In the recoloring the root of the subtree ends up black, done. v p g u u g p v

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert: cont Recover the RBT property. –3.2 v is red, p is red, g is black, color of u will determine the method by which to recover RBT If u is black –If v is right child of left parent (or left of right parent) 1. RL (or LR) rotation at p 2. LR (or RL) rotation at g 3.Recoloring »In the recoloring the root of the subtree ends up black, done. v p g u v p u g v g u p

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT Insert complexity BST insert is O(log n) Each rotation/recoloring takes In the worst case, recoloring/rotations may propagate to the root of the tree, and since the height is O(log n), the worst case time complexity of recovering RBT property is O(log n)* which is still O(log n) Thus RVLT Insert is O(log n)

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms RBT: example, insert G E V x N T J L G

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Ex: The tree after BST insert(G) E V x N T J L G v p g u p, g, u, v is left child of right parent

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Ex: The tree after BST insert(G) E V x N T J L G v p g u p, g, u, v was left child of right parent

Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Ex: The tree after BST insert(G) E V x N T J L G p, g, u, v was left child of right parent