ساختمان داده ها و الگوريتم ها

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 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)
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.
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.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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.
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.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
Red-Black tree Recall binary search tree –Key values in the left subtree = the node value Operations:
Red-Black Trees CS302 Data Structures Dr. George Bebis.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
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: 
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION.
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.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION.
1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II.
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)
1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees.
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.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
Balanced Search Trees Modified from authors’ slides.
BST Trees Saurav Karmakar
G64ADS Advanced Data Structures
Red Black Trees
Red-Black Trees.
Red-Black Trees.
Design and Analysis of Algorithms
Red-Black Trees Motivations
CS200: Algorithms Analysis
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.
Red Black Tree Essentials
Red-Black Trees.
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Properties of Red-black trees
Announcements Midterm will be given on 10/21/99
Binary Search Trees Comp 122, Spring 2004.
Red-Black Trees CS302 Data Structures
Presentation transcript:

ساختمان داده ها و الگوريتم ها Red-Black Trees ساختمان داده ها و الگوريتم ها

Red-Black Trees (RBT) درختهاي قرمز و سياه (Red/Black Trees) نوعي درخت جستجوي دودويي (Binary Search Tree) است که هر گره آن علاوه بر فيلدهاي ديگر، يک بيت رنگ نيز دارد بيت رنگ دوحالته (قرمز يا سياه) است هدف از اين بيت، تضمين توازن نسبي درخت است در درخت جستجوي دودويي، عمليات جستجو ، افزودن و حذف گره هزينه اي متناسب با عمق درخت دارند. عمق درخت متوازن با N گره از مرتبه O(log N) است. عمق درخت نامتوازن با N ‌ گره از مرتبه O(N) ‌است.

Red-black Tree(RBT) RBT = BST + 1 color bit بقيه مشخصات همانند BST است ارث بري Inheritance key, left, right, p. هر گره درخت دقيقا دو فرزند دارد به جاي فرزند نداشته، null استفاده مي کنيم تمام برگهاي تهي ( فرزنداني که وجود ندارند،) به رنگ سياه هستند براي مشخص نمودن برگهاي تهي ، يک گره کمکي nil مي سازيم و از آن به جاي تمام فرزندان تهي درخت استفاده مي کنيم. مشابه گره first ‌ در ليستهاي پيوندي

Red-black Properties هر گره درخت يا سياه است يا قرمز ريشه درخت سياه است هر گره تهي(null) سياه است اگر گرهي قرمز باشد، هر دو فرزند آن سياه هستند همه مسيرهايي که از يک گره شروع شده و به برگ مي رسند، دقيقا داراي تعداد مساوي گره سياه هستند.

مثال RBT توجه: هر گره درخت دقيقا دو فرزند دارد؛ به جاي فرزند نداشته، null استفاده مي کنيم 26 17 41 null[T] 30 47 38 50

ارتفاع (عمق) درخت RBT ارتفاع گره h(x) : ارتفاع يک گره برابر با طول بزرگترين مسير گره به برگهاي درخت است. ارتفاع سياه گره x bh(x) : تعداد گرههاي سياه در هر يک از مسيرهايي است که از اين گره شروع مي شوند و به برگ null(T) ختم مي شوند برگ null(T) جزء مسير محسوب مي شود خود گره x جزء مسير محسوب نمي شود ارتفاع سياه يک درخت RBT برابر با ارتفاع سياه ريشه آن است

Height of a RBT 26 h: ارتفاع bh:ارتفاع سياه bh(x) ≤ h(x) ≤ 2 bh(x) طول بلندترين مسير از يک گره به برگي از درخت، حداکثر دو برابر طول کوتاهترين مسير از همان گره به برگهاي درخت است. 17 41 h=2 bh=1 h=2 bh=1 30 47 h=1 bh=1 38 h=1 bh=1 50 null[T]

لم1 : ارتفاع درخت RBT لم1: طول بزرگترين مسير از يک گره به برگ حداکثر دو برابر طول کوتاهترين مسير است: اثبات: تعداد گرههاي‌ سياه روي مسيرهاي که از گره x‌شروع شده و به برگي مي رسند برابر bh(x) است ( خاصيت 5) اگر طول کوتاهترين مسير را با s(x)‌نشان دهيم، bh(x) <= s(x) بنابر خاصيت چهارم، روي مسيرهاي مذکور گرههاي قرمز متوالي وجود ندارند و همه مسيرها هم به گره سياه تمام مي شوند. بنابراين روي بلندترين مسير، ترتيب گرههاي قرمز و سياه حداکثر يک در ميان و به تعداد مساوي است: h(x) <= 2bh(x) درنهايت h(x) <= 2s(x)

تعداد گرههاي RBT لم 2: تعداد گرههاي داخلي درختچه واقع در گره x ‌حداقل برابر است با:2bh(x)–1. اثبات ( با استفاده از استقرا رياضي) اگر h(x)=0 ‌، آنگاه x‌برگ است.‌ و ارتفاع سياه آن نيز صفر است. در نتيجه تعداد گرههاي درختچه برابر 20 -1 = 0 است اگر h(x) =h>0 ‌ و فرض کنيم ارتفاع سياه گره x‌برابر bh(x) = b باشد: هر کدام از فرزندان x ارتفاعي برابر با h-1 خواهند داشت. اگر فرزند درخت قرمز باشد، ارتفاع سياه آن b و در غير اين صورت b-1 ‌خواهد بود بنابر فرض استقرا هر درختچه شروع شده از فرزند x حداقل-1 2bh(x) -1 گره خواهد داشد و درنتيجه، درختچه واقع در x حداقل 2(2bh(x)-1 -1 ) + 1‌گره خواهد داشت  2 (2bh(x) – 1 – 1)+1= 2bh(x) – 1 تعداد گرههاي درختچه واقع در x

حد ارتفاع RBT لم1: طول بزرگترين مسير از يک گره به برگ حداکثر دو برابر طول کوتاهترين مسير است h(x) <= 2bh(x) لم 2: تعداد گرههاي داخلي درختچه واقع در گره x ‌حداقل برابر است با:2bh(x)–1. لم 3: حداکثر ارتفاع يک درخت RBT با n گره، برابر است با: 2 log (n+1) اثبات لم دوم: , n  2bh – 1 لم اول: bh h/2  n  2h/2 – 1  h  2 lg(n + 1).

عمليات روي RBT همانند BST ، تمام عمليات با O (log n)‌قابل انجام هستند

Left Rotation P(x) P(x) … … A A α α B β γ B β γ

Left Rotation P(x) P(x) … … A A B α α B β γ β γ

Left Rotation … … γ B A α β A α B β γ  Left Rotation  Right Rotation y A α β A x α B β γ

Left Rotation Example

Insert a node 11 2 14 1 7 15 5 8 4 Insert (4) Tree color violation

Insert a node 11 2 14 1 7 15 5 8 4 y=right(p(p(z)) if color[y] = RED then color[p[z]] ← BLACK color[y] ← BLACK color[p[p[z]]] ← RED z ← p[p[z]] 4 z FIX-UP Color violation

Insert a node 11 2 1 14 7 8 15 5 4 z if z = right[p[z]] then z ← p[z] LEFT-ROTATE(T, z) color[p[z]] ← BLACK color[p[p[z]]] ← RED RIGHT-ROTATE(T, p[p[z]]) 4 FIX-UP Color violation

Insert a node 11 14 7 15 2 8 5 1 4 z if z = right[p[z]] then z ← p[z] LEFT-ROTATE(T, z) color[p[z]] ← BLACK color[p[p[z]]] ← RED RIGHT-ROTATE(T, p[p[z]]) 4 FIX-UP Color violation

Insert a node 7 2 11 14 5 1 8 15 4 z if z = right[p[z]] then z ← p[z] LEFT-ROTATE(T, z) color[p[z]] ← BLACK color[p[p[z]]] ← RED RIGHT-ROTATE(T, p[p[z]]) FIX-UP Color violation

Delete a node 11 2 14 1 7 15 5 8 z Delete Node 7 RB Tree Coloring violation

Delete Node 11 2 14 1 7 15 5 8 nil Delete Node z = 7 z y = succssor(z) x nil

Delete Node 11 2 14 1 7 15 5 8 nil z y = successor(z) x Copy Node 8 data to node 7

Delete Node 11 2 14 z 1 8 15 5 8 x nil If color(y) = black, then fix-up color violation at x

Delete Node 11 2 14 z 1 8 15 5 x nil گره x يا فرزند گره حذف شده است يا گره nil(T)

Delete Node 11 2 14 1 7 15 5 8 nil Delete Node z = 2 z y = succssor(z) x nil

Delete Node 11 2 14 1 7 15 5 8 nil Delete Node z = 2 z y = succssor(z) x nil

Delete Node 11 5 14 1 7 15 5 8 nil Delete Node z = 2 z y = succssor(z) x nil

Delete Node 11 Delete Node z = 5 z 5 14 y 1 7 15 x 8 nil

Delete Node 11 Delete Node z = 5 z 7 14 y 1 7 15 x 8 nil

Delete Node 11 Delete Node z = 5 z 7 14 y 1 7 15 x 8 nil

Delete Node 11 Delete Node z = 5 z 7 14 y 1 15 x 8 nil Color Violation

نقض شرايط RBT اگر y (گره حذف شده) سياه باشد، در حالات زير خاصيت RBT نقض مي شود:( x گرهي است که قبلا فرزند y بوده است. اگر y فرزندي نداشته باشد، x گره nil درخت است.) y ريشه درخت بوده و فرزند جایگزین شده قرمز است  x، ريشه جديد، قرمز شده است (نقض 1) p(y)=p(x) قرمز است  x ,p(x) هر دو قرمزند ( نقض 4) هر مسيري که قبلا شامل گره y بود، حالا يک گره سياه کمتر از بقيه دارد( نقض 5) حل: فرض مي کنيم گره x داراي يک نشان سياه اضافي است خاصيت 1 نقض مي شود با يک سري عمليات باسازي و تغيير رنگ، خواص مورد نظر تامين مي شوند

اصلاح درخت انتقال گره x به سطوح بالاتر تا جايي که:

Delete Node y گره حذف شده و x گرهي است که قبلا فرزند y بوده است. اگر y فرزندي نداشته باشد، x گره nil درخت است. RB-DELETE-FIXUP(T, x) while x ≠ root[T] and color[x]= BLACK do if x = left[p[x]] then w ← right[p[x]] //Four different cases else (same as then clause with "right" and "left" exchanged) color[x] ← BLACK

اصلاح رنگ درخت بعد از حذف يک گره RB-Delete-Fixup(T, x) while x  root[T ] and color[x] = BLACK do if x = left[p[x]] then w  right[p[x]] if color[w] = RED then color[w]  BLACK // Case 1 color[p[x]]  RED // Case 1 LEFT-ROTATE(T, p[x]) // Case 1 w  right[p[x]] // Case 1

اصلاح رنگ - ادامه RB-Delete-Fixup(T, x) (Contd.) /* x is still left[p[x]] */ if color[left[w]] = BLACK and color[right[w]] = BLACK then color[w]  RED // Case 2 x  p[x] // Case 2 else if color[right[w]] = BLACK then color[left[w]]  BLACK // Case 3 color[w]  RED // Case 3 RIGHT-ROTATE(T,w) // Case 3 w  right[p[x]] // Case 3 color[w]  color[p[x]] // Case 4 color[p[x]]  BLACK // Case 4 color[right[w]]  BLACK // Case 4 LEFT-ROTATE(T, p[x]) // Case 4 x  root[T ] // Case 4 else (same as then clause with “right” and “left” exchanged) color[x]  BLACK

حالت اول: w قرمز است p[x] B D w x B E A D x A C C E   new w           فرزندان w سياه هستند رنگ w‌ ‌و p(x) را باهم عوض کن ( دقت کنيد: p(x) حتما سياه است! (چرا؟)) left rotate(T , p(x)) برادر جديد x سياه است. زيرا يکي از فرزندان پيشين w است نهايتا گره x‌ داراي دو سياه و گره w داراي يک سياه است يکي از حالات 2- 4 اتفاق مي افتد

حالت دوم: w و فرزندانش سياه هستند p[x] c new x c B B w x A D A D     C E C E         w و فرزندانش سياه هستند از گره x و w يک سياه کم کرده و به پدر اين دو يک سياه اضافه کن حالا گره x‌ داراي يک سياه است و يک گره عادي است p(x) داراي يک مشخصه سياه اضافي است  الگوريتم را از x جديد، p(x) ادامه بده اگر p(x) قرمز بوده و از حالت اول به اين حالت آمده ايم رنگ p(x) را سياه کن  الگوريتم خاتمه پيدا مي کند

حالت سوم: w و فرزند راست سياه هستند و فرزند چپ قرمز است c c B B w x x new w A D A C     D C E   E       w و فرزند راست سياه هستند و فرزند چپ قرمز است رنگ w‌ و فرزند چپ را باهم عوض کن right rotate(w) w سياه است و حالا فرزند راست آن قرمز است  چند تغيير رنگي ساده لازم است!

حالت چهارم: w سياه و فرزند راست آن قرمز است c B D w x B E A D  x    c’ A C C E         حالت چهارم: w سياه و فرزند راست آن قرمز است w را مشابه p(x) رنگ کن p(x) و فرزند راست w را سياه رنگ کن left rotate p[x]. سياه اضافي گره x را حذف کن  پايان الگوريتم! براي پايان الگوريتم ، x جديد به ريشه درخت اشاره مي کند!

آناليز الگوريتم حذف گره : O ( log n ) تامين خاصيت رنگي تنها حالت دوم ممکن است تکرار شود در هر تکرار گره x‌ يک سطح در درخت بالاتر مي رود حداکثر تکرار اين مرحله از الگوريتم ارتفاع درخت است هزينه نهايي O ( log n )

جمع بندي RBT درخت جستجوي دودويي با مشخصه اضافه “رنگ” است با مديريت خواص RBT‌مي توان تمام عمليات درخت جستجوي دودويي را با هزينه O(log n) انجام داد که در آن n تعداد گرههاي درخت است عمليات حذف و اضافه کردن گره به درخت ممکن است سبب نقض خواص رنگي RBT شوند. در اين صورت بايد درخت را از نظر رنگي اصلاح کرد

تمرين الگوريتمي بنويسيد که يک درخت جستجوي دوديي رنگ شده را بگيرد و تعيين کند آيا اين درخت يک درخت سياه و قرمز است يا نه! آيا مي توانيد الگوريتمي در حالت کلي ارائه دهيد که هر درخت دلخواهي را به درخت RBT تبديل کند ؟مرتبه اجرايي الگوريتم شما چيست ؟