Red-black tree Lai Ah Fur. Background: AVL trees may require many restructure operations (rotations) to be performed after an element removal, (2,4) trees.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Binary Search Trees Comp 550.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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.
資料結構實習-十 Binary Tree Traversal.
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.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
:Count the Trees ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10007:Count the Trees 解題者:楊家豪 解題日期: 2006 年 3 月 題意: 給 n 個點, 每一個點有自己的 Label,
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
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.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
AVL tree self-adjusting tree Lai Ah Fur. AVL tree discovers: Adelson-Velskii and Landis balanced Binary search tree the depth of the tree: O(lg N) definition:
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
AA Trees.
Balanced Search Trees Modified from authors’ slides.
BST Trees Saurav Karmakar
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
ساختمان داده ها و الگوريتم ها
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red Black Tree Essentials
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

red-black tree Lai Ah Fur

Background: AVL trees may require many restructure operations (rotations) to be performed after an element removal, (2,4) trees may require many fusing or split operations to be performed after either an insertion or removal. The red-black tree, does not have these drawbacks. It requires that only O(1) structural changes be made after an update in order to stay balanced.

Property of A red-black tree A red-black tree is a binary search tree with nodes colored red and black in a way that satisfies the following properties: –Root Property: The root is black. –External Property: Every external node is black. –Internal Property: The children of a red node are black. –Depth property: All the external nodes have the same black depth, which is defined as the number of black ancestors minus one. –The height of a red-black tree storing n items is O(log n).

Example of A red-black tree

x y β α γ x y β α y x β α γ L EFT -R OTATE (T,x) R IGHT -R OTATE (T,y) L EFT -R OTATE (T,x) 1 y←right[x]//Set y. 2 right[x] ← left[y] //Turn y’s left subtree into x’s right subtree. 3 if left[x] ≠ nil[T] 4 then p[left[y] ] ←x // β’s father 5 p[y ] ← p[x ] //Link x’s parent to y. 6 if p[x ] = nil[T] 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//Put x on y’s left 12 p[x ] ←y

RB-I NSERT (T,z) //insert z into T 1 y ← nil[T] 2 x ← root[T] 3 while x≠ nil[T] 4 do y←x 5 if key[z]< key[x] 6 then x ← left[x] 7 else x ← right [x] 8 p[z] ←y 9 if y=nil[T] 10 then root[T] ← z 11 else if key[z]<key[y] 12 then left[y] ← z 13 else right[y] ← z 14 left[z] ←nil[T] 15 right[z] ← nil[T] 16 color[z] ← RED 17 RB-I NSERT -F IXUT (T,z)

RB-INSERT-FIXUP(T,z) 1 while color [p[z]] = RED 2 do if p[z]=left [p[p[z]]] 3 then y ← right[p[p[z]]] 4 if color [y] = RED 5 then color p[z] ← BLACK //case 1 6 color [y ] ← BLACK //case 1 7 color [p[p[z]]] ← RED //case 1 8 z ←[p[z]] //case 1 9 else if z = right [p[z]] 10 then z← p[z] //case 2 11 L EFT -R OTATE (T,z) //case 2 12 color [p[z]] ← BLACK //case 3 13 color [p[p[z]]] ← RED // //case 3 14 R IGHT -R OTATE (T, p[p[z]]) //case 3 15 else (same as then clause with “ right”and “left” exchanged 16 color [root [T]] ←BLACK

z z y Case 1 : z’s uncle y is red y Case 2 (a) (b) 7

Case z y Case z (c) (d)

Case 1-1: z’s uncle y is red α α C D B z y A βγ δε C D B z A βγ δε new z α C DB z y A β γ δ ε C DB z y A βα γ δ ε 做法 : 改變 parent, uncle, grandparent 的 color Problem: 連續雙 red α β γ δ ε :Black height 不變

Case 1-2: z’s uncle y is red α C D B z y A β γ δ εε α C DB z y A β γ δε new z 做法 : 改變 parent, uncle, grandparent 的 color Problem: 連續雙 red α β γ δ ε :Black height 不變 If c’s parent is red? Continue… C D B z y A β γ δ new z α C DB z y A β γ δε

Case 2-1: z’s uncle y is black and z is a right child Case 3-1: z’s uncle y is black and z is a left child α C B z y A β γ δ case 3-1case 2-1 C B z y A β α γ δ B A z βα γ δ C Left rotation Change color: parent & grandpa right rotation α C B z y A βγ δ Case 3-2 Case 2-2 B C z βα γ δ A α C y B z A z β γ δ Right rotate

4 (a) (b) (c) Case 3-2 (d) (e) Case 1-2 +root must be black insertion

(g) (f)

5 (h) Insert

5 (i)Insert 14 Case

5 (j)

5 (k)

5 (l)

5 (m) Case

5 (n)

5 (o) Insert 17 Case

5 (p) Case

12 (q)

Insertion complexity The insertion of a key-element item in a red-black tree storing n items can be done in O(log n) time and at most O(log n) recolorings and one trinode restructuring (a restructure operation).

RB-D ELETE (T,z) 1 if left[z]=nil[z] or right[z]=nil[T] 2 then y ←z 3 else z ←T REE -S UCCESSOR (z) 4 if left[y] ≠ nil[T] 5 then x← left[y] 6 else x ← right[y] 7 p[x] ← p [y] 8 if p[y]= nil[T] 9 then root [T] ← x 10 else if y=left [p[z]] 11 then left [p[z]] ← x 12 else right [p[z]] ← x 13 if y ≠z 14 then key [z] ← key [y] 15 copy y’s satellite data into z 16 if color [y] = BLACK 17 then RB-D ELETE -F IXUP (T,x) 18 return y

RB-D ELETE F IXUP (T,x) //y 為真正被 deleted 之 node, x 是 y 的 right or left child 1 While x ≠ root[T] and color[x] = BLACK 2 do if x =left [p[x]] 3 then w ← right [p[x]] 4 if color[w] = RED 5 then color[w] ← BLACK //Case 1 6 color [p[x]] ← RED //Case 1 7 L EFT -R OTATE (T,p[x]) //Case 1 8 w ← right [p[x]] //Case 1 9 if color [left[w]] = BLACK and color [right[w]]= BLACK 10 then color[w] ← RED//Case 2 11 x ← p[x] //Case 2 12 else if color [left[w]] = BLACK 13 then color [left[w]] ← BLACK //Case 3 14 color[w] ← RED //Case 3 15 R IGHT -R OTATE (T,w) //Case 3 16 w ←right [p[x]] //Case 3 17 color[w] ← color [p[x]] //Case 4 18 color [p[x]] ← BLACK //Case 4 19 color [right[w]] ← BLACK //Case 4 20 L EFT -R OTATE (T,p[x]) //Case 4 21 x ← root[T] //Case 4 22 else (same as then clause with”right”and”left”exchanged) 23 color[x] ← BLACK // 若 x is red, 改為 black, black height 即能維持

B A x βα Case 1 δγζε D CE w D x E w B AC βαδγ ζε new w (a) B A x βα Case 2 δγζε D CE w new x (b) c B A βα δγζε D CE c :Red or black (C) (d) y 為真正被 deleted 之 node, x 是 y 的 right or left child restructure Reduce 1 black height recolor

B A x βα Case 3 δγζε D CE w (c) B A x βα Case 4 δγζε D CE w (d) c c new w B A βα δ γ ζε C D E c x c’ D EB AC βαδγ ζε c new x=root[T]

Case 1: x’s sibling w is red Case 2: x’s sibling w is black, and both of w’s children are black Case 3: x’s sibling w is black, w’s left child is red, and w’s right child is black Case 4: x’s sibling w is black, and w’s right child is red

If v is a 2-node, then keep the (black) children of v as is. If v is a 3-node, then create a new red node w, give v’s first two (black) children to w, and make w and v;s third child be the two children of v. If v is a 4-node, then create two new red nodes w and z, give v’s first two (black) children to w, give v’s last two (black) children to z, and make w and z be the two children of v.

12 (a) initial deletion

12 (b) Delete

(c) Delete restructure

7 (d) Delete 17

7 (e) Delete 18

7 (f)

7 (g) After recoloring Delete 15

7 (h) Delete 16

7 (i) (j) (k) adjustment recoloring

(a) (b) (c) or 7 86

Insertion: Case 1: The Sibling w of v is Black u v z w u v z w u v z w u v zw (a) black Double red

20 b a c (b) After a trinode restructuring 3010 Take node z, its parent v, and grandparent u, and temporarily relabel them as a,b,and c, in left-to-right order, so that a, b, c will be visited in this order by an inorder tree traversal. Replace the grandparent u with the node labeled b, and nodes a and c the children of b, keeping inorder relationships unchanged.

Case 2: The Sibling w of v is Red … 30 … u v w (a) z 30 u v w (b) z … recoloring

… 30 … x y r z 40 … (a) Deletion: case 1: the sibling y of r is black and Has a red child z.

… 30 … … (b) x y z r 40

… 20 … … (c)After restructure 20 b a c r (a)(b)

10 30 … x y r (a) 10 … Case 2: the sibling y of r is black and both children of y are black.

x y r (b) 10 … … 10 After recoloring

x y r (a)

x y r (b) After recoloring

20 30 (a) … 10 … 40 … 30 x y r z Case 3: the sibling y of r is red.

20 30 (b) After adjustment … 10 … 40 … 20 y zx r

Deletion complexity The algorithm for removing an item from a red-black tree with n items takes O(log n) time and performs O(log n) recolorings and at most one adjustment plus one additional trinode restructuring. Thus it perform at most two restructure operations.

2 5 (1)Insert 2(2)Insert (3)Insert Change Color 2 13 (4)Insert (5)Insert RL (6)Insert

(7)Insert RR (8)Insert (9)Insert RL

(1)Delete Rotate Del (2)Delete Del

最主要的三個判斷 判斷一:爸爸是爺爺的左 or 右兒子 判斷二: uncle 是不是紅的 判斷三:我是爸爸的左 or 右兒子 所以會有八種可能 請往下看 ( 注意:例圖不是由樹根畫起 而是最低的三層節點。 )

爸爸是爺爺的左兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊

爸爸是爺爺的右兒子系列 Case 1 Uncle 是紅的 Case 2 在爸爸右邊 Case 3 在爸爸左邊

Case 1 (uncle 是紅的 ) 解決方法 爺爺變紅色 爸爸變黑色 原來的樣子 回左兒子系列

Case 2 ( 爸在爺右邊 ) 解決方法 原來的樣子 做向左旋轉 這樣就變成可以用 Case 3 來解決了 回左兒子系列

Case 3 ( 爸在爺左邊 ) 解決方法 原來的樣子 爺爺變紅色 爸爸變黑色 做向右旋轉 回左兒子系列

8 8 加入 加入 變色 Case 3 2 右轉 Case 2 左轉 加入 8

8 7 5 Case 變色 2 根黑色 Case 變色 加入 4 加入 2 加入 6

加入 3

Case 變色 Case 變色 2 右轉 加入 1

謝謝您的流覽 希望有所幫助 有問題請洽

exercise Complete the following algorithm: RB- DELETE FIXUP(T,x) & RB-INSERT- FIXUP(T,z) 實作習題四 : 以 java 實作 red-black tree (similar to 習題 1)