SlideShare a Scribd company logo
1 of 6
Download to read offline
1
RedBlack Tree
2
typedef structnode * ref;
struct node {
int key;
int color;
ref parent;
ref left;
ref right;
}
ref getnode(int key, int color, ref nil) {
p = new node;
p–>key = key;
p–>color = color;
p–>left = p–>right = p–>parent = nil;
return p;
}
ref nil, root;
…
nil = new node;
nil–>color = BLACK;
nil–>left = nil–>right = nil–>parent = nil;
root = nil;
…
3
void leftRotate(ref &root, ref x) {
ref nil = root–>parent;
y = x–>right;
x–>right = y–>left;
if (y–>left != nil)
y–>left–>parent = x;
y–>parent = x–>parent;
if (x–>parent == nil)
root = y;
else
if (x == x–>parent–>left)
x–>parent–>left = y;
else
x–>parent–>right = y;
y–>left = x;
x–>parent = y;
}
4
void RBT_Insertion(ref &root, int key) {
x = getNode(key, RED, NIL);
BST_Insert(root, x);
Insertion_FixUp(root, x);
}
Thao tác thêm phần tử
5
void BST_Insert(ref &root, ref x) {
ref nil = root–>parent;
y = nil;
z = root;
while (z != nil) {
y = z;
if (x–>key < z–>key)
z = z–>left;
else
z = z–>right;
}
x–>parent = y;
if (y == nil)
root = x;
else
if (x–>key < y–>key)
y–>left = x;
else
y–>right = x;
}
6
void Insertion_FixUp(ref &root, ref x) {
while (x–>parent–>color == RED)
if (x–>parent == x–>parent–>parent–>left)
ins_leftAdjust(root, x);
else
ins_rightAdjust(root, x);
root–>color = BLACK;
}
7
void ins_leftAdjust(ref &root, ref &x) {
u = x–>parent–>parent–>right;
if (u–>color == RED) { // TH 2
x–>parent–>color = BLACK;
u–>color = BLACK;
x–>parent–>parent–>color = RED;
x = x–>parent–>parent;
}
else {
if (x == x–>parent–>right) { // TH 4
x = x–>parent;
leftRotate(root, x);
}
// TH 3
x–>parent–>color = BLACK;
x–>parent–>parent–>color = RED;
rightRotate(root, x–>parent–>parent);
}
} 8
Thao tác xóa phần tử
9
void RBT_Deletion(ref &root, int k) {
z = searchTree(root, k);
if (z == nil) return;
y = ((z–>left == nil) || (z–>right == nil)) ? z :
TreeSuccessor(root, z);
x = (y–>left == nil) ? y–>right : y–>left;
x–>parent = y–>parent;
if (y–>parent == nil)
root = x;
else
if (y == y–>parent–>left)
y–>parent–>left = x;
else
y–>parent–>right = x;
if (y != z) {
z–>key = y–>key;
z–>color = y–>color;
}
if (y–>color == BLACK)
Deletion_FixUp(root, x);
free(y);
}
10
void Deletion_FixUp(ref root, ref x) {
while ((x->color == BLACK) &&
(x != root))
if (x == x->parent->left)
del_leftAdjust(root, x);
else
del_rightAdjust(root, x);
x->color = BLACK; // TH 1
}
11
void del_leftAdjust(ref &root, ref &x) {
w = x->parent->right;
if (w->color == RED) { // TH 3
w->color = BLACK;
x->parent->color = RED;
leftRotate(root, x->parent);
w = x->parent->right; }
// TH 2
if ((w->right->color == BLACK) &&
(w->left->color == BLACK)) {
w->color = RED;
x = x->parent; }
else {
if (w->right->color == BLACK) { // TH 4 (i)
w->left->color= BLACK;
w->color = RED;
rightRotate(root, w);
w = x->parent->right; }
// TH 4 (ii)
w->color = x->parent->color;
x->parent->color= BLACK;
w->right->color = BLACK;
leftRotate(root, x->parent);
x = root; }
}

More Related Content

Similar to Chuong trinh cay do den

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)Trupti Agrawal
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfmichardsonkhaicarr37
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfeyelineoptics
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdferremmfab
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaVladimir Kostyukov
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 

Similar to Chuong trinh cay do den (18)

Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Define a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdfDefine a class template for an AVL tree. Create an object of su.pdf
Define a class template for an AVL tree. Create an object of su.pdf
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
Trees gt(1)
Trees gt(1)Trees gt(1)
Trees gt(1)
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
Red black tree
Red black treeRed black tree
Red black tree
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
List out of lambda
List out of lambdaList out of lambda
List out of lambda
 
Trees
TreesTrees
Trees
 
4.2 bst 03
4.2 bst 034.2 bst 03
4.2 bst 03
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
UNIT II.ppt
UNIT II.pptUNIT II.ppt
UNIT II.ppt
 

More from Hồ Lợi

Tóm tắt các hàm chuẩn của c
Tóm tắt các hàm chuẩn của cTóm tắt các hàm chuẩn của c
Tóm tắt các hàm chuẩn của cHồ 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
 
Giao trinh c c++
Giao trinh c c++Giao trinh c c++
Giao trinh c c++Hồ 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
 

More from Hồ Lợi (20)

Xu ly chuoi
Xu ly chuoiXu ly chuoi
Xu ly chuoi
 
Tóm tắt các hàm chuẩn của c
Tóm tắt các hàm chuẩn của cTóm tắt các hàm chuẩn của c
Tóm tắt các hàm chuẩn của c
 
T4
T4T4
T4
 
Nguyen lyoop
Nguyen lyoopNguyen lyoop
Nguyen lyoop
 
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
 
Giao trinh c c++
Giao trinh c c++Giao trinh c c++
Giao trinh c c++
 
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
 

Chuong trinh cay do den

  • 1. 1 RedBlack Tree 2 typedef structnode * ref; struct node { int key; int color; ref parent; ref left; ref right; } ref getnode(int key, int color, ref nil) { p = new node; p–>key = key; p–>color = color; p–>left = p–>right = p–>parent = nil; return p; } ref nil, root; … nil = new node; nil–>color = BLACK; nil–>left = nil–>right = nil–>parent = nil; root = nil; …
  • 2. 3 void leftRotate(ref &root, ref x) { ref nil = root–>parent; y = x–>right; x–>right = y–>left; if (y–>left != nil) y–>left–>parent = x; y–>parent = x–>parent; if (x–>parent == nil) root = y; else if (x == x–>parent–>left) x–>parent–>left = y; else x–>parent–>right = y; y–>left = x; x–>parent = y; } 4 void RBT_Insertion(ref &root, int key) { x = getNode(key, RED, NIL); BST_Insert(root, x); Insertion_FixUp(root, x); } Thao tác thêm phần tử
  • 3. 5 void BST_Insert(ref &root, ref x) { ref nil = root–>parent; y = nil; z = root; while (z != nil) { y = z; if (x–>key < z–>key) z = z–>left; else z = z–>right; } x–>parent = y; if (y == nil) root = x; else if (x–>key < y–>key) y–>left = x; else y–>right = x; } 6 void Insertion_FixUp(ref &root, ref x) { while (x–>parent–>color == RED) if (x–>parent == x–>parent–>parent–>left) ins_leftAdjust(root, x); else ins_rightAdjust(root, x); root–>color = BLACK; }
  • 4. 7 void ins_leftAdjust(ref &root, ref &x) { u = x–>parent–>parent–>right; if (u–>color == RED) { // TH 2 x–>parent–>color = BLACK; u–>color = BLACK; x–>parent–>parent–>color = RED; x = x–>parent–>parent; } else { if (x == x–>parent–>right) { // TH 4 x = x–>parent; leftRotate(root, x); } // TH 3 x–>parent–>color = BLACK; x–>parent–>parent–>color = RED; rightRotate(root, x–>parent–>parent); } } 8 Thao tác xóa phần tử
  • 5. 9 void RBT_Deletion(ref &root, int k) { z = searchTree(root, k); if (z == nil) return; y = ((z–>left == nil) || (z–>right == nil)) ? z : TreeSuccessor(root, z); x = (y–>left == nil) ? y–>right : y–>left; x–>parent = y–>parent; if (y–>parent == nil) root = x; else if (y == y–>parent–>left) y–>parent–>left = x; else y–>parent–>right = x; if (y != z) { z–>key = y–>key; z–>color = y–>color; } if (y–>color == BLACK) Deletion_FixUp(root, x); free(y); } 10 void Deletion_FixUp(ref root, ref x) { while ((x->color == BLACK) && (x != root)) if (x == x->parent->left) del_leftAdjust(root, x); else del_rightAdjust(root, x); x->color = BLACK; // TH 1 }
  • 6. 11 void del_leftAdjust(ref &root, ref &x) { w = x->parent->right; if (w->color == RED) { // TH 3 w->color = BLACK; x->parent->color = RED; leftRotate(root, x->parent); w = x->parent->right; } // TH 2 if ((w->right->color == BLACK) && (w->left->color == BLACK)) { w->color = RED; x = x->parent; } else { if (w->right->color == BLACK) { // TH 4 (i) w->left->color= BLACK; w->color = RED; rightRotate(root, w); w = x->parent->right; } // TH 4 (ii) w->color = x->parent->color; x->parent->color= BLACK; w->right->color = BLACK; leftRotate(root, x->parent); x = root; } }