1
20-Apr-04 17 - 1
©2004 PM - IKI10100
BinarySearchTree BinarySearchTree
UTS 2004
root left
BinaryNode right 55 element
left
BinaryNode right 70 element
left BinaryNode
right 12 element
left
BinaryNode right 24 element BinarySearchTree
20-Apr-04 17 - 2
©2004 PM - IKI10100
Sorting Sorting
Algoritma comp swap comp swap comp swap Bubble Sort 4/10 0 9/10 4 10 10 Selection Sort 10 0/4 10 4 10 4 Insertion Sort 4 0 7 4 10 10
Pre-Sorted Random Reverse-Sorted [2 5 8 13 17] [5 13 2 17 8] [17 13 8 5 2]
UTS 2004
20-Apr-04 17 - 3
©2004 PM - IKI10100
Rectangular Numbers Rectangular Numbers
UTS 2004
Bilg ke- 1
2
3
4
Gambar f (n) 1
5
13
25
4
8
12
• utk n = 1 f(n) = 1
• utk n > 1
f(n) = f(n-1) + 4(n-1)
20-Apr-04 17 - 4
©2004 PM - IKI10100
Rectangular Numbers Rectangular Numbers
int RectangularNumber ( int n ) { if (n == 1) return 1;
else
return RectangularNumber (n – 1) + 4*(n-1);
} UTS 2004
2
20-Apr-04 17 - 5
©2004 PM - IKI10100
Rectangular Numbers Rectangular Numbers
Yang harus dibuktikan:
UTS 2004
∑
=+
−
=
− +
ni
n n
i
1
2
2 1
2 ) 1 ( 4 1
20-Apr-04 17 - 6
©2004 PM - IKI10100
BST: Recursive find Method BST: Recursive find Method
BinaryNode find (int x, BinaryNode t) { if (x < t.element) return find(x, t.left);
else if (x > t.element) return find(x, t.right);
else if (x == t.element) return t;
else return null;
} UTS 2004
20-Apr-04 17 - 7
©2004 PM - IKI10100
BST: Recursive find Method BST: Recursive find Method
BinaryNode find (int x, BinaryNode t) { if (t == null) throw new …..;
else if (x < t.element) return find(x, t.left);
else if (x > t.element) return find(x, t.right);
else return t;
}
Kesalahan Umum
20-Apr-04 17 - 8
©2004 PM - IKI10100
BST:
BST: oneChild oneChild Method Method
int oneChild ( ) { return oneChild (root); } int oneChild (BinaryNode t) {
if (t == null) return 0;
if (t.left != null && t.right == null)
return 1 + oneChild(t.left);
else if (t.left == null && t.right != null) return 1 + oneChild(t.right);
else
return oneChild(t.left) + oneChild(t.right);
} UTS 2004
3
20-Apr-04 17 - 9
©2004 PM - IKI10100
BST:
BST: oneChild oneChild Method Method
int oneChild ( ) { return oneChild (root); } int oneChild (BinaryNode t) {
if (t == null) return –1;
if (t.left != null && t.right != null)
return oneChild(t.left) + oneChild(t.right);
else
return 2 + oneChild(t.left) + oneChild(t.right);
} UTS 2004
20-Apr-04 17 - 10
©2004 PM - IKI10100
BST:
BST: oneChild oneChild Method Method
int oneChild ( ) { return oneChild (root); } int oneChild (BinaryNode t) {
if (t == null) return 0;
else if (t.left != null && t.right == null) return 1;
else if (t.left == null && t.right != null) return 1;
else
return oneChild(t.left) + oneChild(t.right);
}
Kesalahan Umum