Presentation is loading. Please wait.

Presentation is loading. Please wait.

IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.

Similar presentations


Presentation on theme: "IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data."— Presentation transcript:

1 IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Binary Search Tree

2 2 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Outline Concept of Binary Search Tree (BST) BST operations Find Insert Remove Running time analysis of BST operations

3 3 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Binary Search Tree: Properties Elements 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. X X

4 4 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Binary Search Tree: Examples

5 5 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Binary Search Tree: Examples

6 6 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Basic Operations FindMin, FindMax, Find Insert Remove

7 7 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 BinaryNode findMin(BinaryNode t) { if (t != null) while (t.left != null) t = t.left; return t; } BinaryNode findMin(BinaryNode t) { if (t != null) while (t.left != null) t = t.left; return t; } FindMin Find node with the smallest value Algorithm: Keep going left until you reach a dead end! Code:

8 8 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 FindMax Find node with the largest value Algorithm: Keep going right until you reach a dead end! Code: BinaryNode findMax(BinaryNode t) { if (t != null) while (t.right != null) t = t.right; return t; } BinaryNode findMax(BinaryNode t) { if (t != null) while (t.right != null) t = t.right; return t; }

9 9 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Find You are given an element to find in a BST. If it exists, return the node. If not, return null. Algorithm? Code?

10 10 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Find: Implementation BinaryNode find(Type x, BinaryNode 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 } BinaryNode find(Type x, BinaryNode 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 }

11 11 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Insertion: Principle When inserting a new element into a binary search tree, it will always become a leaf node

12 12 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Insertion: Algorithm To 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!

13 13 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Insertion: Implementation BinaryNode insert(Type x, BinaryNode t) { if (t == null) t = new BinaryNode (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; } BinaryNode insert(Type x, BinaryNode t) { if (t == null) t = new BinaryNode (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; }

14 14 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing An Element

15 15 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing An Element: Algorithm If 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

16 16 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Leaf

17 17 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Node With 1 Child

18 18 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Node With 1 Child

19 19 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 removeMin BinaryNode removeMin(BinaryNode t) { if (t == null) throw new ItemNotFoundException(); else if (t.left != null) { t.left = removeMin(t.left); return t; } else return t.right; } BinaryNode removeMin(BinaryNode t) { if (t == null) throw new ItemNotFoundException(); else if (t.left != null) { t.left = removeMin(t.left); return t; } else return t.right; }

20 20 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Node With 2 Children

21 21 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Node With 2 Children

22 22 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Node With 2 Children

23 23 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Removing Root

24 24 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Remove BinaryNode remove(Type x, BinaryNode 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; } BinaryNode remove(Type x, BinaryNode 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; }

25 25 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Find k-th element SLSL SRSR X k < S L + 1 SLSL SRSR X k == S L + 1 SLSL SRSR X k > S L + 1

26 26 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 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); } 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); }

27 27 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 Analysis Running time for: Insert? Find min? Remove? Find? Average case: O(log n) Worst case: O(n)

28 28 Ruli Manurung (Fasilkom UI)IKI10100I: Data Structures & Algorithms Week 10 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 is equally balanced. All operations will take O(n) - worst case, when the height of the tree equals the number of nodes.


Download ppt "IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data."

Similar presentations


Ads by Google