Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Red-Black Trees.

Presentasi serupa


Presentasi berjudul: "Red-Black Trees."— Transcript presentasi:

1 Red-Black Trees

2 DEFINISI Red-Black Trees adalah Binary Search Tree dengan menyimpan data tambahan yaitu warna (Merah atau Hitam). Syarat yang harus dipenuhi oleh sebuah Red-Black Trees antara lain: Setiap Node harus mempunyai warna Merah atau hitam Root berwarna hitam Setiap nill node diberi warna hitam Jika sebuah node merah maka kedua anaknya harus hitam Tiap node, pada seluruh path dari node tersebut ke descendant leaves mengandung jumlah node hitam yang sama

3 PRESENTASI Red Black Trees dapat dipresentasikan dengan menggunakan link list typedef enum {red,black} color; typedef struct red_black *red_black_ptr typedef struct red_black { element data; red_black_ptr l_child; red_black_ptr r_child; color l_color; color r_color; }

4 PRESENTASI typedef enum {red,black} color;
typedef struct red_black *red_black_ptr typedef struct red_black { element data; color node_color red_black_ptr l_child; red_black_ptr r_child; }

5 PRESENTASI

6 INSERT Proses insert pada Red-Black Trees sama seperti proses insert pada Binary Search Trees: Cari posisi dari node baru dimulai dari root Berikan warna merah kepada node tersebut. Periksa apakah setelah dilakukan insert tree tersebut masih RB Trees. Jika tidak perbaiki menjadi RB Trees (sesuai dengan syarat pada slide awal)

7 Insert Case 1 Jika Uncle (Y) berwarna merah
X->parent->color=black; Y->color=black; X->parent->parent->color=red; X=X->parent->parent;

8 Insert Case 2 Jika uncle(y) black node x adalah right child
Parent left child X=X->parent; Leftrotate(x) Continue to Case 3

9 Insert Case 3 Jika uncle(y) black node x adalah left child dari parent yang merupakan left child X->parent->color=black; X->parent->parent->color=red X=X->parent->parent Rightrotate(x)

10 Insert Case 4 Jika uncle(y) black node x adalah left child
dari Parent yang merupakan right child X=X->parent; Rightrotate(x) Continue to Case 5

11 Insert Case 5 Jika uncle(y) black node x adalah right child dari parent yang merupakan right child X->parent->color=black; X->parent->parent->color=red X=X->parent->parent Leftrotate(x)

12 INSERT Rotasi terdiri dari 2 macam Left Rotation (Rotasi kiri)
Right Rotation (Rotasi kanan)

13 Implementation 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; } 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 ); /* case 3 */ x=x->parent->parent right_rotate( T, x );

14 Implementation (2) else { y = x->parent->parent->left;
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 is a black node */ if ( x == x->parent->left ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; right_rotate( T, x ); /* case 3 */ x=x->parent->parent left_rotate( T, x); /* Colour the root black */ T->root->colour = black; }

15 INSERT Insert(1) 1 1 Colour root is black Insert(2) 1 No repair needed 2 Insert(3) 1 1 2 Left rotation 2 2 1 3 3 3 Insert(4) 2 2 2 Parent is red-rebalance required Case 5:parent to right of grandparent Colour Root is black 1 3 1 3 1 3 4 4 4

16 INSERT Insert(5) 2 2 2 Left rotation 1 3 1 3 1 4 4 4 3 5 5 5 Insert(6) 2 2 Parent is red-rebalance required Case 5:parent to right of grandparent 1 4 1 4 3 5 3 5 6 6 Insert(7) 2 2 2 Left rotation 1 4 1 4 1 4 3 5 3 5 3 6 6 6 5 7 7 7

17 INSERT Insert(8) 2 2 2 Parent is red-rebalance required Case 5:parent to right of grandparent Left rotation 1 4 1 4 1 4 3 6 3 6 3 6 5 7 5 7 5 7 8 8 8 4 2 6 1 3 5 7 8

18 Delete Proses delete pada RBT sama dengan proses delete pada BST
Jika yang didelete adalah node dengan warna merah maka tree tersebut masih tetap RBT. Jika node yang didelete memiliki 1 anak berwarna merah maka ganti warna anak tersebut menjadi hitam. Jika proses delete tersebut tidak menghasilkan sebuah RBT maka perbaiki (Fixup). Hal ini terjadi karena proses delete dilakukan terhadap node yang berwarna hitam. Catatan: proses perpindahan hanya melakukan perpindahan data (key) tidak warna

19 Delete

20 Delete Fixup Case 1 Precondition: To do: X adalah left child dan black
Sibling dari x adalah red (parent black) To do: Parent dari x ubah menjadi red brother dari x ubah menjadi black Left Rotate

21 Delete Fixup

22 Delete Fixup Case 2 Precondition: To do: X adalah left child dan black
Sibling dari x adalah black Kedua anak dari Brother x black To do: Sibling dari x ubah menjadi red Pindahkan x ke parent dari x

23 Delete Fixup

24 Delete Fixup Case 3 Precondition: To do: X adalah left child dan black
Sibling dari x adalah black Right child dari brother x adalah black Left child dari brother x adalah red To do: Sibling dari x ubah menjadi red Left child dari brother x ubah menjadi black Right rotate dari brother

25 Delete Fixup

26 Delete Fixup Case 4 Precondition: To do: X adalah left child dan black
Sibling dari x adalah black Right child dari brother x adalah red To do: Ubah Sibling x menjadi warna dari parent x Ubah warna parent x menjadi black Right child dari Sibling x ubah menjadi black Left rotate dari parent x dan stop

27 Delete Fixup

28 Delete Fixup delete(rbTree T, node n) node child, cut;
if isNill(del.left) or isNill(del.right) then cut = n; else cut = successor(n); end if if not isNill(cut.left) then child = cut.left; child cut.right; child.parent = cut.parent; if isNill(cut.parent) then T.root = child; if cut.parent.left = cut then cut.parent.left = child; cut.parent.right = child; if cut <> n then n.values = cut.values; if cut.color = BLACK then rbDeleteFixUp(T, child); return cut; end

29 Delete Fixup rbDeleteFixUp(rbTree T, node n)
while n <> T.root and n.color = BLACK do //Fix either case, returns a node that if red we can color black. if n = n.parent.left then n = leftCase(T, n); else n = rightCase(T, n); end if end while n.color = BLACK; end

30 Delete Fixup node rightCase(rbTree T, node n) node sibling;
sibling = n.parent.left; //CASE 1: rotate right to convert into case 2, 3 or 4 if sibling.color = RED then sibling.color = BLACK; n.parent.color = RED; rotateRight(T, n.parent); end if //CASE 2: No rotation needed, simply recolor and move up the tree. if sibling.left.color = BLACK and sibling.right.color = BLACK then sibling.color = RED; n = n.parent; else //CASE: 3: rotate left to turn this into case 4 if sibling.left.color = BLACK then sibling.right.color = BLACK; rotateLeft(T, sibling); //CASE 4: We have to rotate right sibling.color = n.parent.color; n.parent.color = BLACK; sibling.left.color = BLACK; rightRotate(T, n.parent); n = T.root // to terminate the while loop return n; end


Download ppt "Red-Black Trees."

Presentasi serupa


Iklan oleh Google