dictionary adt - university of toronto263/jessica/lecture3.pdf · dictionary adt what’s stored a...

72
Dictionary ADT Week 3 CSC263 | Jessica Burgner-Kahrs 1 CSC263 Winter 2020

Upload: others

Post on 01-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Dictionary ADTWeek 3

CSC263 | Jessica Burgner-Kahrs1

CSC263 Winter 2020

Page 2: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Today and next two weeks

ADT Dictionary

Data structure Binary search tree (BST)Balanced BST - AVL treeHash Table

2CSC263 | Jessica Burgner-Kahrs

Page 3: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Dictionary

What’s stored§ words

Supported operations§ Search for a word§ Insert a word§ Delete a word

3CSC263 | Jessica Burgner-Kahrs

Page 4: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Dictionary, more preciselyWhat’s storedA set S of where each item/node x has a field x.key(assumption: keys are distinct, unless otherwise specified)

4

Node / item xKey k

CSC263 | Jessica Burgner-Kahrs

Page 5: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Dictionary ADT

What’s stored

A set S of where each item/node x has a field x.key(assumption: keys are distinct, unless otherwise specified)

Supported operations§ Search(S, k) return x in S, s.t., x.key = k

return NIL if no such x§ Insert(S, x) insert node x into S

if already exists node y with same key, replace y with x§ Delete(S, x) delete a given node x from S

5

Remember: k is a key, x is a node.

CSC263 | Jessica Burgner-Kahrs

Page 6: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

More on Delete

Why Delete(S, x) instead of Delete(S, k)?

Delete(S, k) can be implemented by:x = Search(S, k)Delete(S, x)

We want to separate different operations, i.e., each operation focuses on only one job.

6CSC263 | Jessica Burgner-Kahrs

Page 7: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Think of use cases for an ADT Dictionary

1min > Think for yourself1 min > Pair with your neighbor and talk about it

Share with the class

CSC263 | Jessica Burgner-Kahrs7

Page 8: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Implementing a Dictionaryusing simple data structures

CSC263 | Jessica Burgner-Kahrs8

Page 9: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Unsorted (doubly) linked list

Search(S,k)

Insert(S,x)

Delete(S,x)

9

40 ⇄ 33 ⇄ 18 ⇄ 65 ⇄ 24 ⇄ 25

CSC263 | Jessica Burgner-Kahrs

O(n) worst casego through the list to find the key

O(n) worst caseneed to check if x.key is already in the list

O(1) worst casejust delete, O(1) in a doubly linked list

Page 10: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Sorted array

10

[ 18 , 24 , 25 , 33 , 40 , 65 ]

O(log n) worst casebinary search!

O(n) worst caseinsert at front, everything has to shift to back

O(n) worst casedelete, shift left

CSC263 | Jessica Burgner-Kahrs

Search(S,k)

Insert(S,x)

Delete(S,x)

Page 11: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

We can do better using smarter data structures!

CSC263 | Jessica Burgner-Kahrs11

unsorted list sorted array

Search(S,k) O(n) O(log n)

Insert(S,x) O(n) O(n)

Delete(S,x) O(1) O(n)

BST

O(n)

O(n)

O(n)

Balanced BST

O(log n)

O(log n)

O(log n)

Page 12: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Binary Search Tree

CSC263 | Jessica Burgner-Kahrs12

Page 13: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

It’s a binary tree, like binary heap

Each node has at most 2 children

13CSC263 | Jessica Burgner-Kahrs

Page 14: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

need NOT be nearly-complete, unlike binary heap

14CSC263 | Jessica Burgner-Kahrs

Page 15: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

It has the BST property

15

< >

For every node x in the tree

All nodes in the leftsubtree have keys smallerthan x.key

All nodes in the rightsubtree have keys largerthan x.key

CSC263 | Jessica Burgner-Kahrs

Page 16: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

BST or NOT?

CSC263 | Jessica Burgner-Kahrs16

65

8040

66

6033 82

41

65

8040

64

6033 82

41

Page 17: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Because of BST property, we can say that the keys in a BST are sorted.

CSC148 Quiz: How to obtain a sorted list from a BST?

Perform an inorder traversal.

17CSC263 | Jessica Burgner-Kahrs

Page 18: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

18

InorderTraversal(x):# print all keys in BST rooted at x in ascending order

if x ≠ NIL:InorderTraversal(x.left)print x.keyInorderTraversal(x.right)

We pass a BST to a function by passing its root node.

Worst case running time of InorderTraversal:O(n), because visit each node exactly once

CSC263 | Jessica Burgner-Kahrs

Page 19: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Operations on a BST

CSC263 | Jessica Burgner-Kahrs19

Page 20: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Information at each node x

x.key the key

x.left the left child (node)

x.right the right child (node)

x.p the parent (node)

x.value the value

20CSC263 | Jessica Burgner-Kahrs

Page 21: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Operations on a BST

read-only operationsTreeSearch(root,k)TreeMinimum(x) / TreeMaximum(x)Successor(x) / Predecessor(x)modifying operationsTreeInsert(root,x)TreeDelete(root,x)

21CSC263 | Jessica Burgner-Kahrs

Page 22: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeSearch(root,k)Search the BST rooted at root,

return the node with key k; return NIL if not exist.

CSC263 | Jessica Burgner-Kahrs22

Page 23: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeSearch(root,k)

§ start from root§ if k is smaller than the key of the

current node, go left§ if k is larger than the key of the

current node, go right§ if equal, found§ if going to NIL, not found

23

65

8040

64

6033 82

41

CSC263 | Jessica Burgner-Kahrs

Page 24: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeSearch(root,k) Pseudo-code

24

TreeSearch(root, k):

if root == NIL or k == root.key:return root

if k < root.key:return TreeSearch(root.left, k)

else:return TreeSearch(root.right, k)

Worst case running time:O(h), height of tree, going at most from root to leaf

CSC263 | Jessica Burgner-Kahrs

Page 25: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeMinimum(x)Return the node with the minimum key of the tree rooted at x

CSC263 | Jessica Burgner-Kahrs25

Page 26: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeMinimum(x)

§ start from root§ keep going to the left, until cannot go anymore§ return that final node

26

65

8040

64

6033 82

41

CSC263 | Jessica Burgner-Kahrs

Page 27: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeMinimum(x) Pseudo-code

27

TreeMinimum(x):

while x.left ≠ NIL:x = x.left

return x

Worst case running time:O(h), height of tree, going at most from root to leaf

TreeMaximum(x) is exactly the same, except that it goes to the right instead of to the left.

CSC263 | Jessica Burgner-Kahrs

Page 28: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Successor(x)Find the node which is the successor of x

in the sorted list obtained by inorder traversal

or, node with the smallest key larger than x

CSC263 | Jessica Burgner-Kahrs28

Page 29: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Successor(x)

§ The successor of 33 is... § The successor of 40 is… § The successor of 64 is… § The successor of 65 is …

29

65

8040

64

6033 82

43

62

40436580

CSC263 | Jessica Burgner-Kahrs

Page 30: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Successor(x)Organize into two cases§ x has a right child (easy)

§ x does not have a right child (less easy)

30CSC263 | Jessica Burgner-Kahrs

Page 31: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

x has a right child

Successor(x) must be in x’s right subtree (the nodes right after x in

the inorder traversal)

It’s the minimum of x’s right subtree,

i.e., TreeMinimum(x.right)

31

65

8040

64

6033 82

43

62

The first (smallest) node among what’s right after x.CSC263 | Jessica Burgner-Kahrs

Page 32: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

x does not have a right child

How to find

§ go up to x.p

§ if x is a right child of x.p, keep going up

§ if x is a left child of x.p, stop, x.p is the guy!

32

65

8040

64

6033 82

43

62

Find this guy!

97

99

CSC263 | Jessica Burgner-Kahrs

Page 33: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

x does not have a right child

Consider the inorder traversal(left subtree -> root -> right subtree)

33

y

Ax

Find this guy!

x is the last one visited in some node y’s left subtree A (because no right child)

The successor y of x is the lowest ancestorof x whose left subtree contains x(y is visited right after finishing subtree A in inorder traversal)

CSC263 | Jessica Burgner-Kahrs

Page 34: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Summary of the two cases of Successor(x)If x has a right child

return TreeMinimum(x.right)

If x does not have a right child§ keep going up to x.p while x is a right child, stop when x

is a left child, then return x.p§ if already gone up to the root and still not finding it,

return NIL.

34CSC263 | Jessica Burgner-Kahrs

Page 35: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Successor(x) Pseudo-code

35

Successor(x):if x.right ≠ NIL:

return TreeMinimum(x.right)y = x.pwhile y ≠ NIL and x == y.right: #x is right child

x = yy = y.p # keep going up

return y

Worst case running timeO(h), Case 1: TreeMin is O(h); Case 2: at most leaf to root

CSC263 | Jessica Burgner-Kahrs

Page 36: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

36

Predecessor(x)works symmetrically the same way as

Successor(x)

CSC263 | Jessica Burgner-Kahrs

Page 37: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Agenda Recap

ADT: DictionaryData structure: BSTread-only operationsTreeSearch(root, k)TreeMinimum(x) / TreeMaximum(x)Successor(x) / Predecessor(x)

modifying operationsTreeInsert(root, x)TreeDelete(root, x)

37CSC263 | Jessica Burgner-Kahrs

Page 38: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

38CSC263 | Jessica Burgner-Kahrs

7 min

Page 39: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeInsert(root, x)Insert node x into the BST rooted at rootreturn the new root of the modified tree

if exists y, s.t. y.key = x.key, replace y with x

CSC263 | Jessica Burgner-Kahrs39

Page 40: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeInsert(root, x)

Go down, left and right like what we do in TreeSearch

When next position is NIL, insert there

If find equal key, replace the node

40

65

8040

64

6033 82

43

62

42

42

CSC263 | Jessica Burgner-Kahrs

Page 41: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Exercise

41

65

8040

61

6033 82

43

62

81

81

64

64

CSC263 | Jessica Burgner-Kahrs

Page 42: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Ex 2: Insert sequence into an empty tree

42

Insert sequence 1:11, 5, 13, 12, 6, 3, 14

Insert sequence 2:3, 5, 6, 11, 14, 13, 12

11

5 13

12 143 6

3

5

6

13

11

12

Different insert sequences result in different “shapes” (heights) of the BST.

14

CSC263 | Jessica Burgner-Kahrs

Page 43: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeInsert(root, x): Pseudo-code

43

TreeInsert(root, x):# insert and return the new root

if root == NIL:root = x

elif x.key < root.key:root.left = TreeInsert(root.left, x)

elif x.key > root.key:root.right = TreeInsert(root.right, x)

else # x.key == root.key:replace root with x # update x.left, x.right

return root

Worst case running time:O(h)

CSC263 | Jessica Burgner-Kahrs

Page 44: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeDelete(root, x)Delete node x from BST rooted at root while maintaining BST property,

return the new root of the modified tree

CSC263 | Jessica Burgner-Kahrs44

Page 45: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

What’s tricky about deletion?

Tree might be disconnectedafter deleting a node, need to connect them back together, while maintaining the BST property.

45

65

8040

64

6033 82

43

62

CSC263 | Jessica Burgner-Kahrs

Page 46: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Delete(root, x): Organize into 3 cases

Case 1: x has no child

Case 2: x has one child

Case 3: x has two children

46

Easy

Easy

Less easy

CSC263 | Jessica Burgner-Kahrs

Page 47: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 1: x has no child

47

65

8040

64

6033 82

43

62

Just delete it, nothing else need to be changed.

CSC263 | Jessica Burgner-Kahrs

Page 48: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 2: x has one child

First delete that node, which makes an open spot.

Then promote x’s only child to the spot, together with the only child’s subtree.

48

65

8040

58

5533 82

57 62

CSC263 | Jessica Burgner-Kahrs

Page 49: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 2: x has one child

49

65

8040

5833 82

57 62

First delete that node, which makes an open spot.

Then promote x’s only child to the spot, together with the only child’s subtree.

CSC263 | Jessica Burgner-Kahrs

Page 50: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3: x has two children

Delete x, which makes an open spot.

A node y should fill this spot, such that L < y < R.Which one should be y?

50

x

L R

y ← the minimum of R, i.e., Successor(x)L < y because y is in R, y < R because it’s minimum

CSC263 | Jessica Burgner-Kahrs

Page 51: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Further divide into two cases

51

Case 3.1:y happens to be the right child of x

Case 3.2:y is not the right child of x

x

L

y

x

L

z

yno left child, coz y is min

no left child coz y is min

CSC263 | Jessica Burgner-Kahrs

Page 52: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3.1: y is x’s right child

Easy, just promotey to x’s spot

52

x

L

y

CSC263 | Jessica Burgner-Kahrs

Page 53: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

L

y

Case 3.1: y is x’s right child

Easy, just promotey to x’s spot

53CSC263 | Jessica Burgner-Kahrs

Page 54: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3.2: y is NOT x’s right child

1. Promote w to y’s spot, y becomes free.

54

x

L

z

y

w

Order: y < w < z

CSC263 | Jessica Burgner-Kahrs

Page 55: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3.2: y is NOT x’s right child

1. Promote w to y’s spot, y becomes free.

55

2. Make z be y’s right child(y adopts z)

x

L

zy

w

Order: y < w < z

CSC263 | Jessica Burgner-Kahrs

Page 56: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3.2: y is NOT x’s right child

1. Promote w to y’s spot, y becomes free.

56

2. Make z be y’s right child(y adopts z)

Order: y < w < z

x

Lz

y

w3. Promote y to x’s spot

CSC263 | Jessica Burgner-Kahrs

Page 57: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Case 3.2: y is NOT x’s right child

1. Promote w to y’s spot, y becomes free.

57

2. Make z be y’s right child(y adopts z)

Order: y < w < z

L

z

y

w3. Promote y to x’s spot

x deletedBST order maintained, all is good.CSC263 | Jessica Burgner-Kahrs

Page 58: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

More thinking about Case 3.2Okay, promote, adopt, promote... I can see that it works, pretty clever.

But HOW ON EARTH can I come up with this kind of clever algorithms!

Thinking Process: understand the BST property (the invariant), predict the final shape of the tree, and see how to get there.

58

Order: y < w < z

CSC263 | Jessica Burgner-Kahrs

x

L

z

y

w L

z

y

w

Page 59: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Summarize TreeDelete(root, x)Case 1: x has no child, just deleteCase 2: x has one child, promoteCase 3: x has two children, y = Successor(x)

Case 3.1: y is x’s right child, promoteCase 3.2: y is NOT x’s right child

§ promote y’s right child§ y adopt x’s right child§ promote y

59CSC263 | Jessica Burgner-Kahrs

Page 60: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeDelete(root,x) Pseudo-codeCLRS Chapter 12.3Key: Understand Transplant(root,u,v)# v takes away u’s parent

60

u v u v

used for promoting v and deleting u

CSC263 | Jessica Burgner-Kahrs

Page 61: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Transplant(root, u, v):# v takes away u’s parentif u.p == NIL: # if u is root

root = v # v replaces u as rootelif u == u.p.left:# if u is parent’s left child

u.p.left = v #parent accepts v as left childelse: # if u is parent’s right child

u.p.right = v #parent accepts v as right childif v ≠ NIL:

v.p = u.p # v accepts new parent

61CSC263 | Jessica Burgner-Kahrs

Page 62: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeDelete(root, x):if x.left == NIL:

Transplant(root, x, x.right)elif x.right == NIL:

Transplant(root, x, x.left)else:

y = TreeMinimum(x.right)if y.p ≠ x:

Transplant(root, y, y.right)y.right = x.righty.right.p = y

Transplant(root, x, y)y.left = x.lefty.left.p = y

return root

Promote right child

Promote left child

get Successor(x)

y is not right child of x

promote w

y adopts z

promote y

update pointers

Case 1 & 2

Case 3

Case 3.2

62CSC263 | Jessica Burgner-Kahrs

Page 63: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

TreeDelete(root, x) worst case running time

O(h) (time spent on TreeMinimum)

63CSC263 | Jessica Burgner-Kahrs

Page 64: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Now, about that h(height of tree)

CSC263 | Jessica Burgner-Kahrs64

Page 65: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Definition: Height of a tree

The longest path from the root to a leaf, in terms of number of edges.

65

65

8040

64

6033 82

43

62h = 4

CSC263 | Jessica Burgner-Kahrs

Page 66: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Consider a BST with n nodes, what’s the highest it can be?h = n-1

i.e, in worst case h ∈ Θ(n)

so all the operations we learned with O(h)runtime, they are O(n) in worst case

66CSC263 | Jessica Burgner-Kahrs

Page 67: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

So, what’s the best case for h ?

In best case, h ∈ Θ(log n)

A Balanced BST guarantees to have height in Θ(log n)

67

Therefore, all the O(h) become O(log n)CSC263 | Jessica Burgner-Kahrs

Page 68: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Recap Quiz

CSC263 | Jessica Burgner-Kahrs68

Page 69: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Question 1

How to visit the nodes in a BST in a sorted order?

q inorder traversalq preorder traversalq postorder traversalq level-by-level traversal

CSC263 | Jessica Burgner-Kahrs69

Page 70: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Question 2

Node x has two children. The predecessor of x is the tree _________ in x’s _________ subtree.

q maximum, rightq maximum, leftq minimum, leftq minimum, right

CSC263 | Jessica Burgner-Kahrs70

Page 71: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Question 3

Insert three keys into a BST in this order: 2, 6, 3.What is the height of the resulting tree?

q 0q 1q 2q 3

CSC263 | Jessica Burgner-Kahrs71

Page 72: Dictionary ADT - University of Toronto263/jessica/lecture3.pdf · Dictionary ADT What’s stored A set Sof where each item/node xhas a field x.key (assumption: keys are distinct,

Next week

A Balanced BST called AVL tree

72CSC263 | Jessica Burgner-Kahrs