SlideShare a Scribd company logo
1 of 37
Download to read offline
Cấu trúc dữ liệu và thuật toán
Tìm kiếm
Các cây cân bằng động khác và cây đỏ - đen
(Red-Black and Other Dynamically BalancedTrees)
Tìm kiếm – Viếng thăm (Re-visited)
• Cây nhị phân O(log n) nếu nó là cây cân bằng
• Cây nhị phân đơn giản tốt cho các mảng tĩnh
• Tần xuất thấp (preferably zero) cho insertions/deletions
Nhưng bộ dữ liệu của tôi có thể thay đổi!
• Bộ dữ liệu động
• Cần thiết phải tạo ra cây cân bằng
• Đầu tiên, kiểm tra một vài hoạt động của cây cơ
bản.
• Hữu ích trong một vài cách!
Cây du lịch
• Du lịch = viếng thăm tất cả các node của cây
• Ba chọn lựa cơ bản
Thứ tự trước
• Gốc
• Cây con trái
• Cây con phải
x A + x + B C x D E F
L R L L R
Cây du lịch
• Du lịch = viếng thăm tất cả các node của cây
• Ba chọ lựa cơ bản
Thứ tự giữa
• Cây con trái
• Gốc
• Cây con phải
A x B + C x D x E + F
L R
L
11
Cây du lịch
• Du lịch = Viếng thăm các node của cây
• Ba chọn lựa cơ bản
Thứ tự sau
• Cây con trái
• Cây con phải
• Gốc
A B C + D E x x F + x
L R
L
11
Cây du lịch
Thứ tự sau
• Cây con trái
• Cây con phải
• Gốc
Công thức đảo
• Công thức đại số
= Cây du lịch nào?
(A (((BC+)(DEx) x) F +)x )
11
(A x(((B+C) x(DxE))+F))
Cây – Tìm kiếm
• Cây tìm kiếm nhị phân
• Tạo một danh sách sắp xếp theo thứ tự giữa
• Thư tự : A D E G H K L M N O P T V
Cây – Tìm kiếm
• Cây tìm kiếm nhị phân
• Bảo quản thứ tự
• Quan sát thấy rằng: biến đổi này bảo quản cây tìm kiếm
Cây- tìm kiếm
• Cây tìm kiếm nhị phân
• Bảo quản thứ tự
• Quan sát thấy rằng: biến đổi này bảo quản cây tìm kiếm
• Chúng tôi thực hiện một góc quay đối với cây con
về các node T và O.
Cây - Xoay
• Cây tìm kiếm nhị phân
• Góc quay có thể thực hiện theo huớng trái hoặc phải
(left- or right-rotations)
• Cho cả hai cây: Du lịch theo thứ tự giữa(inorder) là
A x B y C
Cây - Xoay
• Cây tìm kiếm nhị phân
• Góc quay có thể thực hiện theo huớng trái hoặc phải
(left- or right-rotations)
• Chú ý rằng: Việc xoay cần thiết phải di chuyển
B từ con phải của x đến con trái của y
Cây -Các cây đỏ đen
• Một cây Red-Black
• Cây tìm kiếm nhị phân
• Mỗi node có “mầu” red hoặc black
• Một cây nhị phân thông thường với các node được tô
màu tạo thành cây đỏ- đen
Cây – Cây Đỏ Đen
• Một cây Đỏ Đen (A Red-Black Tree)
• Tất cả các node là Đỏ hoặc Đen
• Các lá là Đen (BLACK)
Khi bạn tìm hiểu
đoạn code rb-tree, bạn sẽ
gặp các node gác (black)
được bổ xung như các lá.
Chúng không chứa dữ liệu.
Các node gác (black)
Cây – Cây Đỏ Đen
• Một cây Đỏ Đen (A Red-Black Tree)
• Tất cả các node là Đỏ hoặc Đen
• Các lá là Đen (BLACK)
• Nếu một node là RED,
thì cả hai con của nó
là BLACK
Điều này có nghĩa: không có
Một nhánh nào tồn tại hai node
Đỏ kế liền nhau.
(Nhưng bất cứ các node BLACK
nào cũng có thể kế liền nhau.)
Cây – Cây Đỏ Đen
• Một cây Đỏ Đen (A Red-Black Tree)
• Tất cả các node là Đỏ hoặc Đen
• Các lá là Đen (BLACK)
• Nếu một node là RED,
thì cả hai con của nó
là BLACK
• Mọi nhánh
từ một node đến một lá
chứa cùng số lượng
các node BLACK
Tính từ gốc(root),
có 3 node BLACK
trên tất cả các đường
Cây – Cây Đỏ Đen
• Một cây Đỏ Đen (A Red-Black Tree)
• Tất cả các node là Đỏ hoặc Đen
• Các lá là Đen (BLACK)
• Nếu một node là RED,
thì cả hai con của nó
là BLACK
• Mọi nhánh
từ một node đến một lá
chứa cùng số lượng
các node BLACK
Chiều dài của nhánh này chính là
Chiều cao các node đen của cây
Cây – Cây Đỏ Đen
• Lemma
Một RB-Tree với n node có
height 2 log(n+1)
• Proof .. See Cormen
• Bản chất,
height 2 black height
• Thời gian tìm kiếm
O( log n )
Giống như một
Cây nhị phân,
Bổ xung thêm
hai Thuộc tính
đánh Dấu
Cây – Cây Đỏ Đen
• Cấu trúc dữ liệu
• Như chúng tôi đã biết, Các node trong cây red-black cần
biết cha mẹ của chúng,
• Do đó, chúng tôi cần cấu trúc dữ liệu này
struct t_red_black_node {
enum { red, black } colour;
void *item;
struct t_red_black_node *left,
*right,
*parent;
}
Cây - Insertion
• Chèn thêm một new node
• Yêu cầu một re-balance của cây
rb_insert( Tree T, node x ) {
/* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
Nhãn của node
x
Chèn node
4
Tô màu red
Cây - Insertion
rb_insert( Tree T, node x ) {
/* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
Trong khi chưa đi tới root
và cha của x là red
x->parent
Cây - Insertion
rb_insert( Tree T, node x ) {
/* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
Nếu x nằm ở bên trái ông của nó
x->parent
x->parent->parent
Trees - Insertion
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
y là bác phải của x
x->parent
x->parent->parent
“bác” phải
Trees - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
x->parent
x->parent->parent
“Bác” phải
Nếu bác là red, đổi mầu của y,
ông
và cha.
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
Cha của x lại nằm bên trái,
Đánh dấu bác của x
Nhưng bác là black tại thời
điểm này
New x
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
.. Nhưng bác là black tại thời
điểm này và x nằm bên phải
Cha của nó
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
.. Vì thế chuyển x lên và
quay x như một gốc ...
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
.. Nhưng cha của x vẫn là red ...
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
.. Bác là black ..
.. và x chuyển về trái cha của nó
bác
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
else { /* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
.. Vì thế chúg ta có trường hợp
Cuối cùng ..
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
else { /* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
.. Đổi mầu
Và quay ..
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
else { /* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
else { /* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
Bây giờ, đây là cây red-black ..
Vì thế, chúng ta kết thúc!
Cây - Insertion
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
else { /* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
right_rotate( T, x->parent->parent );
}
}
else ....
Các trường hợp là tương
đương khi cha nằm ở bên
Phải của ông!
Cây Red-black – Phân tích
• Addition
• Insertion So sánh O(log n)
• Fix-up
• Với tưng giai đoạn,
x di chuyển trong cây
tới một mức đỉnh của nó O(log n)
• Overall O(log n)
• Deletion
• cũng O(log n)
Cây đỏ đen – Cái gì bạn cần biết?
• Các yêu cầu bạn cần nắm:
• Thuật toán tồn tại có liên quan
• Định nghĩa thế nào là cây đỏ đen
• Khi nào sử dụng nó
• ie Những bài toán nào, nó có thể giải đáp?
• Độ phức tạp của nó
• Nó hoạt động như thế nào
• Nơi nà, nó có thể ứng dụng
• Làm thế nào để chuyển nó vào ứng dụng của bạn.

More Related Content

More from Hồ Lợi

Lect04 functions
Lect04 functionsLect04 functions
Lect04 functionsHồ Lợi
 
Ky thuatkhudequy
Ky thuatkhudequyKy thuatkhudequy
Ky thuatkhudequyHồ Lợi
 
Itt epc assignment
Itt epc assignmentItt epc assignment
Itt epc assignmentHồ Lợi
 
Huong danontapc
Huong danontapcHuong danontapc
Huong danontapcHồ Lợi
 
H hai epc_baitap
H hai epc_baitapH hai epc_baitap
H hai epc_baitapHồ Lợi
 
Giaotrinhbaitapkythuatlaptrinh
GiaotrinhbaitapkythuatlaptrinhGiaotrinhbaitapkythuatlaptrinh
GiaotrinhbaitapkythuatlaptrinhHồ Lợi
 
Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2Hồ Lợi
 
Epc assignment
Epc assignmentEpc assignment
Epc assignmentHồ Lợi
 
Epc test practical
Epc test practicalEpc test practical
Epc test practicalHồ Lợi
 
De thic++ --th
De thic++ --thDe thic++ --th
De thic++ --thHồ Lợi
 
Cpl test1%20key
Cpl test1%20keyCpl test1%20key
Cpl test1%20keyHồ Lợi
 

More from Hồ Lợi (20)

Lect04 functions
Lect04 functionsLect04 functions
Lect04 functions
 
Ky thuatkhudequy
Ky thuatkhudequyKy thuatkhudequy
Ky thuatkhudequy
 
Itt epc assignment
Itt epc assignmentItt epc assignment
Itt epc assignment
 
Huong danontapc
Huong danontapcHuong danontapc
Huong danontapc
 
H hai epc_baitap
H hai epc_baitapH hai epc_baitap
H hai epc_baitap
 
Gtrinh oop
Gtrinh oopGtrinh oop
Gtrinh oop
 
Giaotrinhbaitapkythuatlaptrinh
GiaotrinhbaitapkythuatlaptrinhGiaotrinhbaitapkythuatlaptrinh
Giaotrinhbaitapkythuatlaptrinh
 
Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2Giao trinh ky thuat lap trinh 2
Giao trinh ky thuat lap trinh 2
 
File trong c_
File trong c_File trong c_
File trong c_
 
Epc assignment
Epc assignmentEpc assignment
Epc assignment
 
Epc test practical
Epc test practicalEpc test practical
Epc test practical
 
De thic++ --th
De thic++ --thDe thic++ --th
De thic++ --th
 
Dethi c++ -lt
Dethi c++ -ltDethi c++ -lt
Dethi c++ -lt
 
Debug trong c
Debug trong cDebug trong c
Debug trong c
 
D05 stl
D05 stlD05 stl
D05 stl
 
Cpl test3
Cpl test3Cpl test3
Cpl test3
 
Cpl test2
Cpl test2Cpl test2
Cpl test2
 
Cpl test1
Cpl test1Cpl test1
Cpl test1
 
Cpl test1%20key
Cpl test1%20keyCpl test1%20key
Cpl test1%20key
 
Chuong14
Chuong14Chuong14
Chuong14
 

Bai8 caynhiphan

  • 1. Cấu trúc dữ liệu và thuật toán Tìm kiếm Các cây cân bằng động khác và cây đỏ - đen (Red-Black and Other Dynamically BalancedTrees)
  • 2. Tìm kiếm – Viếng thăm (Re-visited) • Cây nhị phân O(log n) nếu nó là cây cân bằng • Cây nhị phân đơn giản tốt cho các mảng tĩnh • Tần xuất thấp (preferably zero) cho insertions/deletions Nhưng bộ dữ liệu của tôi có thể thay đổi! • Bộ dữ liệu động • Cần thiết phải tạo ra cây cân bằng • Đầu tiên, kiểm tra một vài hoạt động của cây cơ bản. • Hữu ích trong một vài cách!
  • 3. Cây du lịch • Du lịch = viếng thăm tất cả các node của cây • Ba chọn lựa cơ bản Thứ tự trước • Gốc • Cây con trái • Cây con phải x A + x + B C x D E F L R L L R
  • 4. Cây du lịch • Du lịch = viếng thăm tất cả các node của cây • Ba chọ lựa cơ bản Thứ tự giữa • Cây con trái • Gốc • Cây con phải A x B + C x D x E + F L R L 11
  • 5. Cây du lịch • Du lịch = Viếng thăm các node của cây • Ba chọn lựa cơ bản Thứ tự sau • Cây con trái • Cây con phải • Gốc A B C + D E x x F + x L R L 11
  • 6. Cây du lịch Thứ tự sau • Cây con trái • Cây con phải • Gốc Công thức đảo • Công thức đại số = Cây du lịch nào? (A (((BC+)(DEx) x) F +)x ) 11 (A x(((B+C) x(DxE))+F))
  • 7. Cây – Tìm kiếm • Cây tìm kiếm nhị phân • Tạo một danh sách sắp xếp theo thứ tự giữa • Thư tự : A D E G H K L M N O P T V
  • 8. Cây – Tìm kiếm • Cây tìm kiếm nhị phân • Bảo quản thứ tự • Quan sát thấy rằng: biến đổi này bảo quản cây tìm kiếm
  • 9. Cây- tìm kiếm • Cây tìm kiếm nhị phân • Bảo quản thứ tự • Quan sát thấy rằng: biến đổi này bảo quản cây tìm kiếm • Chúng tôi thực hiện một góc quay đối với cây con về các node T và O.
  • 10. Cây - Xoay • Cây tìm kiếm nhị phân • Góc quay có thể thực hiện theo huớng trái hoặc phải (left- or right-rotations) • Cho cả hai cây: Du lịch theo thứ tự giữa(inorder) là A x B y C
  • 11. Cây - Xoay • Cây tìm kiếm nhị phân • Góc quay có thể thực hiện theo huớng trái hoặc phải (left- or right-rotations) • Chú ý rằng: Việc xoay cần thiết phải di chuyển B từ con phải của x đến con trái của y
  • 12. Cây -Các cây đỏ đen • Một cây Red-Black • Cây tìm kiếm nhị phân • Mỗi node có “mầu” red hoặc black • Một cây nhị phân thông thường với các node được tô màu tạo thành cây đỏ- đen
  • 13. Cây – Cây Đỏ Đen • Một cây Đỏ Đen (A Red-Black Tree) • Tất cả các node là Đỏ hoặc Đen • Các lá là Đen (BLACK) Khi bạn tìm hiểu đoạn code rb-tree, bạn sẽ gặp các node gác (black) được bổ xung như các lá. Chúng không chứa dữ liệu. Các node gác (black)
  • 14. Cây – Cây Đỏ Đen • Một cây Đỏ Đen (A Red-Black Tree) • Tất cả các node là Đỏ hoặc Đen • Các lá là Đen (BLACK) • Nếu một node là RED, thì cả hai con của nó là BLACK Điều này có nghĩa: không có Một nhánh nào tồn tại hai node Đỏ kế liền nhau. (Nhưng bất cứ các node BLACK nào cũng có thể kế liền nhau.)
  • 15. Cây – Cây Đỏ Đen • Một cây Đỏ Đen (A Red-Black Tree) • Tất cả các node là Đỏ hoặc Đen • Các lá là Đen (BLACK) • Nếu một node là RED, thì cả hai con của nó là BLACK • Mọi nhánh từ một node đến một lá chứa cùng số lượng các node BLACK Tính từ gốc(root), có 3 node BLACK trên tất cả các đường
  • 16. Cây – Cây Đỏ Đen • Một cây Đỏ Đen (A Red-Black Tree) • Tất cả các node là Đỏ hoặc Đen • Các lá là Đen (BLACK) • Nếu một node là RED, thì cả hai con của nó là BLACK • Mọi nhánh từ một node đến một lá chứa cùng số lượng các node BLACK Chiều dài của nhánh này chính là Chiều cao các node đen của cây
  • 17. Cây – Cây Đỏ Đen • Lemma Một RB-Tree với n node có height 2 log(n+1) • Proof .. See Cormen • Bản chất, height 2 black height • Thời gian tìm kiếm O( log n )
  • 18. Giống như một Cây nhị phân, Bổ xung thêm hai Thuộc tính đánh Dấu Cây – Cây Đỏ Đen • Cấu trúc dữ liệu • Như chúng tôi đã biết, Các node trong cây red-black cần biết cha mẹ của chúng, • Do đó, chúng tôi cần cấu trúc dữ liệu này struct t_red_black_node { enum { red, black } colour; void *item; struct t_red_black_node *left, *right, *parent; }
  • 19. Cây - Insertion • Chèn thêm một new node • Yêu cầu một re-balance của cây rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Nhãn của node x Chèn node 4 Tô màu red
  • 20. Cây - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Trong khi chưa đi tới root và cha của x là red x->parent
  • 21. Cây - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Nếu x nằm ở bên trái ông của nó x->parent x->parent->parent
  • 22. Trees - Insertion /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; y là bác phải của x x->parent x->parent->parent “bác” phải
  • 23. Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; x->parent x->parent->parent “Bác” phải Nếu bác là red, đổi mầu của y, ông và cha.
  • 24. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent;
  • 25. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Cha của x lại nằm bên trái, Đánh dấu bác của x Nhưng bác là black tại thời điểm này New x
  • 26. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. Nhưng bác là black tại thời điểm này và x nằm bên phải Cha của nó
  • 27. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. Vì thế chuyển x lên và quay x như một gốc ...
  • 28. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x );
  • 29. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. Nhưng cha của x vẫn là red ...
  • 30. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. Bác là black .. .. và x chuyển về trái cha của nó bác
  • 31. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); } .. Vì thế chúg ta có trường hợp Cuối cùng ..
  • 32. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); } .. Đổi mầu Và quay ..
  • 33. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); }
  • 34. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); } Bây giờ, đây là cây red-black .. Vì thế, chúng ta kết thúc!
  • 35. Cây - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ x->parent->colour = black; x->parent->parent->colour = red; right_rotate( T, x->parent->parent ); } } else .... Các trường hợp là tương đương khi cha nằm ở bên Phải của ông!
  • 36. Cây Red-black – Phân tích • Addition • Insertion So sánh O(log n) • Fix-up • Với tưng giai đoạn, x di chuyển trong cây tới một mức đỉnh của nó O(log n) • Overall O(log n) • Deletion • cũng O(log n)
  • 37. Cây đỏ đen – Cái gì bạn cần biết? • Các yêu cầu bạn cần nắm: • Thuật toán tồn tại có liên quan • Định nghĩa thế nào là cây đỏ đen • Khi nào sử dụng nó • ie Những bài toán nào, nó có thể giải đáp? • Độ phức tạp của nó • Nó hoạt động như thế nào • Nơi nà, nó có thể ứng dụng • Làm thế nào để chuyển nó vào ứng dụng của bạn.