Tuesday, 31 May 2016

Heap, Tries, and Hashing

Heap 


heap merupakan sebuah complete binary tree dimana 1 parent memiliki 2 anak.
ada 3 jenis heap, yaitu: min heap, max heap, dan min-max heap

Max: Root memiliki nilai terbesar
Min: Root memiliki nilai terkeceil
Min-Max: berganti gantian dari nilai terkecil dan terbesar

insert:

buildheap.png

delete:

img295.gif

Tries

tries adalah salah satu data structure yang digunakan dalam search engine dan berfungsi untuk auto correct atau auto spelling.

 i1LRz.png

Hash

berguna uantuk mencari nilai tengah dan berbagai operasi lainnya yang membutuhkan tabel hash dan merupakan salah satu cara untuk melakukan ekripsi.

ada 3 cara melakukan hashing

1.Division
340hash3.gif

2. Mid

slide_19.jpg

3. Folding

slide_4.jpg

Tuesday, 24 May 2016

RedBlack Tree & 2-3 Tree

Red Black Tree

apa itu redblack tree? red black tree adalah suatu jenis tree yang dapat menyeimbangkan dirinya sendiri.. WOOW!?! gimana caranya??

berikut penjelasannya

Keseimbangannya dijaga dengan cara mewarnai masing masing node dari tree tersebut dengan salah satu dari dua warna (red dan black) yang melalui cara pewarnaan tersebut dapat dengan mudah menyeimbangkan kembali tree tersebut, berikut contohcontohnya

Insertion:




Deletion

seperti insertion, kita juga akan merotasi dan mengganti warna
kita cek warna sodara untuk proses ini dan menentukan case mana yang sesuai.
The main property that violates after insertion is two consecutive reds. In delete, the main violated property is, change of black height in subtrees as deletion of a black node may cause reduced black height in one root to leaf path.
Deletion is fairly complex process.  To understand deletion, notion of double black is used.  When a black node is deleted and replaced by a black child, the child is marked as double black. The main task now becomes to convert this double black to single black.
Deletion Steps
Following are detailed steps for deletion.
1) Perform standard BST delete. When we perform standard delete operation in BST, we always end up deleting a node which is either leaf or has only one child (For an internal node, we copy the successor and then recursively call delete for successor, successor is always a leaf node or a node with one child). So we only need to handle cases where a node is leaf or has one child. Let v be the node to be deleted and u be the child that replaces v (Note that u is NULL when v is a leaf and color of NULL is considered as Black).
2) Simple Case: If either u or v is red, we mark the replaced child as black (No change in black height). Note that both u and v cannot be red as v is parent of u and two consecutive reds are not allowed in red-black tree.
rbdelete11
3) If Both u and v are Black.
3.1) Color u as double black.  Now our task reduces to convert this double black to single black. Note that If v is leaf, then u is NULL and color of NULL is considered as black. So the deletion of a black leaf also causes a double black.
rbdelete12_new
3.2) Do following while the current node u is double black or it is not root. Let sibling of node be s.
….(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s). Let the red child of s be r. This case can be divided in four subcases depending upon positions of s and r.
…………..(i) Left Left Case (s is left child of its parent and r is left child of s or both children of s are red). This is mirror of right right case shown in below diagram.
…………..(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of right left case shown in below diagram.
…………..(iii) Right Right Case (s is right child of its parent and r is right child of s or both children of s are red)
rbdelete13New
…………..(iv) Right Left Case (s is right child of its parent and r is left child of s)
rbdelete14
…..(b): If sibling is black and its both children are black, perform recoloring, and recur for the parent if parent is black.
rbdelete15
In this case, if parent was red, then we didn’t need to recur for prent, we can simply make it black (red + double black = single black)
…..(c): If sibling is red, perform a rotation to move old sibling up, recolor the old sibling and parent. The new sibling is always black (See the below diagram). This mainly converts the tree to black sibling case (by rotation) and  leads to case (a) or (b). This case can be divided in two subcases.
…………..(i) Left Case (s is left child of its parent). This is mirror of right right case shown in below diagram. We right rotate the parent p.
…………..(iii) Right Case (s is right child of its parent). We left rotate the parent p.
rbdelete16
3.3) If u is root, make it single black and return (Black height of complete tree reduces by 1).


2-3 Tree

wah apalagi ni 2-3 tree? 2-3tree adalah tree yang bisa nampung sampe 3 anak wiidh, berikut operasinya

Insertion:


Deletion: 


Tuesday, 17 May 2016

AVL Tree

Pada pertemuan kali ini, saya akan membahas... AVL TREE.

Apa itu AVL tree?

Avl tree adalah salah satu jenis binary tree yang memiliki maksimal 1 level perbedaan antara substreenya atu disebut balanced condition sehingga memudahkan pencarian.

Berikut contoh gambar AVL tree:



Terdapat dua cara menyeimbangkan AVL tree yaitu dengan single rotation dan double rotation:

Single:



Double: