Objectives Memahami sifat dari Binary Search Tree (BST) Struktur - - PowerPoint PPT Presentation

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Memahami sifat dari Binary Search Tree (BST) Struktur - - PowerPoint PPT Presentation

Objectives Memahami sifat dari Binary Search Tree (BST) Struktur Data & Algoritme Memahami operasi-operasi pada BST Memahami kelebihan dan kekurangan dari BST ( Data Structures & Algorithms ) Binary Search Tree Denny (


slide-1
SLIDE 1

1

Struktur Data & Algoritme ( Data Structures & Algorithms)

Denny (denny@cs.ui.ac.id) Suryana Setiawan (setiawan@cs.ui.ac.id)

Fakultas I lm u Kom puter Universitas I ndonesia Sem ester Genap - 2 0 0 4 / 2 0 0 5

Version 2 .0 - I nternal Use Only

Binary Search Tree

SDA/ TOPI C/ V2.0/ 2

Objectives

Memahami sifat dari Binary Search Tree (BST) Memahami operasi-operasi pada BST Memahami kelebihan dan kekurangan dari BST

SDA/ TOPI C/ V2.0/ 3

Outline

Properties of Binary Search Tree (BST) Operation Insert find remove

SDA/ TOPI C/ V2.0/ 4

Properties of Binary Search Tree

For every node X in the tree, the values of all the keys

in the left subtree are smaller than the key in X and the values of all the keys in the right subtree are larger than the key in X.

So, the key should be comparable.

X <X >X

slide-2
SLIDE 2

2

SDA/ TOPI C/ V2.0/ 5

7 2 3 9 1 5 6

Binary Search Tree

SDA/ TOPI C/ V2.0/ 6

3 2 1 3 1 2 2 1 3 1 3 2 1 2 3

Binary Search Tree

SDA/ TOPI C/ V2.0/ 7

Basic Operations

insert findMin and findMax remove

SDA/ TOPI C/ V2.0/ 8

Insertion

Penyisipan sebuah elemen baru dalam binary search

tree, elemen tersebut pasti akan menjadi leaf

10 2 3 15 1 5 6 12 14

slide-3
SLIDE 3

3

SDA/ TOPI C/ V2.0/ 9

Insertion: algorithm

Insert X into a binary search tree: start from the root. If the value of X is less than the value of the root, then

X should be inserted on the left sub-tree.

On the other hand, if the value of X is greater than the

value of the root, then X should be inserted on the right sub-tree.

Remember that, a sub tree is also a tree. So, the

problem to insert an element in the sub-tree is same as the problem to insert an element in the root.

So? We can attack this problem with recursive approach.

SDA/ TOPI C/ V2.0/ 10

Insertion

BinaryNode insert(int x, BinaryNode t) { if (t == null) { t = new BinaryNode (x, null, null); } else if (x < t.element) { t.left = insert (x, t.left); } else if (x > t.element) { t.right = insert (x, t.right); } else { throw new DuplicateItem(“exception”); } return t; }

SDA/ TOPI C/ V2.0/ 11

FindMin

Mencari node yang memiliki nilai terkecil. Algorithm: ke kiri terus sampai buntu….:) Code:

BinaryNode findMin (BinaryNode t) { if (t == null) throw exception; while (t.left != null) { t = t.left; } return t; }

SDA/ TOPI C/ V2.0/ 12

FindMax

Mencari node yang memiliki nilai terbesar Algorithm? Code?

slide-4
SLIDE 4

4

SDA/ TOPI C/ V2.0/ 13

Find

Diberikan sebuah nilai yang harus dicari dalam

sebuah BST. Jika ada elemen tersebut, return node

  • tersebut. Jika tidak ada, return null.

Algorithm? Code?

7 2 3 9 1 5 6

SDA/ TOPI C/ V2.0/ 14

8 4 5 12 1 6

3

Remove

5 6 4

SDA/ TOPI C/ V2.0/ 15

Remove

if the node is a leaf (has no child), no problemo…

delete it immediately

if the node has one child: its parent adjusts a child

reference to bypass the node.

if the node has two children? replace the item in this node with the smallest item in

the right subtree and then remove that node, or

replace the item in this node with the biggest item in

the left subtree and then remove that node

introduce new sub-problems: removeMin, removeMax

SDA/ TOPI C/ V2.0/ 16

Removing 6

8 4 5 12 1 6

3

slide-5
SLIDE 5

5

SDA/ TOPI C/ V2.0/ 17

After 6 removed

8 4 5 12 1 6

3

SDA/ TOPI C/ V2.0/ 18

removeMin

BinaryNode removeMin(BinaryNode t) { if (t == null) throw exception; if (t.left != null) { t.left = removeMin (t.left); } else { t = t.right; } return t; }

SDA/ TOPI C/ V2.0/ 19

removeMax

code?

SDA/ TOPI C/ V2.0/ 20

7 2 3 9 1 5 4

Removing 2

slide-6
SLIDE 6

6

SDA/ TOPI C/ V2.0/ 21

After 2 deleted

7 2 3 9 1 5 4 2

3

X

SDA/ TOPI C/ V2.0/ 22

Removing Root

7 2 3 12 1 5 4 10 14 9 11 9

SDA/ TOPI C/ V2.0/ 23

Remove

BinaryNode remove(int x, BinaryNode t) { if (t == null) throw exception; if (x < t.element) { t.left = remove(x, t.left); } else if (x > t.element) { t.right = remove(x, t.right); } else if (t.left != null && t.right != null) { t.element = findMin(t.right).element; t.right = removeMin(t.right); } else { t = (t.left != null) ? t.left : t.right; } return t; }

SDA/ TOPI C/ V2.0/ 24

SL SR X k < SL + 1 SL SR X k == SL + 1 SL SR X k > SL + 1

Find k-th element

slide-7
SLIDE 7

7

SDA/ TOPI C/ V2.0/ 25

Find k-th element

BinaryNode findKth(int k, BinaryNode t) { if (t == null) throw exception; int leftSize = (t.left != null) ? t.left.size : 0; if (k <= leftSize ) { return findKth (k, t.left); } else if (k == leftSize + 1) { return t; } else { return findKth ( k - leftSize - 1, t.right); } }

SDA/ TOPI C/ V2.0/ 26

Analysis

Runnning time for: insert? Find min? remove? Find? Worst case: O(n)

SDA/ TOPI C/ V2.0/ 27

Summary

Binary Search Tree maintains the order of the tree. Each node should be comparable All operations take O(log n) - average case, when the

tree equally balanced.

All operations will take O(n) - worst case, when the all

the height of the tree equals with the total of the nodes.

SDA/ TOPI C/ V2.0/ 28

Further Reading

http://telaga.cs.ui.ac.id/WebKuliah/IKI101

00/1998/handout/handout16.html

Chapter 18

slide-8
SLIDE 8

8

SDA/ TOPI C/ V2.0/ 29

What’s Next

AVL tree