Struktur Data & Algoritme (Data Structures & Algorithms ) Binary Search Tree
Denny
[email protected] Fakultas Ilmu Komputer Universitas Indonesia Semester Genap - 2000/2001
Version 1.0 - Internal Use Only
Objectives n n n
Memahami sifat dari Binary Search Tree (BST) Memahami operasi-operasi pada BST Memahami kelebihan dan kekurangan dari BST
SDA/BST/DN/V1.0/2
Fundamental Data Types: String, Math, Casting
1
Outline n n
Properties of Binary Search Tree (BST) Operation n Insert n find n remove
SDA/BST/DN/V1.0/3
Properties of Binary Search Tree n
n
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 SDA/BST/DN/V1.0/4
Fundamental Data Types: String, Math, Casting
2
Binary Search Tree 7 9
2 1
5
3
6
SDA/BST/DN/V1.0/5
Binary Search Tree 3 2 1
3
1
1 2
1
3 3
2
2 2 1
3 SDA/BST/DN/V1.0/6
Fundamental Data Types: String, Math, Casting
3
Basic Operations n n n
insert findMin and findMax remove
SDA/BST/DN/V1.0/7
Insertion n
Penyisipan sebuah elemen baru dalam binary search tree, elemen tersebut pasti akan menjadi leaf
10 15
2 1
12
5
3
6
14 SDA/BST/DN/V1.0/8
Fundamental Data Types: String, Math, Casting
4
Insertion: algorithm n
n
n n
Insert X into a binary search tree: n start from the root. n If the value of X is less than the value of the root, then X should be inserted on the left sub-tree. n 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/BST/DN/V1.0/9
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/BST/DN/V1.0/10
Fundamental Data Types: String, Math, Casting
5
FindMin n n
n
Mencari node yang memiliki nilai terkecil. Algorithm: n 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/BST/DN/V1.0/11
FindMax n n n
Mencari node yang memiliki nilai terbesar Algorithm? Code?
SDA/BST/DN/V1.0/12
Fundamental Data Types: String, Math, Casting
6
Find n
n n
Diberikan sebuah nilai yang harus dicari dalam sebuah BST. Jika ada elemen tersebut, return node tersebut. Jika tidak ada, return null. Algorithm? Code? 7
9
2 1
5
3
6 SDA/BST/DN/V1.0/13
Remove 8 12
4 6
1 3
5
SDA/BST/DN/V1.0/14
Fundamental Data Types: String, Math, Casting
7
Remove n n n
n
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? n replace the item in this node with the smallest item in the right subtree and then remove that node, or n replace the item in this node with the biggest item in the left subtree and then remove that node intoduce new sub-problems: n removeMin, removeMax
SDA/BST/DN/V1.0/15
Removing 6 8 12
4 6
1 3
5
SDA/BST/DN/V1.0/16
Fundamental Data Types: String, Math, Casting
8
After 6 removed 8 12
4 6
1 3
5
SDA/BST/DN/V1.0/17
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/BST/DN/V1.0/18
Fundamental Data Types: String, Math, Casting
9
removeMax n
code?
SDA/BST/DN/V1.0/19
Removing 2 7 9
2 1
5 3 4
SDA/BST/DN/V1.0/20
Fundamental Data Types: String, Math, Casting
10
After 2 deleted 7
2
9
3 2
1
5 4 3
4
SDA/BST/DN/V1.0/21
Removing Root 7 9 12
2 1
10
5 3
9
14 11
4
SDA/BST/DN/V1.0/22
Fundamental Data Types: String, Math, Casting
11
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/BST/DN/V1.0/23
Find k-th element
X
X
SR SL
k < SL + 1
X
SR SL
k == S L + 1
SR SL
k > SL + 1 SDA/BST/DN/V1.0/24
Fundamental Data Types: String, Math, Casting
12
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