14 binary search tree v1
TRANSCRIPT
1
Struktur Data & Algoritme(Data Structures & Algorithms)
Denny ([email protected])Suryana Setiawan ([email protected])
Fakultas Ilmu KomputerUniversitas Indonesia
Semester Genap - 2004/2005Version 2.0 - Internal Use Only
Binary Search Tree
SDA/TOPIC/V2.0/2
ObjectivesMemahami sifat dari Binary Search Tree (BST)Memahami operasi-operasi pada BSTMemahami kelebihan dan kekurangan dari BST
SDA/TOPIC/V2.0/3
OutlineProperties of Binary Search Tree (BST)Operation
Insertfindremove
SDA/TOPIC/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 arelarger than the key in X.So, the key should be comparable.
X
<X >X
2
SDA/TOPIC/V2.0/5
7
2
3
9
1 5
6
Binary Search Tree
SDA/TOPIC/V2.0/6
3
2
1
3
1
2
2
1 3
1
3
2
1
2
3
Binary Search Tree
SDA/TOPIC/V2.0/7
Basic Operations insertfindMin and findMaxremove
SDA/TOPIC/V2.0/8
InsertionPenyisipan sebuah elemen baru dalam binary search tree, elemen tersebut pasti akan menjadi leaf
10
2
3
15
1 5
6
12
14
3
SDA/TOPIC/V2.0/9
Insertion: algorithmInsert 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/TOPIC/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/TOPIC/V2.0/11
FindMinMencari 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/TOPIC/V2.0/12
FindMaxMencari node yang memiliki nilai terbesarAlgorithm?Code?
4
SDA/TOPIC/V2.0/13
FindDiberikan sebuah nilai yang harus dicari dalamsebuah BST. Jika ada elemen tersebut, return node tersebut. Jika tidak ada, return null.Algorithm?Code? 7
2
3
9
1 5
6SDA/TOPIC/V2.0/14
8
4
5
12
1 6
3
Remove
5
6
4
SDA/TOPIC/V2.0/15
Remove if the node is a leaf (has no child), no problemo…delete it immediatelyif 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, orreplace 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/TOPIC/V2.0/16
Removing 6
8
4
5
12
1 6
3
5
SDA/TOPIC/V2.0/17
After 6 removed
8
4
5
12
1 6
3
SDA/TOPIC/V2.0/18
removeMinBinaryNode removeMin(BinaryNode t) {
if (t == null) throw exception;
if (t.left != null) {t.left = removeMin (t.left);
} else {t = t.right;
}return t;
}
SDA/TOPIC/V2.0/19
removeMaxcode?
SDA/TOPIC/V2.0/20
7
2
3
9
1 5
4
Removing 2
6
SDA/TOPIC/V2.0/21
After 2 deleted
7
2
3
9
1 5
4
23
X
SDA/TOPIC/V2.0/22
Removing Root
7
2
3
12
1 5
4
10 14
9 11
9
SDA/TOPIC/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/TOPIC/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
7
SDA/TOPIC/V2.0/25
Find k-th elementBinaryNode 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/TOPIC/V2.0/26
AnalysisRunnning time for:
insert?Find min?remove?Find?
Worst case: O(n)
SDA/TOPIC/V2.0/27
SummaryBinary Search Tree maintains the order of the tree.Each node should be comparableAll 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/TOPIC/V2.0/28
Further Readinghttp://telaga.cs.ui.ac.id/WebKuliah/IKI10100/1998/handout/handout16.html
Chapter 18
8
SDA/TOPIC/V2.0/29
What’s NextAVL tree