Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf ·...

38
Binary Search Tree Speaker 邱聖元

Transcript of Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf ·...

Page 1: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Binary Search Tree

Speaker 邱聖元

Page 2: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Outline

IntroductionOperations Conclusion

Page 3: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Introduction

What’s a binary search tree?– It’s a binary tree !– For each node in a BST, the left subtree is

smaller than it; and the right subtree isgreater than it.

Page 4: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Introduction

This is a simple BST

10

7

96

13

12 14

Page 5: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Introduction

Is this a BST ?

25

19

228

27

24 31

NO! Because 24 isless than 25

Page 6: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Introduction

Node structure

RightLeft

Key

Parent

Page 7: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operations

There are 3 common operations– QUERY– INSERT– DELETE

Page 8: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Query

The QUERY operation can be further spitinto– Search– Max/Min– Successor/Predecessor

Page 9: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Search

Search(T,k)– search the BST T for a value k

Page 10: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Search

10

7

96

13

12 14

K=12

?

greatersmaller

Page 11: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Search

10

7

96

13

12 14

K=12

?

greatersmaller

Page 12: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Search

10

7

96

13

12 14

K=12

Page 13: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Search

Search operation takes time O(h), where his the height of a BST

Page 14: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation –Min/Max

For Min, we simply follow the left pointeruntil we find a null node

Why? Because if it’s not the minimum node,then the real min node must reside atsome node’s right subtree. By the propertyof BST, it’s a contradiction

Similar for Max Time complexity: O(h)

Page 15: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation Predecessor/Successor

Successor(x)– If we sort all elements in a BST to a sequence,

return the element just after x– Time complexity: O(h)

Page 16: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation Predecessor/Successor

Find Successor

if Right(x) exists, // Step 1

then return Min( Right(x) ) ;else // Step 2

Find the first ancestor of x whose leftsubtree contains x ;

Page 17: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation Predecessor/Successor

Step1: X

Min

Page 18: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation Predecessor/Successor

Finding theancestor whoseleft subtreecontains X

X

Step2:

Page 19: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Insert

Insert(T,z)– Insert a node with KEY=z into BST T– Time complexity: O(h)

Page 20: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Insert

Step1: if the tree is empty,then Root(T)=z

Step2: Pretending we are searching for zin BST T, until we meet a null node

Step3: Insert z

Page 21: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Insert

The light nodesare comparedwith k

30

17 52

2412

20 26

44 55

26

Page 22: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Delete(T,z)– Delete a node with key=z from BST T– Time complexity: O(h)

Page 23: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Case 1: z has no child

55

32

25 48

57

65

Page 24: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

55

32

25 48

57

65

We can simplyremove it fromthe tree

Page 25: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Case 2: z has one child

55

32

25 48

57

65

58 78

Page 26: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

55

32

25 48

57

65

After removingit, connect it’ssubtree to it’sparent node

58 78

Page 27: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Case 3: z has two child

55

32

25 48

61

6559

42

46

z

Page 28: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

55

32

25 48

61

6559

42successor

Find it’s successor

46

z

Page 29: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

55

32

25 48

61

6559

Pull out successor,and connect thetree with it’s child

46

42

What if successorhas two children?

z

successor

Page 30: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

What if the successor has two nodes?Not possible ! Because if it has two nodes,

at least one of them is less than it, then inthe process of finding successor, we won'tpick it !

Page 31: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

55

32

25 48

61

6559

42

Replace the keywith it’s successor

46

zsuccessor

Page 32: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Page 33: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

Page 34: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Operation - Delete

The original sequence is:

After deleting 32

25 32 42 46 48 55 59 61 65

25 42 46 48 55 59 61 65

Page 35: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Conclusion

If we have a sorted sequence, and we wantto design a data structure for it

Array? Or BST?

Page 36: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Conclusion

The Search time:

O(log n)Array

O(h)BST

Page 37: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Conclusion

We already know that n is fixed, but hdiffers from how we insert thoseelements !

n

A skew binarysearch tree

Page 38: Binary Search Tree - National Tsing Hua Universitywkhon/algo08-tutorials/tutorial-bst.pdf · 2008-04-10 · Introduction What’s a binary search tree? –It’s a binary tree ! –For

Conclusion

So why we still need BST?

Easier insertion/deletionAnd with some optimization, we can avoid

the worst case !