Top Banner
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20 th Mar 2007 Binary Search Tree
28

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

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

IKI 10100: Data Structures & Algorithms

Ruli Manurung(acknowledgments to Denny & Ade Azurat)

1

Fasilkom UI

Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Binary Search Tree

Page 2: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

2Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Outline

Concept of Binary Search Tree (BST)

BST operations Find Insert Remove

Running time analysis of BST operations

Page 3: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

3Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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 >X

Page 4: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

4Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

7

2

3

9

1 5

6

Binary Search Tree: Examples

Page 5: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

5Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

3

2

1

3

1

2

2

1 3

1

3

2

1

2

3

Binary Search Tree: Examples

Page 6: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

6Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Basic Operations

FindMin, FindMax, Find

Insert

Remove

Page 7: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

7Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

FindMin

Find node with the smallest value

Algorithm: 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;}

Page 8: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

8Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

FindMax

Find node with the largest value

Algorithm: 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;}

Page 9: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

9Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Find

You are given an element to find in a BST. If it exists, return the node. If not, return null.

Algorithm?

Code?7

2

3

9

1 5

6

Page 10: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

10Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Find: Implementation

BinaryNode<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

}

Page 11: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

11Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Insertion: Principle

When inserting a new element into a binary search tree, it will always become a leaf node.

10

2

3

15

1 5

6

12

14

Page 12: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

12Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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!

Page 13: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

13Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Insertion: Implementation

BinaryNode<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;}

Page 14: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

14Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

8

4

5

12

1 6

3

Removing An Element

5

6

4

Page 15: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

15Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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

Page 16: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

16Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Removing Leaf

8

4

5

12

1 6

3

Page 17: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

17Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Removing Node With 1 Child

8

4

5

12

1 6

3

Page 18: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

18Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Removing Node With 1 Child

8

4

5

12

1 6

3

Page 19: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

19Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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;

}

Page 20: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

20Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

7

2

3

9

1 5

4

Removing Node With 2 Children

Page 21: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

21Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

7

3

3

9

1 5

4

Removing Node With 2 Children

2

3

Page 22: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

22Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Removing Node With 2 Children

7

2

3

9

1 5

4

2

3

Page 23: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

23Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Removing Root

7

2

3

12

1 5

4

10 14

9 11

9

Page 24: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

24Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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;}

Page 25: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

25Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

SL

SR

X

k < SL + 1

SL

SR

X

k == SL + 1

SL

SR

X

k > SL + 1

Find k-th element

Page 26: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

26Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Find k-th element

BinaryNode<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);

}

Page 27: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

27Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

Analysis

Running time for: Insert? Find min? Remove? Find?

Average case: O(log n)

Worst case: O(n)

Page 28: IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture20.

28Ruli Manurung (Fasilkom UI) IKI10100: Lecture 20th Mar 2007

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.

Summary