BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a...

58
cs2420 | Introduction to Algorithms and Data Structures | Spring 2015 BINARY SEARCH TREES 1

Transcript of BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a...

Page 1: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

cs2420 | Introduction to Algorithms and Data Structures | Spring 2015BINARY SEARCH TREES

1

Page 2: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

administrivia…

2

Page 3: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

3

-assignment 7 due tonight at midnight

-asking for regrades through assignment 5 and midterm must be complete by Friday

Page 4: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

last time…

4

Page 5: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

5

Page 6: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

6

-trees are a linked data structure with a hierarchical formation

-any node is a subtree of some larger tree -except the very top node!

-there is a strict parent-to-child relationship among nodes

-links only go from parent to child a

b c

d

-there is exactly one path from the root to any other node

Page 7: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

7

root

parent

children

Leaf nodes

nodes

Page 8: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

8

subtree rooted at node d

a

b

e

h i

d

f g

c

subtree rooted at node c (leaf nodes are trees too!)

Page 9: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

9

public static void DFT(BinaryNode N) { if(N == null) return;

System.out.println(N.data);

DFT(N.left); DFT(N.right);

}

a

b c

d e

f

what does this print out?a b d f e c

Page 10: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

-pre-order:

-in-order:

-post-order:

10

use N // eg. print N DFT(N.left); DFT(N.right);

DFT(N.left); use N // eg. print N DFT(N.right);

DFT(N.left); DFT(N.right); use N // eg. print N

a

b c

d e

f

note: Nodes are still traversed in the same order, but “used” (printed) in a different order

Page 11: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

11

how to compute the height of a binary tree?

int height(Node n) { if(n == null) return -1;

else return max(height(n.left), height(n.right)) + 1;

}

complexity?

Page 12: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

12

+

- *

/ ^3

15 11

7

2 4

evaluating an expression tree uses which of the following traversal orders?A) pre-order B) in-order C) post-order

Page 13: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

13

*

3-

5 2

what is the value of this expression tree?A) 9 B) -1 C) -5

Page 14: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

14

d

eb

a c

what order are the nodes printed using pre-order traversal?A) d b a c e B) a b c d e C) a c b e d

Page 15: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

15

Page 16: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

today…

16

Page 17: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

17

-binary search trees

-insertion

-performance

-deletion

Page 18: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

binary search trees (BSTs)

18

Page 19: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

19

-a binary search tree is a binary tree with a restriction on the ordering of nodes

-all items in the left subtree of a node are less than the item in the node -all items in the right subtree of a node are greater than or equal to the item in the node

-BSTs allow for fast searching of nodes

Page 20: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

20

looking for h i

d j

fb

e ha c

g

is h less than or greater than i?

Page 21: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

21

d

fb

e ha c

g

looking for h

is h less than or greater than d?

Page 22: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

22

f

e h

g

looking for h

is h less than or greater than f?

Page 23: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

23

h

g

looking for h

return true

Page 24: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

24

-NOTE: at each step we eliminate approximately half of the tree

-what does this imply about the search complexity?

-what if the item is not found in the tree? how do we know when to stop and return false?

Page 25: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

25

is this a BST?A) yes B) no

dog

cat fish

alpacaelephant

zebra

bee

unicorn

bird

Page 26: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

26

is this a BST?A) yes B) no

p

k y

ni

c

rg j

a d

zt

u

v

Page 27: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

insertion

27

Page 28: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

28

i

d j

fb

e ha c

we want to insert g is g less than or greater than i?

Page 29: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

29

i

d j

fb

e ha c

we want to insert g

is g less than or greater than d?

Page 30: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

30

i

d j

fb

e ha c

we want to insert g

is g less than or greater than f?

Page 31: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

31

i

d j

fb

e ha c

we want to insert g

is g less than or greater than h?

Page 32: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

32

we want to insert g i

d j

fb

e ha c

gcode demo…

Page 33: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

performance

33

Page 34: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

-big-O complexity of BST searching-we disregard one subtree at each step

-roughly half of the remaining nodes! -O(log N)

-what is the big-O complexity of BST insertion?

-searching and insertion are both O(log N)

34

Page 35: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

-O(log N) performance requires eliminating half of the possibilities on each step…

-are all left and right subtrees the same size?

35

i

d j

fb

e ha c

g

Page 36: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

-is this a valid BST?

-what is the big-O complexity of searching and insertion for this tree?

-the order in which nodes are inserted determines the structure of the tree

-these nodes were inserted in descending order

36

i

d

b

a

code demo…

Page 37: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

insertion & searching-average case: O(log N)

-inserted in random order

-worst case:O(N)-inserted in ascending or descending order

-best case: O(log N)

-how does this compare to a sorted array?

37

Page 38: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

BST vs array-arrays can have O(log N) searching performance as well

-how?

-what is the cost of inserting into an array?-we can put it at the end and resort: O(log N) -or insert it in the right spot: O(N)

38

BST wins for insertion! yay!

Page 39: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

balanced vs unbalancedall operations depend on how well-balanced the tree is

4

2 6

31 75

1

2

3

4

5

6

7

O(log N) balanced

O(N) unbalanced

Page 40: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

deletion

40

Page 41: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

41

-since we must maintain the properties of a tree structure, deletion is more complicated than with an array or linked-list

-there are three different cases:1.deleting a leaf node 2.deleting a node with one child subtree 3.deleting a node with two children subtrees

-first step of deletion is to find the node to delete-just a regular BST search -BUT, stop at the parent of the node to be deleted

Page 42: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 1: deleting a leaf node20

9 27

165

10

192 6 11

17

current

DELETE NODE 6 -stop at parent node (5) -set parent’s reference to null

current.right = null;

Page 43: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 1: deleting a leaf node20

9 27

165

10

192 11

17

current

current.right = null;

DELETE NODE 6 -stop at parent node (5) -set parent’s reference to null

Page 44: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 2: delete node with 1 child20

9 27

165

10

192 6 11

17

current

DELETE NODE 19 -stop at parent node (16) -set parent’s reference to node’s child -multiple cases depending on which side the child and grandchild are on!

current.right = current.right.left;

Page 45: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 2: delete node with 1 child20

9 27

165

10

172 6 11

current

DELETE NODE 19 -stop at parent node (16) -set parent’s reference to node’s child -multiple cases depending on which side the child and grandchild are on!

current.right = current.right.left;

Page 46: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 3: delete node with 2 children20

9 27

165

10

192 6 11

17

current

DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10)

Page 47: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 3: delete node with 2 children20

10 27

165

10

192 6 11

17

current

DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10) -perform a delete on on successor (10)

Page 48: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 3: delete node with 2 children20

10 27

165

192 6 11

17

current

DELETE NODE 9 -stop at parent node (20) -replace the node with smallest item in its right subtree(10) -perform a delete on successor (10) (guaranteed not to have a left child! case 1 or 2…)

Page 49: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

case 3: delete node with 2 children20

10 27

165

192 6 11

17

current

Replacing the deleted node with the smallest child in right subtree guarantees the BST structure… why?

The smallest item in the right subtree is greater than any item in the left subtree, and smaller than any item in the new right subtree.

Page 50: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

deletion performance

-first, find the node we want to delete:

-cost of:-case 1 (delete leaf):

-case 2 (delete node with 1 child):

-case 3 (delete node with 2 children):

50

what is the cost of deleting a node from a BST?

O(log N)

O(1)set a single reference to nulL:

O(1)bypass a reference:

Find the successor:

delete the duplicate successor:

O(log N)O(1)

Page 51: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

some other useful properties

-how can we find the smallest node?-start at the root, go as far left as possible -O(log N) on a balanced tree

-how can we find the largest node?-start at the root, go as far right as possible -O(log N) on a balanced tree

-how can we print nodes in ascending order?-in-order traversal

51

Page 52: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

quick review…

52

Page 53: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

53

d

eb

a

which of the following trees is the result of adding c to this bst?

d

eb

a

d

eb

a

d

eb

ac c c

A) B) C)

Page 54: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

54

9

5 16

6 192

3

11

12

what will 5’s left child be after deleting 2?A) 3 B) 6 C) null

Page 55: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

55

9

5 16

6 192

3

12

13

what node will replace 9 after deleting 9?A) 6 B) 10 C) 13 D) 19

10

11

Page 56: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

56

9

5 16

6 192

3

11

12

what node will replace 5 after deleting 5?A) 2 B) 3 C) 6 D) 12

Page 57: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

next time…

57

Page 58: BINARY SEARCH TREESmiriah/cs2420/lectures/L15-bst.pdf-a binary search tree is a binary tree with a restriction on the ordering of nodes-all items in the left subtree of a node are

58

-reading-chapter 14 in book

-homework-assignment 7 due tonight -assignment 8 out later today