Balanced search trees

Similar documents
Algorithms. Red-Black Trees

13.4 Deletion in red-black trees

Properties of red-black trees

13.4 Deletion in red-black trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

Search Trees. Undirected graph Directed graph Tree Binary search tree

Red-Black Trees. Every simple path from a node to one of its descendant leaf nodes contains the same number of BLACK nodes.

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

Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees

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

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

Binary Search Trees, etc.

Algorithms. AVL Tree

Lecture 6: Analysis of Algorithms (CS )

catch(...){ printf( "Assignment::SolveProblem() AAAA!"); }

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

CS 3343 Fall 2007 Red-black trees Carola Wenk

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

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

CS350: Data Structures Red-Black Trees

12 Binary Search Tree

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

COMP251: Red-black trees

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

Data Structures Week #6. Special Trees

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Data Structures and Algorithms

COMP Analysis of Algorithms & Data Structures

CSE2331/5331. Topic 7: Balanced search trees. Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331

Balanced search trees. DS 2017/2018

Algorithms. 演算法 Data Structures (focus on Trees)

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

Ch04 Balanced Search Trees

Lecture: Analysis of Algorithms (CS )

COMP Analysis of Algorithms & Data Structures

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

We assume uniform hashing (UH):

Multiway Search Trees. Multiway-Search Trees (cont d)

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

Graduate Algorithms CS F-07 Red/Black Trees

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Binary Trees. Recursive definition. Is this a binary tree?

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Data Structures Week #6. Special Trees

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

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

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

Data Structures and Algorithms CMPSC 465

Multi-Way Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

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

Search Trees. COMPSCI 355 Fall 2016

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

CHAPTER 10 AVL TREES. 3 8 z 4

Binary search trees :

OPPA European Social Fund Prague & EU: We invest in your future.

Lecture 6. Binary Search Trees and Red-Black Trees

Animations. 01/29/04 Lecture

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

Chapter 13. Michelle Bodnar, Andrew Lohr. April 12, 2016

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.

Trees. Eric McCreath

Red-Black Trees (2) Antonio Carzaniga. April 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Section 4 SOLUTION: AVL Trees & B-Trees

Red-black tree. Background and terminology. Uses and advantages

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

Balanced Binary Search Trees. Victor Gao

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

Theory & Algorithms 15/01/04. Red-Black Trees. Nicolas Wack, Sylvain Le Groux

COMP171. AVL-Trees (Part 1)

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

ECE250: Algorithms and Data Structures AVL Trees (Part A)

Priority Queues. Priority Queues Trees and Heaps Representations of Heaps Algorithms on Heaps Building a Heap Heapsort.

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Binary search trees (BST) Binary search trees (BST)

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

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

Binary Search Trees. Analysis of Algorithms

Data Structures in Java

Data Structures Week #6. Special Trees

Sample Solutions CSC 263H. June 9, 2016

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Multi-Way Search Trees

Advanced Data Structures Summary

Best-Case upper limit on the time for insert/delete/find of an element for a BST withnelements?

Algorithms. Deleting from Red-Black Trees B-Trees

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

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

Binary Search Trees. Motivation. Binary search tree. Tirgul 7

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Lecture 23: Binary Search Trees

Part 2: Balanced Trees

Red-Black Trees (2) Antonio Carzaniga. Faculty of Informatics University of Lugano. April 26, Antonio Carzaniga 1

Lower Bound on Comparison-based Sorting

Lecture 7. Binary Search Trees and Red-Black Trees

Red-Black Trees. 1 Properties of red-black trees

Transcription:

Balanced search trees Ordinary binary search trees have expected height Θ(log n) if items are inserted and deleted in random order, but for other orders the height can be Θ(n). This is undesirable, since many operations require time Θ(tree height). Therefore, alternative tree structures have been devised. Common ones are: Red-black trees Weight-balanced trees Height-balanced trees (including AVL trees) B-trees (including 2-3 trees) Splay trees COMP3600/6466: Lecture 13 2017 1

Chapter 13 Red-Black Trees A red-black tree (RBT) is a binary search tree with one extra bit of storage per node: its colour can be either RED or BLACK. Each node in a RBT contains at least the following fields: color key left child pointer left() right child pointer right() parent pointer p() COMP3600/6466: Lecture 13 2017 2

13.1 Properties of red-black trees A binary search tree is a red-black tree if it satisfies the following five red-black tree properties. Every node is colored only with either RED or BLACK. The root node must be BLACK. Every leaf (NIL) must be BLACK. If a node is RED, then both its children are BLACK. This implies this node must be an internal node! Each path from a node x to any one of its descendant leaf nodes contains the same number of BLACK nodes. Note that the color of node x will be not counted. COMP3600/6466: Lecture 13 2017 3

What red-black trees really look like How we draw them (Cormen, p310) COMP3600/6466: Lecture 13 2017 4

13.1 Properties of red-black trees (continued) The black height bh(x) of a node x in a RBT is the number of BLACK nodes on the path from node x down to a leaf, not counting x itself but counting the leaf. The definition of red-black tree guarantees that it doesn t matter which leaf below x is reached. Lemma: A red-black tree with n internal nodes has height h at most 2log(n + 1). Proof outline: Show by induction that the subtree rooted at any node x contains at least 2 bh(x) 1 internal nodes. Show that the height of the red-black tree h is at most twice the black height of the root 2 bh(root), i.e., h 2 bh(root) Why?. COMP3600/6466: Lecture 13 2017 5

13.1 Properties of red-black trees (cont.) We show that the subtree rooted at any node x contains at least 2 bh(x) 1 internal nodes, by mathematical induction on the height of node x. Let h(x) represent the height of x. If the height of x is zero (i.e., h(x) = 0), then x must be a leaf, the subtree rooted at x contains at least 2 bh(x) 1 = 2 0 1 = 0 internal nodes. Consider a node x, assume that its children (if they do exist), By the inductive assumption is held, the internal node in the subtree rooted at a child y of x is no less than 2 bh(y) 1. The rest is to show that the claim holds for node x, i.e., the number of internal node in the subtree rooted at x is at least 2 bh(x) 1. COMP3600/6466: Lecture 13 2017 6

13.1 Properties of red-black trees (cont.) Consider a node x is an internal node with two children. Then, each child of node x has a black-height of either bh(x) or bh(x) 1, depending on whether node x is red or black. By the inductive hypothesis, each of the two children of x contains at least 2 bh(x) 1 1 internal nodes, the subtree rooted at x thus contains at least 2 (2 bh(x) 1 1) + 1 = 2 bh(x) 1 internal nodes. COMP3600/6466: Lecture 13 2017 7

13.1 Properties of red-black trees (continued) Let h be the height of the RBT, then h 2 bh(root) Why?, i.e., the black-height of the tree root is at least h/2,, bh(root) h/2. Then, the number of internal nodes contained in the RBT is at least 2 bh(root) 1 2 h/2 1 by the lemma in slide 3. If there are n internal nodes in the RBT, then n 2 h/2 1, h 2log(n + 1). Exercise: Assuming that a RBT contains n internal nodes, then, what is the maximum number N of nodes the RBT contained? Answer: As the RBT is a binary tree, the maximum number of leaves of the tree is no more than 2n if there are n internal nodes. Thus, the number of node contained in the tree N n + 2n = 3n. In other words, each of the mentioned 7 operations in the binary search tree can be implemented in a RBT within O(h) = O(log(n + 1)) = O(logN) time, where N is the number of elements in the RBT. COMP3600/6466: Lecture 13 2017 8

13.1 Operations on a red-black tree Since the height of a red-black tree is at most 2log(n + 1), a red-black tree can support the operations SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR in time O(logn), where n is the number of internal nodes in the RBT. INSERT and DELETE operations on a red-black tree might destroy its defining properties. Insertion and deletion thus have to be accompanied by some operations that restore the red-black property. For this, we may have to change the color of some nodes in the RBT. or the pointer structure of nodes in the RBT. COMP3600/6466: Lecture 13 2017 9

13.2 Rotations To maintain a balanced tree T, we change the pointer structure by using Rotations: (1) The left Rotation; and (2) The right rotation. COMP3600/6466: Lecture 13 2017 10

13.2 Rotations (cont.) Notice that the operation of either the left rotation or the right rotation only takes O(1) time by changing the pointers of several nodes, the binary search tree property of the resulting tree is still held. We show the claim as follows. We show the claim after the left rotation. Before the left rotation (i) α.key x.key (ii) x.key < y.key (iii)β.key y.key (iv) y.key < γ.key The resulting tree after the left rotation (I) α.key x.key (II) x.key y.key (III) β.key y.key IV) y.key < γ.key COMP3600/6466: Lecture 13 2017 11

13.2 Rotations (cont) Example of a left rotation (Cormen, p314) COMP3600/6466: Lecture 13 2017 12

13.2 Left rotation LEFT ROTATE(T,x) 1 y right[x] 2 right[x] left[y] 3 if left[y] NIL 4 then p[left[y]] x 5 p[y] p[x] 6 if p[x] = NIL 7 then root[t ] y 8 else if x = left[p[x]] 9 then left[p[x]] y 10 else right[p[x]] y 11 left[y] x; p[x] y RIGHT ROTATE(T,x) is similar, omitted. COMP3600/6466: Lecture 13 2017 13

13.3 Insertion in a red-black tree We use the TREE INSERT procedure for binary search trees to insert a node x into a red-black tree T as if it were a binary search tree, and then colour the new inserted node RED. At this stage, two of the five red-black tree properties might be violated: The root might be RED (the original tree is empty, or the root is colored with red after a sequence of coloring changes on the tree). A RED node might have a RED parent. However, only one violation of the TWO mentioned cases can exist. So, we need procedures for fixing these violations. COMP3600/6466: Lecture 13 2017 14

13.3 Insertion in a red-black tree (cont.) The general strategy to fix the color violations is as follows. If the second violation occurs, we do some operations that either fix it or move it to a higher level, where a level near to the tree root is higher. If it has not been fixed before we get to the tree root, the first violation occurs. We can fix the first violation by just setting the colour of the tree root to BLACK. COMP3600/6466: Lecture 13 2017 15

13.3 Insertion in a red-black tree (continued) The detailed implementation of the general strategy is as follows. Suppose we have a RED node z which has a RED parent. There are three cases. Case 1: z has a RED uncle y. Colour the parent and uncle of z BLACK and the grandparent of z RED. First case of insertion fixup (Cormen, p320) COMP3600/6466: Lecture 13 2017 16

13.3 Insertion in a red-black tree (continued) Cases 2 and 3: z has a BLACK uncle y. If z is a right child (case 2), do a left rotation at the parent of z then continue to case 3. If z is a left child (case 3), colour the parent of z BLACK and the grandparent of z RED, then do a right rotation at the grandparent of z. Second and third cases of insertion fixup (Cormen, p321) COMP3600/6466: Lecture 13 2017 17

13.3 Insertion in a red-black tree (continued) Why the insertion process works. Induction hypothesis: At every stage, either (a) there is exactly one RED with a RED parent, (b) or the root is RED, but not both of them. If case 1 occurs, the prescribed operations either fix the error or move it towards the root. If cases 2 or 3 occur, the prescribed operations fix the tree always. If the root is RED, making it BLACK fixes the tree. COMP3600/6466: Lecture 13 2017 18

COMP3600/6466: Lecture 13 2017 19