Red-Black Tree Algorithm : Design & Analysis [12].

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
Trees Types and Operations
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
CS 171: Introduction to Computer Science II
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
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):
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
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 /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 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Balanced Trees Balanced trees have height O(lg n).
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.
CPSC 335 BTrees Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Balanced Binary Search 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.
Introduction to Algorithms Jiafen Liu Sept
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg 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.
Computer Algorithms Submitted by: Rishi Jethwa Suvarna Angal.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
1. 2 Setting Up Deletion As with binary search trees, we can always delete a node that has at least one external child If the key to be deleted is stored.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
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 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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Data Structures Chapter 10: Efficient Binary Search Trees 10-1.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
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.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
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 an 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.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Binary Search Trees
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Data Structures – LECTURE Balanced 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 v z Red-Black Trees 1 Red-Black Trees
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees.
Algorithms and Data Structures Lecture VIII
AVL Search Tree put(9)
CS202 - Fundamental Structures of Computer Science II
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Chapter 12&13: Binary Search Trees (BSTs)
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Red-Black Tree Algorithm : Design & Analysis [12]

In the last class … Design against an Adversary Selection Problem – Median A Linear Time Selection Algorithm Analysis of Selection Algorithm A Lower Bound for Finding the Median

Red-Black Tree Dynamic sets Amortized time analysis Definition of red-black tree Black height Insertion into a red-black tree Deletion from a red-black tree

Dynamic Sets Dynamic set Membership varies during computation Maximum size unknown previously Space allocation techniques such as array doubling may be needed. The problem of “ unusually expensive ” individual operation.

Amortized Time Analysis Amortized equation: amotized cost = actual cost + accounting cost Design goals for accounting cost In any legal sequence of operations, the sum of the accounting costs is nonnegative. The amortized cost of each operation is fairly regular, in spite of the wide fluctuate possible for the actual cost of individual operations.

Accounting Scheme for Stack Push Push operation with array doubling No resize triggered: 1 Resize(n  2n) triggered: tn+1 (t is a constant) Accounting scheme (specifying accounting cost) No resize triggered: 2t Resize(n  2n) triggered: -nt+2t So, the amortized cost of each individual push operation is 1+2t  (1)

Node Group in a binTree principal subtrees Node group As in 2-tree, the number of external node is one more than that of internal node

Binary Search Tree Poor balancing  (n) Each node has a key, belonging to a linear ordered set An inorder traversal produces a sorted list of the keys Each node has a key, belonging to a linear ordered set An inorder traversal produces a sorted list of the keys In a properly drawn tree, pushing forward to get the ordered list. Good balancing  (logn)

Improving the Balancing by Rotation The node group to be rotated Root of the group is changed. The middle principal subtree changes parent

Red-Black Tree: the Definition If T is a binary tree in which each node has a color, red or black, and all external nodes are black, then T is a red-black tree if and only if: [Color constraint] No red node has a red child [Black height constrain] The black length of all external paths from a given node u is the same (the black height of u) The root is black. Almost-red-black tree(ARB tree) Balancing is under controlled

Red-Black Tree with 6 Nodes poorest balancing: height(normal) is 4 Black edge

Black-Depth Convention All with the same largest black depth: 2 ARB Trees

Recursive Definition of Red-Black Tree (A red-black tree of black height h is denoted as RB h ) Definition: An external node is an RB 0 tree, and the node is black. A binary tree ia an ARB h (h  1) tree if: Its root is red, and Its left and right subtree are each an RB h-1 tree. A binary tree ia an RB h (h  1) tree if: Its root is black, and Its left and right subtree are each either an RB h-1 tree or an ARB h tree.

Properties of Red-Black Tree The black height of any RB h tree or ARB h tree is well defind and is h. Let T be an RB h tree, then: T has at least 2 h -1 internal black nodes. T has at most 4 h -1 internal nodes. The depth of any black node is at most twice its black depth. Let A be an ARB h tree, then: A has at least 2 h -2 internal black nodes. A has at most (4 h )/2-1 internal nodes. The depth of any black node is at most twice its black depth. Let T be a red-black tree with n internal nodes, the height of T, in the usual sense, is at most 2lg(n+1)

Influences of Insertion into an RB Tree Black height constrain: No violation if inserting a red node. Color constraint: Inserting 70 Critical clusters(external nodes excluded), which originated by color violation, with 3 or 4 red nodes

Repairing 4-node Critical Cluster Color flip: Root of the critical cluster exchanges color with its subtrees Color flip: Root of the critical cluster exchanges color with its subtrees No new critical cluster occurs, inserting finished.

Repairing 4-node Critical Cluster 2 more insertions Critical cluster New critical cluster with 3 nodes. Color flip doesn’t work, Why? New critical cluster with 3 nodes. Color flip doesn’t work, Why?

Patterns of 3-Node Critical Cluster 85 L RL LL M RRLR R L RL LL M RRLR R L RL LL M RRLR R L RL LL M RRLR R Shown as properly drawn A B C D

Repairing 3-Node Critical Cluster L RL LL M RRLR R Root of the critical cluster is changed to M, and the parentship is adjusted accordingly The incurred critical cluster is of pattern A All into one pattern

Deletion: Logical and Structral u: to be deleted logically  : tree successor of u, to be deleted structurally, with information moved into u  S right subtree of S, to replacing S  After deletion

Deletion in a Red-Black Tree To be deleted  The black height of  is not well-defined ! black depth=1 black depth=2 one deletion

Procedure of Red-Black Deletion 1. Do a standard BST search to locate the node to be logically deleted, call it u 2. If the right child of u ia an external node, identify u as the node to be structurally deleted. 3. If the right child of u is an internal node, find the tree successor of u, call it , copy the key and information from  to u. (color of u not changed) Identify  as the node to be deleted structurally. 4. Carry out the structural deletion and repair any imbalance of black height.

Imbalance of Black Height deleting deleting deleting deleting 60 Black height has to be restored

Analysis of Black Imbalance The imbalance occurs when: A black node is delete structrally, and Its right subtree is black(external) The result is: An RB h-1 occupies the position of an RB h as required by its parent, coloring it as a “gray” node. Solution: Find a red node and turn it red as locally as possible. The gray color might propagate up the tree.

Propagation of Gray Node s lr g p Map of the vicinity of g, the gray node s r g p l glr p s The pattern for which paropagation is needed p-subtree gets well-defined black height, but that is less than that required by its parent Gray Up In the worst case, up to the root of the tree, and successful

Schemes for Repairing g p s abc gab l l Deletion Rebalance group 4 principal subtrees, RB h-1 c p s Restructured Restructuring the deletion rebalance group: Red p: form an RB 1 or ARB 2 tree Black p: form an RB 2 tree Restructuring the deletion rebalance group: Red p: form an RB 1 or ARB 2 tree Black p: form an RB 2 tree