TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Lecture 10: Ordered Collections with Binary Search Trees · 2020-06-26 · Binary Search Trees 1...
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/1.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/2.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/3.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/4.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/5.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/6.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/7.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/8.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/9.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/10.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/11.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/12.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/13.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/14.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/15.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/16.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/17.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/18.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/19.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/20.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/21.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/22.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/23.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/24.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/25.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/26.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/27.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/28.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/29.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/30.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/31.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/32.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/33.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/34.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/35.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/36.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/37.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/38.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/39.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/40.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/41.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/42.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/43.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/44.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/45.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/46.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/47.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/48.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/49.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/50.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/51.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/52.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/53.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/54.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/55.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/56.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/57.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/58.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/59.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/60.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/61.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/62.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/63.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/64.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/65.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/66.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/67.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/68.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/69.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/70.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/71.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/72.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/73.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/74.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/75.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/76.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/77.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/78.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/79.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/80.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/81.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/82.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/83.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/84.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/85.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/86.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/87.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/88.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/89.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/90.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/91.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/92.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/93.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/94.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/95.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/96.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/97.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/98.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/99.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/100.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/101.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/102.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/103.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/104.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/105.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/106.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/107.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/108.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/109.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/110.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/111.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/112.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/113.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/114.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/115.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/116.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/117.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/118.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/119.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/120.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/121.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/122.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/123.jpg)
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,](https://reader034.fdocuments.us/reader034/viewer/2022042804/5f528884918efd0b9a7fa7a4/html5/thumbnails/124.jpg)
Next Time
How can we modify BST insertion and deletion to ensure
that the trees they create are always AVL trees?
124