Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1...

124
Lecture 10: Ordered Collections with Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Transcript of Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1...

Page 1: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lecture 10:

Ordered

Collections with

Binary Search

Trees

1These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

Page 2: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Tool tip: data structures / algs visualizer

Data structures / algorithms visualizer (copyright David Galles, USF)

2

Page 3: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Motivation – Limitations of Dictionaries

● We developed hashing to permit efficient dictionaries

○ Insert()

○ Remove()

○ Find()

● But hash tables have at least two undesirable limitations

1. Worst-case op performance is Θ(n) (only average case is good)

2. Do not adequately represent naturally ordered collections.

3

Page 4: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Ordered Dynamic Set Operations

● Besides the usual dictionary operations, ordered sets support

○ min / max – what is smallest/largest item in collection?

○ iterator – list collection’s items in order from smallest to largest

■ or predecessor/successor: find previous/next item in order

● See, e.g., Java SortedSet interface

● Many data types are naturally ordered (strings, ID #’s), even if we

don’t always use this fact. 4

Page 5: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Ordered Dynamic Set Operations

● Besides the usual dictionary operations, ordered sets support

○ min / max – what is smallest/largest item in collection?

○ iterator – list the items in the set in order from smallest to largest

■ or predecessor/successor: find previous/next item in order

● See, e.g., Java SortedSet interface

● Many data types are naturally ordered (strings, ID #’s), even if we

don’t always use this fact. 5

“Dynamic” means that a query of the

set must return the correct answer at

any point during a sequence of

insertions and deletions.

Page 6: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Candidate Implementations?

● Sorted Array

○ Θ(log n) find, O(1) min/max, O(1) iteration/item

○ Θ(n) insert/remove

● Sorted List

○ Much like array, except for Θ(n) find

● (Hash table does not support ordering – must iterate

through all items to find min/max or next item in order)6

Page 7: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What We Would Like from Our Ordered Sets

● Sub-linear time insert/remove/find

○ (what does sub-linear mean again?)

7

Page 8: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What We Would Like from Our Ordered Sets

● Sub-linear time insert/remove/find

○ (what does sub-linear mean again?)

● Sub-linear time min/max

● Iteration in sub-linear time per element

● All times worst-case (unlike a hash table)

8

Page 9: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How We’ll Get It

● New data structure – binary search tree (BST)

● Can do all operations in time proportional to height of tree

● But height isn’t necessarily sub-linear in size (unlike a heap)

● So we’ll consider how to force BSTs to have small height

9

Page 10: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Binary Trees, Revisited

● A BST is a type of binary tree.

● Tree is made of nodes, each of which

is root of a subtree

● Each node has left and right children,

and a parent (any may be null)

● Unlike heaps, trees used as BSTs

need not be compact.

10

node

left child right child

parent

left subtree right subtree

Page 11: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What Makes a Binary Tree a BST?

● Every node x contains a key value x.key

● Every node satisfies the following invariant (“BST property”):

● For every node y in x’s left subtree, y.key ≤ x.key

● For every node z in x’s right subtree, x.key ≤ z.key

● (If each key in BST is unique, these inequalities are strict <)

11

x

y z

Page 12: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What Makes a Binary Tree a BST?

● Every node x contains a key value x.key

● Every node satisfies the following invariant (“BST property”):

● For every node y in x’s left subtree, y.key ≤ x.key

● For every node z in x’s right subtree, x.key ≤ z.key

● (If each key in BST is unique, these inequalities are strict <)

12

5

? ?

Page 13: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What Makes a Binary Tree a BST?

● Every node x contains a key value x.key

● Every node satisfies the following invariant (“BST property”):

● For every node y in x’s left subtree, y.key ≤ x.key

● For every node z in x’s right subtree, x.key ≤ z.key

● (If each key in BST is unique, these inequalities are strict <)

13

5

2 7

Page 14: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What Makes a Binary Tree a BST?

● Every node x contains a key value x.key

● Every node satisfies the following invariant (“BST property”):

● For every node y in x’s left subtree, y.key ≤ x.key

● For every node z in x’s right subtree, x.key ≤ z.key

● (If each key in BST is unique, these inequalities are strict <)

14

5

5 7

Page 15: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

BST Property in Brief

● Node x is ≥ every node in its left subtree

● Node x is ≤ every node in its right subtree

● [Note that this is a different, stronger tree

invariant than heap property]

15

k

≤ k ≥ k

Page 16: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

BST Property in Brief

● Node x is ≥ every node in its left subtree

● Node x is ≤ every node in its right subtree

● [Note that this is a different, stronger tree

invariant than heap property]

16

k

≤ k ≥ k

We sometimes talk of

“comparing two nodes”…

we actually mean

comparing their keys.

Page 17: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

BST Property in Brief (With Unique Keys)

● Node x is > every node in its left subtree

● Node x is < every node in its right subtree

● [Note that this is a different, stronger tree

invariant than heap property]

17

k

< k > k

Page 18: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Using a BST, How Do We Implement…

● Find?

● Min/Max?

● Insert?

● Iterate?

● Remove?

18

Page 19: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Caveat - Uniqueness

● In what follows, we assume that keys in tree are all unique

● Still possible to have an efficient BST with duplicate keys…

● (E.g. if we must store two records with same key)

● …but it adds complexity to the ops and/or their correctness proofs.

19

Page 20: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find: Use the BST Property

● Suppose we search tree rooted at node x for key k

● If x.key = k, we are done!

● If x.key > k, search for k in ???

● If x.key < k, search for k in ???

20

Page 21: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find: Use the BST Property

● Suppose we search tree rooted at node x for key k

● If x.key = k, we are done!

● If x.key > k, search for k in subtree rooted at x.left

● If x.key < k, search for k in subtree rooted at x.right

● (If desired subtree is null, k is not found)

21

Page 22: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find Examples

22

10

7 13

16

14

5 9

62

3

Page 23: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 6?

23

10

7 13

16

14

5 9

62

3

Page 24: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 6?

24

10

7 13

16

14

5 9

62

3

Page 25: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 6?

25

10

7 13

16

14

5 9

62

3

Page 26: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 6?

26

10

7 13

16

14

5 9

62

3

Page 27: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 6?

27

10

7 13

16

14

5 9

62

3Found!

Page 28: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 8?

28

10

7 13

16

14

5 9

62

3

Page 29: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Find 8? Not Found!

29

10

7 13

16

14

5 9

62

3

(If it existed, it would be here)

Page 30: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Min and Max

● Thanks to BST property, we can easily find min key in tree…

● Remember, we assume unique keys

● Min node can’t have other nodes in its left subtree

● Min node can’t be in the right subtree of any other node

● So where is it?

30

Page 31: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Min and Max

● Thanks to BST property, we can easily find min key in tree…

● Remember, we assume unique keys

● Min node can’t have other nodes in its left subtree

● Min node can’t be in the right subtree of any other node

● Start at root, go left until no longer possible. Final node is min.

31

Page 32: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Min and Max

● Thanks to BST property, we can easily find min key in tree…

● Remember, we assume unique keys

● Min node can’t have other nodes in its left subtree

● Min node can’t be in the right subtree of any other node

● Start at root, go left until no longer possible. Final node is min.

32

Max is found by “opposite”

rule (keep going right), for

similar reasons.

Page 33: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Min/Max Examples

33

10

7 13

16

14

5 9

62

3

min

max

Page 34: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How to Insert a Key into a BST

● An unsuccessful find() ends at null subtree where node containing key

would be if it existed.

● → Create a new leaf node there and put the key in it!

34

Page 35: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert Examples

35

10

7 13

16

14

5 9

62

3

Page 36: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 8

36

10

7 13

16

14

5 9

62

3

Page 37: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 8

37

10

7 13

16

14

5 9

62

3

8 belongs here

Page 38: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 8

38

10

7 13

16

14

5 9

62

3

8

Page 39: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 15

39

10

7 13

16

14

5 9

62

3

8

Page 40: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 15

40

10

7 13

16

14

5 9

62

3

8

15 belongs here

Page 41: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 15

41

10

7 13

16

14

5 9

62

3

8

15

Page 42: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 12

42

10

7 13

16

14

5 9

62

3

8

15

Page 43: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 12

43

10

7 13

16

14

5 9

62

3

8

15

12 belongs here

Page 44: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Insert 12

44

10

7 13

16

14

5 9

62

3

8

15

12

Page 45: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

The Story So Far

● Find

● Min/Max

● Insert

● Iterate?

● Remove?

45

Page 46: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Cost of Operations

● Find – might have to walk from root to deepest leaf of tree

● Min/Max – same

● Insert – same

● Iterate?

● Remove?

46

Page 47: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Cost of Operations

● Find – Θ(h) for tree of height h

● Min/Max – same

● Insert – same

● Iterate?

● Remove?

47

Page 48: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

And Now, Some

Slightly Less Trivial

Methods

48

Page 49: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Iteration

● As we saw in the hash table lab, a collection can provide an iterator

● An iterator for a BST starts out pointing to the min node (by key)

● Each call to iterator.next() must move from current node to next largest

● This operation is called finding the successor of a node

● We write it as “succ(x)” for a node x

49

Page 50: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Where is Successor of Node x?

● If x has a right subtree T’…

● Leftmost (minimum) node z in T’ is > x.

● Every node > x that is not in T’ is > every node in

T’, hence is also > z.

● Conclude that succ(x) = z.

50

x

T’

z

Page 51: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Where is Successor of Node x?

● If x has no right subtree…

● If any node of tree is > x, then x is rightmost

(maximum) node in left subtree T of some node y.

● Every node < y that is not in T is < every node in

T, hence is also < x.

● Conclude that succ(x) = y.

51

x

y

T

Page 52: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How to Compute succ(x)

● If x has a right subtree T’

● return min(T’)

● Else

● follow parent pointers from x until some node y is a right parent

● return y

52

Page 53: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Successor Examples

53

10

7 13

16

14

5 9

62

3

Page 54: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(10)

54

10

7 13

16

14

5 9

62

3

Page 55: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(10) – 10 has a right subtree

55

10

7 13

16

14

5 9

62

3

Page 56: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(10) – min of right subtree of 10 is 13

56

10

7 13

16

14

5 9

62

3

Page 57: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(10) = 13

57

10

7 13

16

14

5 9

62

3

Page 58: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(6)

58

10

7 13

16

14

5 9

62

3

Page 59: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(6) – 6 has no right subtree

59

10

7 13

16

14

5 9

62

3

Page 60: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(6) – Follow parents to first right parent

60

10

7 13

16

14

5 9

62

3

Page 61: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(6) – Follow parents to first right parent

61

10

7 13

16

14

5 9

62

3

Page 62: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(6) = 7

62

10

7 13

16

14

5 9

62

3

Page 63: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(16)

63

10

7 13

16

14

5 9

62

3

Page 64: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(16) – 16 has no right subtree

64

10

7 13

16

14

5 9

62

3

Page 65: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(16) – follow parents to first right parent?

65

10

7 13

16

14

5 9

62

3

Page 66: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Succ(16) does not exist (16 is max!)

66

10

7 13

16

14

5 9

62

3

Page 67: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Cost of Operations

● Find – might have to walk from root to deepest leaf of tree

● Min/Max – same

● Insert – same

● Iterate – might have to walk from root to deepest leaf or vice versa

● Remove?

67

Page 68: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Cost of Operations

● Find – Θ(h) for tree of height h

● Min/Max – same

● Insert – same

● Iterate – same

● Remove?

68

Page 69: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Last But Not Least, Remove(k)

● First, walk down from root to locate node x with key k, as for find().

● Three possibilities for node x to be removed:

69

k

kk

x is a leaf x has one subtree x has two subtrees

Page 70: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Easy Cases for Removal (Verify BST Property)

● If x is a leaf, removing x does not impact remaining tree at all.

● If x has one subtree, remove x and link subtree’s root to x’s parent.

● (BST property holds between x’s parent and its entire subtree)

70

k

k

Page 71: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove(6)

71

10

7 13

16

14

5 9

62

3

Page 72: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove(6)

72

10

7 13

16

14

5 9

2

3

Page 73: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove(13)

73

10

7 13

16

14

5 9

62

3

Page 74: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove(13)

74

10

7

16

14

5 9

62

3

Page 75: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove(13)

75

10

7 16

145 9

62

3

Page 76: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Removing a Node With Two Subtrees

● We cannot just delete the node!

● One parent, two subtrees – no place to put one of the subtrees

● Instead, will preserve tree structure by “stealing” key from a subtree

76

k

Page 77: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Removing a Node With Two Subtrees

● Let x be node to be deleted, and let y = succ(x).

● Replace x.key by y.key

● This is safe for BST property – why?

● Now delete duplicate copy of y.key by

removing y

77

k

j

node x

node y = succ(x)

Page 78: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Removing a Node With Two Subtrees

● Let x be node to be deleted, and let y = succ(x).

● Replace x.key by y.key

● This is safe for BST property – why?

● Now delete duplicate copy of y.key by

removing y

78

j

j

node x

node y = succ(x)

Page 79: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Removing a Node With Two Subtrees

● Let x be node to be deleted, and let y = succ(x).

● Replace x.key by y.key

● This is safe for BST property – why?

● Now delete duplicate copy of y.key by

removing y

79

j

j

node x

node y = succ(x)

Page 80: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 5

80

10

7 13

16

14

5 9

62

3

Page 81: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 5

81

10

7 13

16

14

5 9

62

3

succ(5) = 6

Page 82: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 5

82

10

7 13

16

14

6 9

62

3

succ(5) = 6

Page 83: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 5

83

10

7 13

16

14

6 9

2

3

Page 84: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 10

84

10

7 13

16

14

5 9

62

3

Page 85: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 10

85

10

7 13

16

14

5 9

62

3

succ(10) = 13

Page 86: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 10

86

13

7 13

16

14

5 9

62

3

succ(10) = 13

Page 87: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Remove 10

87

13

7 16

145 9

62

3

Page 88: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Sanity Check – Is Recursive Remove Safe?

● If we remove a node with two subtrees…

● Its successor is leftmost node of its right subtree.

● Leftmost node has no left subtree.

● Hence, “recursive” remove always removes node with 0 or

1 subtrees – easy cases!

88

Page 89: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Cost of Operations

● Find – might have to walk from root to deepest leaf of tree

● Min/Max – same

● Insert – same

● Iterate – might have to walk from root to deepest leaf or vice versa

● Remove – might have to walk from root to deepest leaf

89

Page 90: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Costs for BST Operations

● Find – Θ(h) for tree of height h

● Min/Max – Θ(h) for tree of height h

● Insert – Θ(h) for tree of height h

● Iterate – Θ(h) for tree of height h

● Remove – Θ(h) for tree of height h

90

Page 91: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Worst-Case Costs for BST Operations

● Find – Θ(h) for tree of height h

● Min/Max – Θ(h) for tree of height h

● Insert – Θ(h) for tree of height h

● Iterate – Θ(h) for tree of height h

● Remove – Θ(h) for tree of height h

91

Are these costs sublinear

in n, the # of nodes in the

tree? Depends how #

nodes relates to height.

Page 92: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Here’s a binary tree with n nodes:

● This tree has height ???.

92

…n nodes

Page 93: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Here’s a binary tree with n nodes:

● This tree has height n-1.

● Can we realize this tree as a BST by some sequence of

insertions?93

Page 94: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Insert keys 1..n in order

94

1

Page 95: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Insert keys 1..n in order

95

1

2

Page 96: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Insert keys 1..n in order

96

1

2

3

Page 97: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

How Tall Can a BST with n Nodes Be?

● Insert keys 1..n in order

97

1

2

3

n

Page 98: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Bad News…

● Given the right sequence of insertions, a BST with n nodes can have

height Θ(n)

● That means that all our BST operations are worst-case Θ(n)

● This is no better in the worst case than a list or array. In fact, it’s

worse for some operations (e.g. min/max).

98

Page 99: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Can We Overcome

Worst-Case Θ(n)

Costs for Tree

Operations?

99

Page 100: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What If Our Trees Were Never Too Tall?

● Defn: a binary tree with n nodes is said to be balanced if it

has height O(log n).

● Example: a complete binary tree with 2n-1 nodes has

height n – 1, so is balanced.

● In a balanced BST, all BST ops are worst case O(log n).

100

Page 101: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What If Our Trees Were Never Too Tall?

● Defn: a binary tree with n nodes is said to be balanced if it

has height O(log n).

● Example: a complete binary tree with 2n-1 nodes has

height n – 1, so is balanced.

● In a balanced BST, all BST ops are worst case O(log n).

101

Really, we can write

Θ(log n) here – all

binary trees have

height Ω(log n).

Page 102: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Strategy for Balancing Trees

1. Define a structural property P that applies to only some

BSTs

2. Prove that BSTs satisfying property P are balanced

3. Make sure a trivial BST (one node) satisfies P

4. Show how to insert, remove while maintaining P○ i.e. show that P is an invariant of the BST

102

Page 103: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

An Example of a Balance Property

● AVL Property

● Described 1962 by Adelson-Velsky and Landis

● A tree T satisfies the AVL property if for each node in T,

its left and right subtrees differ in height by at most 1.

● Intuitively, prevents very lopsided trees.

103

Page 104: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

AVL Property for Binary Trees – Formal Defn

● Let H(r) be the height of a binary tree rooted at r

● Defn: T is an AVL tree iff, for every node x in T, one of these is true:

1. x is a leaf.

2. x has one child, which is a leaf.

3. x has two children, and |H(x.right) – H(x.left)| ≤ 1.

104

h h+1

Page 105: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

AVL Property for Binary Trees – Formal Defn

● Let H(r) be the height of a binary tree rooted at r

● Defn: T is an AVL tree iff, for every node x in T, one of these is true:

1. x is a leaf.

2. x has one child, which is a leaf.

3. x has two children, and |H(x.right) – H(x.left)| ≤ 1.

105

hh+1

Page 106: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree?

106

10

7 13

16

14

5 9

62

3

Page 107: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree? NO!

107

10

7 13

16

14

5 9

62

3

C

May not have a node with

one child that is not a leaf.

Page 108: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree?

108

10

7 13

16

14

5 9

62

3

11

Page 109: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree? NO!

109

10

7 13

16

14

5 9

62

3

11

C

Left subtree has height 2;

Right subtree has height 0

Page 110: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree?

110

10

7 13

16

14

5 9

62

11

Page 111: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Is This an AVL Tree? YES!

111

10

7 13

16

14

5 9

62

11

Page 112: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Why Are AVL Trees Balanced?

● Intuitively, a tall tree with few nodes is

“skinny”

● Long path to its deepest leaf cannot

have many nodes branching off it.

● Skinny trees have subtrees with very

different heights

● AVL property prevents skinny trees112

Page 113: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Why Are AVL Trees Balanced?

● Intuitively, a tall tree with few nodes is

“skinny”

● Long path to its deepest leaf cannot

have many nodes branching off it.

● Skinny trees have subtrees with very

different heights

● AVL property prevents skinny trees113

Let’s formalize this

idea to prove that an

AVL tree is balanced.

Page 114: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What is “Skinniest” AVL Tree We Can Build?

● Let N(h) be minimum # of nodes in any AVL tree with

height h.

● N(0) = 1 N(1) = 2

● Can we find a formula for N(h) for h > 1?

114

Page 115: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What is “Skinniest” AVL Tree We Can Build?

● If tree has height h, root’s tallest subtree has height ???.

115

Page 116: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What is “Skinniest” AVL Tree We Can Build?

● If tree has height h, root’s tallest subtree has height h-1.

● By AVL property, other subtree must have height ≥ ???.

116

Page 117: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What is “Skinniest” AVL Tree We Can Build?

● If tree has height h, root’s tallest subtree has height h-1.

● By AVL property, other subtree must have height ≥ h-2.

● Both subtrees are also AVL trees.

● Hence, N(h) = N(h-1) + N(h-2) + 1

117

2 subtrees, plus 1

node for root.

Page 118: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

What is “Skinniest” AVL Tree We Can Build?

● If tree has height h, root’s tallest subtree has height h-1.

● By AVL property, other subtree must have height ≥ h-2.

● Both subtrees are also AVL trees.

● Hence, N(h) = N(h-1) + N(h-2) + 1

118

Let’s guess a solution

to recurrence for N(h)

and check our guess.

Page 119: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lower Bound on AVL Tree Size vs Height

● Let Φ =5+1

2≈ 1.618 . [Yes, the golden ratio again]

● Claim: N(h) ≥ Φh

● → Every AVL tree with height h has ≥ Φh nodes

● → Every AVL tree with n nodes has height ≤ logΦ(n),

hence is balanced.119

Page 120: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lower Bound Proof, 1/2

● Claim: N(h) ≥ Φh

● Pf: by induction on h

● Base 1: N(0) = 1 ≥ Φ0

● Base 2: N(1) = 2 ≥ Φ1

120

Page 121: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lower Bound Proof, 2/2

● Ind: N(h) = N(h-1) + N(h-2) + 1

● ≥ N(h-1) + N(h-2)

● ≥ Φh-1 + Φh-2

● = Φh-2 (Φ + 1)

121

Apply inductive

hypothesis.

Page 122: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lower Bound Proof, 2/2

● Ind: N(h) = N(h-1) + N(h-2) + 1

● ≥ N(h-1) + N(h-2)

● ≥ Φh-1 + Φh-2

● = Φh-2 (Φ + 1)

122

Fact:

Φ2 = Φ+1

Page 123: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Lower Bound Proof, 2/2

● Ind: N(h) = N(h-1) + N(h-2) + 1

● ≥ N(h-1) + N(h-2)

● ≥ Φh-1 + Φh-2

● = Φh-2 (Φ + 1)

● = Φh-2 Φ2

● = Φh. QED

123

Fact:

Φ2 = Φ+1

Page 124: Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1 These slides include material originally prepared by Dr. Ron Cytron, Dr. Jeremy Buhler,

Next Time

How can we modify BST insertion and deletion to ensure

that the trees they create are always AVL trees?

124