CS 583 Analysis of Algorithms

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

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.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees Comp 550.
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Comp 122, Spring 2004 Red-Black Trees. redblack - 2 Comp 122, Spring 2004 Red-black trees: Overview  Red-black trees are a variation of binary search.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
13. Red-Black Tree Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P.2 One of many search-tree schemes that are “ balanced ” in order to guarantee that.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red-Black Trees Comp 550.
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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: 
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Analysis of Algorithms CS 477/677
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Slide Sources: CLRS “Intro. To Algorithms” book website
Lecture 7 Algorithm Analysis
Red-Black Trees Motivations
CS200: Algorithms Analysis
Augmenting Data Structures
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Chapter 14: Augmenting Data Structures
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Lecture 7 Algorithm Analysis
Red Black Tree Essentials
Lecture 7 Algorithm Analysis
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

CS 583 Analysis of Algorithms Red-Black Trees CS 583 Analysis of Algorithms 2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Outline Red-Black Trees Definitions Rotations Augmenting Data Structures Dynamic order statistics Determining the rank of an element Maintaining subtree sizes 2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Definitions A red-black tree is a binary search tree with one extra item per node: its color, which can be either RED or BLACK. By constraining the color of nodes, red-black trees ensure the following balancing rule: Any path from the root to a leaf is no more than twice as long as any other path. Each node contains the following fields: color, key, left, right, and parent. If a child of a node does not exist, it is referred by NIL. NILs are leaf nodes that are called external nodes, and all other key bearing nodes are internal nodes 2/17/2019 CS583 Fall'06: Red-Black Trees

Red-Black Trees Properties Red-black trees must satisfy the following properties: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes. We use a single sentinel nil[T] to represent NIL. Its color field is BLACK, and all other fields are set to arbitrary values. All pointers to NIL are replaced by pointers to sentinel nil[T]. 2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Height of the Tree We call the number of black nodes from, but not including a node x down to a leaf the black-height of the node, denoted bh(x). By property 5, this notion is well defined.   Lemma 13.1 A red-black tree with n internal nodes has height at most 2 lg (n+1). Proof. First, show that the subtree rooted at any node x contains at least 2bh(x)-1 internal nodes. We prove it by induction on the height of x-based subtree. If the height of x is 0, then x is nil[T], hence it contains 0 nodes = 20-1. 2/17/2019 CS583 Fall'06: Red-Black Trees

Height of the Tree (cont.) Now, consider an internal node x with two children. Each child has a black-height of bh(x) (if it is RED), or bh(x) - 1 (if it is BLACK). By our hypothesis, each child has at least 2bh(x)-1-1 (for the BLACK one) nodes. Thus, the subtree x contains at least 2*(2bh(x)-1-1) + 1 = 2bh(x)-1 internal nodes, which proves the claim. Note that, in the case of one child, that child cannot be BLACK (to not violate property 5). Hence, if the child y is RED, its bh(y)=bh(x) => N(x) >= 2bh(x)-1 + 1. 2/17/2019 CS583 Fall'06: Red-Black Trees

Height of the Tree (cont.) To complete the proof, let h be the height of the tree. According to property 4, at least half the nodes on any simple path from the root to a leaf, not including the root, must be black. (The simple path includes only one child, which must be black for each red node.) Consequently, the black-height of the root must be at least h/2; hence:   n >= 2h/2-1 <=> 2h/2 <= n+1 <=> h/2 lg2 <= lg(n+1) <=> h <= 2 lg(n+1)  2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Rotations The insert and delete operations when run on a red-black tree take O(lg n) time. However, they modify the tree, which may violate the red-black tree properties. To restore those properties, we must change the colors of some nodes and the pointers structure.  The pointer structure is changed through rotation, which is a local operation in a search that preserves the binary-search tree property. There are two kinds of rotations: left and right. 2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Left Rotation The left rotation for node x assumes its right child y is not nil[T]. It "pivots" around the link from x to y: It makes y the new root of the subtree. x is y's left child. x's right child is y's left child. x a y b c   ---> y x c a b A rotation operation preserves the BST properties: key[a] <= key[x] <= key[b] <= key[y] <= key[c] 2/17/2019 CS583 Fall'06: Red-Black Trees

Left Rotation: Pseudocode Left-Rotate(T,x) 1 y = right[x] 2 right[x] = left[y] 3 if left[y] <> nil[T] 4 parent[left[y]] = x 5 parent[y] = parent[x] 6 if parent[x] = nil[T] 7 root[T] = y 8 else 9 if x = left[parent[x]] // x is left child 10 left[parent[x]] = y 11 else // x is right child 12 right[parent[x]] = y 13 left[y] = x 14 parent[x] = y The rotation operation runs in (1) time; only pointers are changed, all other fields remain the same. 2/17/2019 CS583 Fall'06: Red-Black Trees

Augmenting Data Structures Many software engineering problems can be solved by using “textbook” data structures such as doubly-linked lists, hash tables, or binary search trees. For example, using a C++ STL library is sufficient for many financial algorithms. In some situations, however, using a straightforward data structure is not sufficient. It is very rare that an entirely new data structure has to be invented. More often, it will suffice to augment a standard data structure by storing an additional information in it. This is not often straightforward as the new information must be updated and maintained. 2/17/2019 CS583 Fall'06: Red-Black Trees

Dynamic Order Statistics Recall that, the ith order statistics is the element in the set of n elements with the ith smallest key. We saw that any order statistics could be retrieved in O(n) time from an ordered set. Now we will augment a red-black tree to determine the order statistics in O(lg n) time. The rank of an element is its position in the linear order of the set. It can also be determined in O(lg n) time in an augmented red-black tree structure, 2/17/2019 CS583 Fall'06: Red-Black Trees

Order-Statistics Tree An order-statistics tree T is a red-black tree with additional information stored in each node. In addition to key[x], color[x], p[x], left[x], and right[x], we have another field size[x]. This field contains the number of internal nodes in the subtree rooted at x (including x itself). If we define size[nil[T]] = 0 (for sentinel nodes) then: size[x] = size[left[x]] + size[right[x]] + 1 We do not require keys to be distinct. This creates ambiguity when determining the rank of an element. The convention is to define the rank based on the inorder tree walk. 2/17/2019 CS583 Fall'06: Red-Black Trees

Retrieving an ith Order Element The procedure below returns a pointer to the node containing the ith smallest key in the subtree rooted at x. OS-Select(x,i) 1 r = size[left[x]] + 1 2 if i = r 3 return x 4 else 5 if i < r 6 return OS-Select(left[x], i) 7 else 8 return OS-Select(right[x], i-r) Each recursive call goes down one level in the tree, hence the total time for this procedure is proportional to the height of the tree, which is O(lg n) for the red-black tree. Thus, the running time of OS-Select is O(lg n). 2/17/2019 CS583 Fall'06: Red-Black Trees

CS583 Fall'06: Red-Black Trees Determining the Rank The procedure below returns the position of x in the linear order determined by an inorder tree walk of T. OS-Rank(T, x) 1 r = size[left[x]] + 1 2 y = x 3 while y <> root[T] 4 if y = right[p[y]] 5 r = r + size[left[p[y]]] + 1 6 y = p[y] 7 return r The rank of x can be viewed as the number of nodes preceding x in an inorder tree walk, plus 1 for x. Invariant: At the start of the while loop 3-6, r is the rank of key[x] in the subtree rooted at y. 2/17/2019 CS583 Fall'06: Red-Black Trees

Determining the Rank: Correctness Initialization: Prior to the first iteration, r is the rank of x in the subtree rooted at x, and y=x. Maintenance: At the end of each iteration y is set to p[y]. Hence r must be a rank of key[x] for a tree at p[y]. If y is a left child, then no additional nodes for x need to be counted. Otherwise, we need to add all nodes in p[y] left subtree and p[y] itself (line 5). Termination: The loop terminates when y=root[T], hence r is the rank of key[x] in the entire tree. 2/17/2019 CS583 Fall'06: Red-Black Trees

Determining the Rank: Performance Each iteration of the while loop takes (1) time. Node y goes up one level in the tree with each iteration. Hence, the running time of OS-Rank is at worst proportional to the height of the tree: O(lg n) on an n-node order-statistics tree. 2/17/2019 CS583 Fall'06: Red-Black Trees

Maintaining Subtree Sizes The size field in each node helps quickly compute order-statistics information. This field should be maintained for both insertion and deletion operations on the red-black trees without affecting the asymptotic running time of these operations. The insertion operation is based on two phases: Walk the tree to add a node to the existing node. Simply increment size[x] for each x on the path traversed. The second phase is based on rotations. The size needs to be changed for only two nodes involved. Since only at most two rotations are needed, a constant time will be added, not affecting the asymptotic time. 2/17/2019 CS583 Fall'06: Red-Black Trees

Maintaining Subtree Sizes: Left Rotation Left-Rotate(T,x) 1 y = right[x] 2 right[x] = left[y] 3 if left[y] <> nil[T] 4 parent[left[y]] = x 5 parent[y] = parent[x] 6 if parent[x] = nil[T] 7 root[T] = y 8 else 9 if x = left[parent[x]] // x is left child 10 left[parent[x]] = y 11 else // x is right child 12 right[parent[x]] = y 13 left[y] = x 14 parent[x] = y 15 size[y] = size[x] 16 size[x] = size[left[x]] + size[right[x]] + 1 2/17/2019 CS583 Fall'06: Red-Black Trees