Analisis Algoritma_8x(1)

Embed Size (px)

Citation preview

  • 8/13/2019 Analisis Algoritma_8x(1)

    1/29

    Analisis Algoritma

    Modul-8 : BST dan Red-Black Tree

  • 8/13/2019 Analisis Algoritma_8x(1)

    2/29

    Representasi Pohon Biner

  • 8/13/2019 Analisis Algoritma_8x(1)

    3/29

    Binary Search Tree

    Andaikan x adalah node pada BST, bila y adalahnode pada sisi kiri x maka: key[y] key[x].

  • 8/13/2019 Analisis Algoritma_8x(1)

    4/29

    Membaca Elemen BST

    Untuk contoh BST diatas, elemen-nya adalah:

    2-3-5-5-7-8

  • 8/13/2019 Analisis Algoritma_8x(1)

    5/29

    Analisis

    Andaikan T(n) = waktu untuk akses n node

    setiap node andaikan perlu (c + d), dimana c

    waktu untuk membaca root, dan d waktu

    mencapai node, maka:

    T(n) = (c+d)*n + c

    Apabila disebelah kiri ada k node, makadisebelah kanan ada (n-k-1) node, sehingga

    algoritma IN-ORDER perlu:

  • 8/13/2019 Analisis Algoritma_8x(1)

    6/29

    Analisis

    T(n) = T(k) + T(n-k-1) + d

    T(n) = ((c+d)*k + c) + ((c+d)*(n-k-1) + c) + d

    T(n) = (c+d)*n + c

    (c+d) + c + d T(n) = (c+d)*n + c = (n)

  • 8/13/2019 Analisis Algoritma_8x(1)

    7/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    8/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    9/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    10/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    11/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    12/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    13/29

    TREE-SEARCH(X,13)1567 - 13

  • 8/13/2019 Analisis Algoritma_8x(1)

    14/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    15/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    16/29

  • 8/13/2019 Analisis Algoritma_8x(1)

    17/29

    Red-Black-Tree

    Red/Black Trees: These are binary trees with thefollowing properties.

    Every node has a value.

    The value of any node is greater than the value of its

    left child and less than the value of its right child. Every node is colored either red or black.

    Every red node that is not a leaf has only blackchildren.

    Every path from the root to a leaf contains the samenumber of black nodes.

    The root node is black.

  • 8/13/2019 Analisis Algoritma_8x(1)

    18/29

    An nnode red/black tree has the property that

    its height is O(lg(n)). Another importantproperty is that a node can be added to ared/black tree and, in O(lg(n))time, the treecan be readjusted to become a larger

    red/black tree. Similarly, a node can bedeleted from a red/black tree and, in O(lg(n))time, the tree can be readjusted to becomesmaller a red/black tree. Due to theseproperties, red/black trees are useful for datastorage.

  • 8/13/2019 Analisis Algoritma_8x(1)

    19/29

    RBT-Insertion Rules

    When a node is inserted in the tree it is given the colorred. This does not affect the black node count on anypath to a leaf. But it could lead to a single pair ofconsecutive red nodes in the tree. If the new node

    becomes a child of a black node there is no problem.But it may become a child of a red node. The doublered violation will begin at a leaf. The rules below aredesigned to move the double violation up toward theroot without affecting any path's black node count until

    it can be eliminated by bringing down a black fromabove or it reaches the root where it can be eliminatedsince the root can be colored black withoutconsequence.

  • 8/13/2019 Analisis Algoritma_8x(1)

    20/29

    Tinjau node 25, merah dan punya anak merah,

    melanggar aturan, dimana node merah yang

    bukan leaf (daun) hanya boleh memiliki anakwarna hitam. Harus di-reorganisasi

    Step-1 : ubah warna sebagai berikut

    Tapi node-30 melanggar aturan, masih

    perlu di-organisir

  • 8/13/2019 Analisis Algoritma_8x(1)

    21/29

    Andaikan posisi node-30 adalah seperti gambar diatas

  • 8/13/2019 Analisis Algoritma_8x(1)

    22/29

    : action: If current is the right child of its parent, rotate clockwise around current.

    Otherwise rotate counter-clockwise around current. Set current to the new parent of

    current (the pre-rotation red child of current). This satisfies the next rule and does not

    change any red/black tree violations. Continue to .

  • 8/13/2019 Analisis Algoritma_8x(1)

    23/29

    : action: Rotate in the direction that causes current to become the parent of its

    pre-rotation parent. All paths through current's pre-rotation red child now are short one

    black because a black node has been rotated out. There are still two reds in a row. But

    the black node count on paths through the pre-rotation parent are unchanged. Hence...

  • 8/13/2019 Analisis Algoritma_8x(1)

    24/29

    : action: Exchange colors between current and its pre-rotation parent. The black

    count through the pre-rotation parent remains unchanged. However, since current is

    made black, the double red is eliminated and all paths through the red child have

    increased their black node count by 1. Hence there are no red/black tree violations and

    the algorithm terminates here.

    : action: If current is the root or the root sentinel, the only thing left to do is set

    the root to black and exit. Otherwise, repeat.

  • 8/13/2019 Analisis Algoritma_8x(1)

    25/29

    RBT Deletion Rules

    Let currentrefer to the node whose parent and siblings arequeried in a deletion step. Initially currentis the nodeselected for deletion. Suppose it has two children. Thenanother node is selected for deletion instead: namely,either a successor of current (a leaf or a node with only a

    right child of next greater value), or a predecessor ofcurrent (a leaf or node with only a left child of next smallervalue). Either is easy to find and rules for doing so are in thenext section. The deleted successor or predecessor is saveduntil the run terminates and then it replaces the node

    originally selected to be deleted. If a successor orpredecessor is necessary, it becomes currentwhen it isfound. Due to the use of a successor, only nodes with atmost one child need to be considered for deletion.

  • 8/13/2019 Analisis Algoritma_8x(1)

    26/29

    Andaikan situasinya sebagai berikut, node-46

    merah akan di-hapus, sehingga menjadi:

  • 8/13/2019 Analisis Algoritma_8x(1)

    27/29

    [1]: action: If a successor or predecessor is used, replace the node selected for

    deletion with the leaf. Otherwise just delete the leaf. The definition of red/black trees

    is not violated.

    [2]: action: This is impossible since the child must be black to avoid two reds in a

    row on a path but then the number of blacks on paths through the left side of the

    node is different from the number of blacks on paths through the right side.

    Current is red with one child.

  • 8/13/2019 Analisis Algoritma_8x(1)

    28/29

    [3]: action: This is impossible since the number of blacks on paths through the left

    side of the node is different from the number of blacks on paths through the right

    side.

    Current is black with one black child.

    Current is black with one red child.

    Current (to be deleted) is node number 32 on the left.

    Result is shown on the right.

  • 8/13/2019 Analisis Algoritma_8x(1)

    29/29

    [4]: action: The child must be a leaf, otherwise the definition of red/black trees is

    violated. Unhook current from the tree and make current's child a child of current's

    former parent (there is one non-violating way to do it). If current is a successor or

    predecessor, it replaces the node selected for deletion. Otherwise current is deleted.

    There is no violation of the definition of red/black trees since the only change is that a

    red leaf is deleted.

    [5]: This step may be reached immediately or through Step [5.1.2]or [5.2.1]. If

    immediately, current is either the node selected for deletion, or the successor node,

    or the predecessor node. In all three cases unhook it from the tree (that is, replace

    it in the tree with a sentinel), but retain the notion of parent, sibling and such as

    though it is still in the tree because it may be needed in one or more steps below. If

    current is the root, just delete it and terminate.

    Current is black and has no children.