EECS 4101/5101 Prof. Andy Mirzaian. Lists Move-to-Front Search Trees Binary Search Trees Multi-Way Search Trees B-trees Splay Trees 2-3-4 Trees Red-Black.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
CSE 4101/5101 Prof. Andy Mirzaian B-trees trees.
Chapter 12 Binary Search Trees
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
EECS 4101/5101 Prof. Andy Mirzaian. Lists Move-to-Front Search Trees Binary Search Trees Multi-Way Search Trees B-trees Splay Trees Trees Red-Black.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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
A balanced life is a prefect life.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
10/22/2002CSE Red Black Trees CSE Algorithms Red-Black Trees Augmenting Search Trees Interval Trees.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Binary Search Trees1 ADT for Map: Map stores elements (entries) so that they can be located quickly using keys. Each element (entry) is a key-value pair.
Splay Trees Splay trees are binary search trees (BSTs) that:
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
Binary Search Trees1 Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees   
Binary Search Trees What is a binary search tree?
COMP261 Lecture 23 B Trees.
Part-D1 Binary Search Trees
AA Trees.
Balanced Search Trees Modified from authors’ slides.
Lecture Efficient Binary Trees Chapter 10 of textbook
BCA-II Data Structure Using C
Week 7 - Friday CS221.
Balancing Binary Search Trees
Red Black Trees
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Red-Black Trees Motivations
TCSS 342, Winter 2006 Lecture Notes
CS202 - Fundamental Structures of Computer Science II
Advanced Associative Structures
Multi-Way Search Trees
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Advanced Implementation of Tables
CS202 - Fundamental Structures of Computer Science II
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Chapter 12&13: Binary Search Trees (BSTs)
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

EECS 4101/5101 Prof. Andy Mirzaian

Lists Move-to-Front Search Trees Binary Search Trees Multi-Way Search Trees B-trees Splay Trees Trees Red-Black Trees SELF ADJUSTING WORST-CASE EFFICIENT competitive competitive? Linear Lists Multi-Lists Hash Tables DICTIONARIES 2

References: [CLRS] chapter 13 AAW animation [CLRS] chapter 13 AAW animation 3

Binary Search trees from trees  trees are perfectly balanced ( height: ½ log(n+1)  h  log(n+1) ) search trees that use 2-nodes, 3-nodes, and 4-nodes.  We can transform a tree to an O(log n) height BST by replacing each 3-node and 4-node by a small-clustered BST with 2 or 3 binary nodes.  Dilemma: How do we distinguish “node clusters”?  trees are perfectly balanced ( height: ½ log(n+1)  h  log(n+1) ) search trees that use 2-nodes, 3-nodes, and 4-nodes.  We can transform a tree to an O(log n) height BST by replacing each 3-node and 4-node by a small-clustered BST with 2 or 3 binary nodes.  Dilemma: How do we distinguish “node clusters”? Disallowed 4-node clusters: 2-node right slantleft slant OR 3-node4-node 4

Red-Black trees from trees  Although the resulting BST has height O(log n), it loses the “node cluster” information and renders the tree algorithms obsolete. We use a “node cluster” colour-coding scheme to resolve this.  Convention: each cluster top level is black (including external nodes), lower levels within each cluster are red. So, any red node is clustered within its parent cluster.  Although the resulting BST has height O(log n), it loses the “node cluster” information and renders the tree algorithms obsolete. We use a “node cluster” colour-coding scheme to resolve this.  Convention: each cluster top level is black (including external nodes), lower levels within each cluster are red. So, any red node is clustered within its parent cluster. Disallowed 4-node clusters: 2-node right slantleft slant OR 3-node4-node 5

Example: tree bh = 3 black height, including external nodes 6

Transformed Red-Black tree bh = 3 black height, including external nodes

Definition: Red-Black tree DEFINITION: T is a Red-Black tree if it satisfies the following: 1.T is a Binary Search Tree with a red/black colour bit per node. 2.Every red node has a black parent. (  root is black.) 3.By convention we assume all external nodes are black. 4.All external nodes have the same number (namely, bh) of black proper ancestors. DEFINITION: T is a Red-Black tree if it satisfies the following: 1.T is a Binary Search Tree with a red/black colour bit per node. 2.Every red node has a black parent. (  root is black.) 3.By convention we assume all external nodes are black. 4.All external nodes have the same number (namely, bh) of black proper ancestors. 8

Implementing Operations Access operations: SEARCH MINIMUM MAXIMUM PREDECESSOR SUCCESSOR Update Operations: INSERT DELETE Use the BST algorithms without change. (Simply ignore the node colours.) Worst-case time: O(h) = O(log n). Simulate the tree algorithms as shown next. Worst-case time: O(h) = O(log n). 9

Example operations SEARCH 40 INSERT

Local Restructuring INSERT and DELETE need the following local operations that take O(1) time each:  Node Colour Switch: red  black  Link Rotation INSERT and DELETE need the following local operations that take O(1) time each:  Node Colour Switch: red  black  Link Rotation a a b b      b b a a      Right Rotate  Left Rotate  FACT: Rotation preserves INORDER sequence of the tree nodes and changes slant of link . 11

Bottom-Up INSERT part 0:4 Bottom-Up INSERT (K,T) Simulate tree bottom up insertion. Bottom-Up INSERT (K,T) Simulate tree bottom up insertion. When inserting K at the bottom of the tree, repeatedly split 4-nodes upwards until you either split the root, or reach a 2-node or a 3-node. Simulate this on the Red-Black tree. a 1 a 2 a 3 b 1 b 2 b 3 K c 1 c 2 c 3 12

Bottom-Up INSERT part 1:4 Bottom-Up INSERT (K,T) Step 1: Follow the search path of K down Red-Black tree T. If K is found, return. Otherwise, we reach a black external node x. Convert x into a red leaf and store K in it. Bottom-Up INSERT (K,T) Step 1: Follow the search path of K down Red-Black tree T. If K is found, return. Otherwise, we reach a black external node x. Convert x into a red leaf and store K in it.  At the end of the algorithm we will re-colour the root black even if it might have temporarily become red.  K is red means x splits into x’ & x”, & K is inserted into the parent cluster.  Now T satisfies all 4 properties of RB-trees except property 2: red x might have a red parent p. We need to climb up the tree to fix up. If p is black, we are done. If p is red, more work follows. If p is black, we are done. If p is red, more work follows. x  p root[T] ’’ ”” K K x p x’x” root[T]  13

Bottom-Up INSERT part 2a:4 Step 2: While parent is part of a “4-node cluster”, keep splitting it and promote its middle key up. (May repeat many times.) a b c   a c c    bb ’’ ”” ’’ ””   tree Red-Black tree b b a a c c ’’ ””       * Colour Switch p, u, g. [  up from  is symmetric ] b b a a c c ’’ ”” *      x pu g 14

Bottom-Up INSERT part 2b:4 Step 2: While parent is part of a “4-node cluster”, keep splitting it and promote its middle key up. (May repeat many times.) a b c     bb ’’ ”” ’’ ””  tree a  c c Red-Black tree b b a a c c ’’ ””       * Colour Switch p, u, g. [  up from  is symmetric ] b a a c c ’’ ”” *      x p u g 15

Bottom-Up INSERT part 3a:4 Step 3a: Have reached a 3-node cluster. a b    ’’ ””   tree  a b TERMINAL CASE Red-Black tree No Change. a a   b b ’’ ””   [  up from  is symmetric ] a a   b b ’’ ”” *   Opposite Slant x p s 16

Bottom-Up INSERT part 3b:4 Step 3b: Have reached a 3-node cluster. a b    ’’ ””   tree  a b [  up from  is symmetric ] Red-Black tree Colour Switch p, g. Rotate  a a   b b ’’ ””    TERMINAL CASE b b   a a ’’ ”” *    Same Slant Zig-Zig  x p g 17

Bottom-Up INSERT part 3c:4 Step 3c: Have reached a 3-node cluster. Red-Black tree Colour Switch x, g. DoubleRotate(x,p,g): i.e., Rotate  then Rotate    a a b b  ’’    ”” a b     ’’ ””  tree a  b TERMINAL CASE b b   a a ’’ ”” *   LR Zig-Zag  x p g 18

Bottom-Up INSERT part 3d:4 Step 3d: Have reached a 3-node cluster. a b     ’’ ””  tree a  b TERMINAL CASE RL Zig-Zag a a   b b ’’ ”” *    x p g Red-Black tree   a a b b  ’’    ”” Colour Switch x, g. DoubleRotate(x,p,g): i.e., Rotate  then Rotate  19

Bottom-Up INSERT part 3e:4 Step 3e: Have reached a 2-node cluster. [  up from  is symmetric ] Red-Black tree No change. a a    ’’ ”” a a  ’’ ””  tree  a TERMINAL CASE a a    ’’ ”” * x p 20

Bottom-Up INSERT part 4:4 Step 4: Have reached nil (parent of root). END Red-Black tree colour[root[T]]  black   ’’ ””  = nil This last step always resets root colour to black. Black-height increases by one if root was temporarily red. This last step always resets root colour to black. Black-height increases by one if root was temporarily red.  root level) ’’ ”” tree    = nil   ’’ ”” * x  21

Bottom-Up INSERT summary Bottom-Up INSERT (K,T) Step 1: Follow the search path of K down Red-Black tree T. If K is found then return Otherwise, we have reached a black external node x. Convert x into a red leaf and store K in it. Step 2: while (p, u)=red (* x=red, parent in 4-node cluster *) do SwitchColour(p,u,g); x  g end-while Step 3: if p=red & u=black (u possibly external) (* x=red *) then case: (* 3-node turns into unbalanced 4-node *) [Zig-Zig (x,p,g)]: SwitchColour(p,g); Rotate(p,g) [Zig-Zag(x,p,g)]: SwitchColour(x,g); DoubleRotate(x,p,g) end-case Step 4: if root[T] = nil then root[T]  x colour[root[T]]  black end Bottom-Up INSERT (K,T) Step 1: Follow the search path of K down Red-Black tree T. If K is found then return Otherwise, we have reached a black external node x. Convert x into a red leaf and store K in it. Step 2: while (p, u)=red (* x=red, parent in 4-node cluster *) do SwitchColour(p,u,g); x  g end-while Step 3: if p=red & u=black (u possibly external) (* x=red *) then case: (* 3-node turns into unbalanced 4-node *) [Zig-Zig (x,p,g)]: SwitchColour(p,g); Rotate(p,g) [Zig-Zag(x,p,g)]: SwitchColour(x,g); DoubleRotate(x,p,g) end-case Step 4: if root[T] = nil then root[T]  x colour[root[T]]  black end x = current node, p = parent of x, u = uncle of x, g = grand parent of x. 22

Example: Bottom-Up Insert ABJ CHI DEFG add E & split 4-nodes up J CHI D E’ FG 45 E” AB balance up unbalanced 4-node ABJ CHI D E’ FG 45 E” * 23

INSERT & DELETE FACT 1: Bottom-Up Insert makes at most 2 rotations. This occurs when after the “4-node splitting loop” a 3-node turns into a zig-zag 4-node. It needs 2 rotations to turn it into a balanced 4-node cluster. FACT 1: Bottom-Up Insert makes at most 2 rotations. This occurs when after the “4-node splitting loop” a 3-node turns into a zig-zag 4-node. It needs 2 rotations to turn it into a balanced 4-node cluster. FACT 2: Bottom-Up Delete makes at most 3 rotations.  Top-Down Insert (exercise).  Bottom-Up Delete (see [CLRS]).  Top-Down Delete: While going down the search path, maintain LI: [LI: current node x is either root[T] or x is red or x has a red child.] With LI, “splice-out” can finish the task in O(1) time.  Top-Down Insert (exercise).  Bottom-Up Delete (see [CLRS]).  Top-Down Delete: While going down the search path, maintain LI: [LI: current node x is either root[T] or x is red or x has a red child.] With LI, “splice-out” can finish the task in O(1) time. FACT 3: Top-Down Insert & Delete may make up to  (log n) rotations. Explain why. This makes them unsuitable for persistent data structures. 24

Bibliography: Red-Black Trees: [R. Bayer, E.M. McCreight, “Symmetric binary B-trees: Data Structure and maintenance algorithms,” Acta Informatica, 1: , 1972.] Colour coded Red-Black Trees: [L.J. Guibas, R. Sedgewick, “A dichromatic framework for balanced trees,” in Proceedings of the 19 th Annual Symposium on Foundations of Computer Science (FOCS), pp: 8-21, IEEE Computer Society, 1978.] AA-trees: [A. Andersson, “Balanced search trees made simple,” in Proceedings 3 rd Workshop on Algorithms and Data Structures (WADS), in Lecture Notes in Computer Science LNCS709, pp: , Springer-Verlag, 1993.] Scapegoat trees: [I. Galperin, R.L. Rivest, “Scapegoat trees,” in Proc. 4 th ACM-SIAM Symp. on Discrete Algorithms (SODA), pp: , 1993.] Treaps: [R. Seidel, C.R. Aragon, “Randomized search trees,” Algorithmica, 16: , 1996.] AVL trees: [G.M. Adel’son-Vel’skiĭ, E.M. Landis, “An algorithm for the organization of information,” Soviet Mathematics Doklady, 3: , 1962.] Weight balanced trees: [J. Nievergelt, E.M. Reingold, “Binary search trees of bounded balance,” SIAM J. of Computing, 2(1):33-43, 1973.] K-neighbor trees: [H.A. Mauer, Th. Ottmann, H.-W. Six, “Implementing dictionaries using binary trees of very small height,” Information Processing Letters, 5(1):11-14, 1976.] 25

Exercises 26

1.[CLRS, Exercise , p. 314] Rotation Sequence: Show that any BST holding a set of n keys can be transformed into any other BST holding the same set of keys using O(n) rotations. [Hint: first show that at most n-1 right rotations suffice to transform the tree into a right-going chain.] 2.[CLRS, Exercise , p. 314] Right Rotation Sequence: We say that a BST T 1 can be right-converted to BST T 2 if it is possible to obtain T 2 from T 1 via a series of right rotations. Give an example of two trees T 1 and T 2, holding the same set of keys, such that T 1 cannot be right-converted to T 2. Then show that if a tree T 1 can be right-converted to T 2, it can be done so using O(n 2 ) right rotations, where n is the number of keys in T 1. 3.Top-Down Insert & Delete: Design and analyze O(log n) time top-down Delete and Insert on red- black trees. [Hint: simulate top-down tree procedures & use the hints given in these slides.] 4.[CLRS, Exercise , p. 330] Suppose we use bottom-up insert and delete on red-black tree T. Suppose that a key K is inserted into T and then immediately deleted from it. Is the resulting tree always the same as the initial tree? Justify your answer. 5.Split and Join on Red-Black trees: These are cut and paste operations on dictionaries. The Split operation takes as input a dictionary (a set of keys) A and a key value K (not necessarily in A), and splits A into two disjoint dictionaries B = { x  A | key[x]  K } and C = { x  A | key[x] > K }. (Dictionary A is destroyed as a result of this operation.) The Join operation is essentially the reverse; it takes two input dictionaries A and B such that every key in A < every key in B, and replaces them with their union dictionary C = A  B. (A and B are destroyed as a result of this operation.) Design and analyze efficient Split and Join on red-black trees. [Note: Definition of Split and Join here is the same we gave on BST’s and trees, and slightly different than the one in Problem 13-2, pages of [CLRS].] 6.Red-Black tree Insertion sequence: Bottom-Up Insert integers 1..n one at a time in increasing order into an initially empty red-black tree in O(n) time total. [Hint: This is related to exercise 4 in the Slide on B-trees. Keep a pointer to the largest key node.] 27

7.RB-Balance: We say a binary tree T is RB-balanced if it satisfies the following property: RB-balance: for every node x in tree T, the length of a longest path from x to any of its descendant external nodes is at most twice the length of a shortest path from x to any of its descendant external nodes. (a) Show that every red-black tree is an RB-balanced Binary Search Tree. (b) Now we want to prove that the converse also holds. Let T be an arbitrary RB-balanced BST with n nodes. We want to show (algorithmically) that it is always possible to colour each node of T red or black to make it a red-black tree. [Note that we make no structural change to T other than colouring its nodes.] Design and analyze an O(n) time algorithm to do the node colouring to turn T into a red-black tree. [You may assume there is space for a colour-bit in each node of T.] (c) Carefully prove the correctness of your algorithm in part (b). 8.BST to RB-tree Conversion: Given an n-node arbitrary BST, design and analyze an O(n) time algorithm to construct an equivalent red-black tree (i.e., one that contains the same set of keys). 9.Range-Search Reporting in RB-tree: Let T be a given red-black tree. We are also given a pair of key values a and b, a < b (not necessarily in T). We want to report every item x in T such that a  key[x]  b. Design an algorithm that solves this problem and takes O(R+log n) time in the worst case, where n is the number of items in T and R is the number of reported items (i.e., the output size). 28

10.Restricted Red-Black Tree: We define a Restricted Red-Black Tree (RRB-tree) to be a standard Red- Black Tree with the added structural constraint that every red node must be the right child of its parent. So, every left child is black. (In comparison with trees, this indicates that we have no 4-node clusters, and every 3-node cluster is right slanted.) We want to show that it is possible to maintain such a structure while performing dictionary operations efficiently. Let T be an arbitrary n-node RRB-tree. (a) Obtain tight lower and upper bounds on height of T as a function of n. (b) Show how to perform the dictionary insert operation on T efficiently. Make sure you consider all possible cases in the algorithm. What is its worst-case running time as a function of n? (c) Consider a leaf node x in T. (Note x is not an external node.) What are possible structures of the subtree rooted at the sibling of x? (d) Using your answer to part (c), show how to perform the dictionary delete operation on T efficiently. Make sure you consider all possible cases in the algorithm. What is its worst-case running time as a function of n? 29

END 30