Upload
lee-burroughs
View
215
Download
0
Embed Size (px)
Citation preview
OutlineOutlineConcept of Binary Search Tree (BST)BST operations
◦Find◦Insert◦Remove
Running time analysis of BST operations
Smt Genap 2011-2012
Binary Search Tree: Binary Search Tree: PropertiesPropertiesElements have keys (no duplicates allowed).
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.
The keys must be comparable.
Smt Genap 2011-2012
X
<X >X
Binary Search Tree: Binary Search Tree: ExamplesExamples
Smt Genap 2011-2012
3
2
1
3
1
2
2
1 3
1
3
2
1
2
3
FindMinFindMinFind node with the smallest valueAlgorithm:
◦Keep going left until you reach a dead end!
Code:BinaryNode<Type> findMin(BinaryNode<Type> t) { if (t != null) while (t.left != null) t = t.left;
return t;}
Smt Genap 2011-2012
FindMaxFindMaxFind node with the largest valueAlgorithm:
◦Keep going right until you reach a dead end!
Code:BinaryNode<Type> findMax(BinaryNode<Type> t) { if (t != null) while (t.right != null) t = t.right;
return t;}
Smt Genap 2011-2012
FindFindYou are given an element to find in a BST. If it exists, return the node. If not, return null.
Algorithm?Code?
Smt Genap 2011-2012
7
2
3
9
1 5
6
Find: ImplementationFind: ImplementationBinaryNode<Type> find(Type x, BinaryNode<T> t) { while(t!=null) { if(x.compareTo(t.element)<0) t = t.left; else if(x.compareTo(t.element)>0) t = t.right; else return t; // Match }
return null; // Not found}
Smt Genap 2011-2012
Insertion: PrincipleInsertion: PrincipleWhen inserting a new element into a binary
search tree, it will always become a leaf node.
Smt Genap 2011-2012
10
2
3
15
1 5
6
12
14
Insertion: AlgorithmInsertion: AlgorithmTo insert X into a binary search tree:◦Start from the root◦ If the value of X < the value of the root:
X should be inserted in the left sub-tree. ◦ If the value of X > the value of the root:
X should be inserted in the right sub-tree.Remember that a sub-tree is also a tree.We can implement this recursively!
Smt Genap 2011-2012
Insertion: ImplementationInsertion: ImplementationBinaryNode<Type> insert(Type x, BinaryNode<Type> t) { if (t == null) t = new BinaryNode<Type>(x); else if(x.compareTo(t.element)<0) t.left = insert (x, t.left); else if(x.compareTo(t.element)>0) t.right = insert (x, t.right); else throw new DuplicateItemException(x);
return t;}
Smt Genap 2011-2012
Removing An Element: Removing An Element: AlgorithmAlgorithmIf the node is a leaf, simply delete it.If the node has one child, adjust parent’s
child reference to bypass the node.If the node has two children:
◦ Replace the node’s element with the smallest element in the right subtree and then remove that node, or
◦ Replace the node’s element with the largest element in the left subtree and then remove that node
Introduces new sub-problems: ◦ removeMin:◦ Alternatively, removeMax
Smt Genap 2011-2012
removeMin removeMin BinaryNode<Type> removeMin(BinaryNode<Type> t) { if (t == null) throw new ItemNotFoundException(); else if (t.left != null) {
t.left = removeMin(t.left); return t; } else return t.right;}
Smt Genap 2011-2012
Remove Remove BinaryNode<Type> remove(Type x, BinaryNode<Type> t){ if (t == null) throw new ItemNotFoundException(); if (x.compareTo(t.element)<0) t.left = remove(x, t.left); else if(x.compareTo(t.element)>0) 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 { if(t.left!=null) t=t.left; else t=t.right; } return t;}
Smt Genap 2011-2012
Find Find kk-th element-th element
Smt Genap 2011-2012
SL
SR
X
k < SL + 1
SL
SR
X
k == SL + 1
SL
SR
X
k > SL + 1
Find Find kk-th element-th elementBinaryNode<Type> findKth(int k, BinaryNode<Type> 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); }
Smt Genap 2011-2012
AnalysisAnalysisRunning time for:
◦Insert?◦Find min?◦Remove?◦Find?
Average case: O(log n)Worst case: O(n)
Smt Genap 2011-2012