Lecture 9: Binary Search Trees
CSE 373: Data Structures and Algorithms
1
Announcements
No poll today, finish Monday’s poll (extended to Friday at 1:00PM due to broken tinyurl link)
CSE 373 22 SP – CHAMPION
2
Strategies to handle hash collision
CSE 373 AU 18 – SHRI MARE
3
Separate chaining
public boolean containsKey(int key) {
int bucketIndex = key % data.length;
loop through data[bucketIndex]
return true if we find the key in
data[bucketIndex]
return false if we get to here (didn’t
find it)
}
CSE 373 ROBBIE WEBER + HANNAH TANG
4
Reminder: the implementations of put/get/containsKey are all very similar, and almost always will have the same complexity class runtime
runtime analysis
Are there different possible states for our Hash Map that make this code run slower/faster, assuming there are already n key-value pairs being stored?
Yes! If we had to do a lot of loop iterations to find the key in the bucket, our code will run slower.
Handling Collisions
int index = natrualHash % TableSize;
while (index in use) {
i++;
}
return index;
CSE 373 SP 18 - KASEY CHAMPION
5
Linear Probing
CSE 373 SP 18 - KASEY CHAMPION
6
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Insert the following values into the Hash Table using a hashFunction of % table size and linear probing to resolve collisions
1, 5, 11, 7, 12, 17, 6, 25
1
5
11
7
12
17
6
25
Linear Probing
CSE 373 SP 18 - KASEY CHAMPION
7
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Insert the following values into the Hash Table using a hashFunction of % table size and linear probing to resolve collisions
38, 19, 8, 109, 10
38
19
8
8
109
10
Problem:
Primary Clustering
When probing causes long chains of occupied slots within a hash table
Runtime
CSE 373 SP 18 - KASEY CHAMPION
8
Can we do better?
CSE 373 SP 18 - KASEY CHAMPION
9
Quadratic Probing
CSE 373 SP 18 - KASEY CHAMPION
10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
(49 % 10 + 0 * 0) % 10 = 9
(49 % 10 + 1 * 1) % 10 = 0
(58 % 10 + 0 * 0) % 10 = 8
(58 % 10 + 1 * 1) % 10 = 9
(58 % 10 + 2 * 2) % 10 = 2
89
18
49
Insert the following values into the Hash Table using a hashFunction of % table size and quadratic probing to resolve collisions
89, 18, 49, 58, 79, 27
58
79
(79 % 10 + 0 * 0) % 10 = 9
(79 % 10 + 1 * 1) % 10 = 0
(79 % 10 + 2 * 2) % 10 = 3
Problems:
If λ≥ ½ we might never find an empty spot
Infinite loop!
Can still get clusters
27
Now try to insert 9.
Uh-oh
Quadratic Probing
There were empty spots. What Gives?
Quadratic probing is not guaranteed to check every possible spot in the hash table
The following is true:
Notice we have to assume p is prime to get that guarantee
Secondary Clustering
CSE 373 SP 18 - KASEY CHAMPION
12
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Insert the following values into the Hash Table using a hashFunction of % table size and quadratic probing to resolve collisions
19, 39, 29, 9
39
29
19
9
Secondary Clustering
When using quadratic probing sometimes need to probe the same sequence of table cells, not necessarily next to one another
Probing
CSE 373 SP 18 - KASEY CHAMPION
13
Questions
Topics Covered:
CSE 373 20 SP – CHAMPION & CHUN
14
Double Hashing
int index = natrualHash % TableSize;
while (index in use) {
i++;
}
return index;
CSE 373 SP 18 - KASEY CHAMPION
15
<- Most effective if g(k) returns value relatively prime to table size
Second Hash Function
CSE 373 SP 18 - KASEY CHAMPION
16
Resizing: Open Addressing
Running Times
CSE 332 SU 18 – ROBBIE WEBER
In-Practice
Summary
CSE 373 SP 18 - KASEY CHAMPION
20
No clustering
Potentially more “compact” (λ can be higher)
Managing clustering can be tricky
Less compact (keep λ < ½)
Array lookups tend to be a constant factor faster than traversing pointers
Summary
Extra optimizations
CSE 373 SP 18 - KASEY CHAMPION
22
Other Hashing Applications
CSE 373 20 WI – HANNAH TANG
23
Cryptography
Hashing also ”hides” the data by translating it, this can be used for security
Fingerprinting
git hashes (“identification”)
Ad Tracking
YouTube Content ID
Caching
File Verification / Error Checking:
Binary Search Trees
CSE 373 22 SP – CHAMPION
24
Binary Trees
public class Node<K> {
K data;
Node<K> left;
Node<K> right;
}
CSE 373 SP 18 - KASEY CHAMPION
25
1
2
5
3
6
7
4
8
Tree Height
CSE 373 SP 18 - KASEY CHAMPION
26
1
2
5
7
7
overallRoot
overallRoot
overallRoot
null
Height = 2
Height = 0
Height = -1 or NA
Other Useful Binary Tree Numbers
h=3
For a binary tree of height h:
Binary Search Tree (BST)
CSE 373 SP 18 - KASEY CHAMPION
28
10
8
32
2
11
50
5
38
9
BST Ordering Applies Recursively
9
3
10
1
5
30
9
3
10
1
5
30
< 9
> 9
9
3
10
1
5
30
< 9
> 9
< 3 & < 9
> 3 & < 9
Aside Anything Can Be a Map
public class Node<K, V> {
K key;
V value;
Node<K, V> left;
Node<K, V> right;
}
1
aqua
a note about keys/maps
public class Node<K, V> {
K key;
V value;
Node<K, V> left;
Node<K, V> right;
}
Binary Trees vs Binary Search Trees:�containsKey(2)
32
11
9
50
8
2
5
10
38
10
8
32
2
11
50
5
38
9
Binary Tree vs. BST: containsKey(5)
10
9
1
3
2
30
14
5
9
3
10
1
5
30
2
14
Without BST Invariant
With BST Invariant
Nodes that
are searched
Binary Trees vs Binary Search Trees: containsKey(2)
if (node == null) {
9
2
1
3
6
5
7
4
8
10
12
14
11
15
13
BST containsKey runtime
if (node == null) {
9
2
1
3
6
5
7
4
8
10
12
14
11
15
13
For the tree on the right, what are some possible interesting cases (best/worst/other?) that could come up? Consider what values of key could affect the runtime
Is it possible to do worse than O(log n) 😈
1
2
3
4
…
15
containsKey(16)
BST different states
Perfectly balanced – for every node, its descendants are split evenly between left and right subtrees.
Degenerate – for every node, all of its descendants are in the right subtree.
9
2
1
3
6
5
7
4
8
10
12
15
14
11
13
1
2
3
4
15
…
Questions break -- Anything y’all want to review / restate?
So far:
How are we going to make this simpler / more efficient? Let’s enforce some invariants!
Invariants
Avoiding 𝚹(n) Behavior
Root Balanced: The root must have the same number of nodes in its left and right subtrees
Recursively Balanced: Every node must have the same number of nodes in its left and right subtrees.
Root Height Balanced: The left and right subtrees of the root must have the same height.
Take 1 minute to consider this question and then we’ll move to breakouts to discuss! (See chat for tips on moving discussion along + general reminders. Note that you’ll be prepping now so you have stuff to say / questions to ask each other. We’re still experimenting w/ breakouts / trying more strategies with them to make them successful, thanks for your patience.)
Root Balanced: The root must have the same number of nodes in its left and right subtrees
Recursively Balanced: Every node must have the same number of nodes in its left and right subtrees.
Root Height Balanced: The left and right subtrees of the root must have the same height.
too weak
Root Balanced: The root must have the same number of nodes in its left and right subtrees
too strong
Recursively Balanced: Every node must have the same number of nodes in its left and right subtrees.
too weak
Root Height Balanced: The left and right subtrees of the root must have the same height.
Invariant Lessons
Roadmap
Avoiding the Degenerate Tree
AVL invariant: For every node, the height of its left subtree and right subtree differ by at most 1.
An AVL tree is a binary search tree that also meets the following invariant
Practice w AVL invariants
AVL invariant: For every node, the height of its left subtree and right subtree differ by at most 1.
Is this a valid AVL tree?
4
5
2
7
3
9
8
10
6
Are These AVL Trees?
6
4
2
7
3
9
8
10
5
4
5
2
7
3
9
8
10
6
Insertion
1
2
3
1
2
3
Left Rotation
x
y
z
Rest of the tree
UNBALANCED
Right subtree is 2 longer
A
B
C
D
x
y
z
Rest of the tree
A
B
C
D
BALANCED
Right subtree is 1 longer
6
8
1
3
10
9
7
2
4
5
11
6
8
1
3
10
9
7
2
4
5
11
9
7
4
8
6
5
1
3
2
10
11
Meme break (it’s from some marvel movie that I haven’t watched -- you’re not alone if you don’t get this reference)
Right rotation
1
2
3
1
2
3
Just like a left roation, just reflected.
It Gets More Complicated
1
3
2
Can’t do a left rotation
Do a “right” rotation around 3 first.
1
3
2
Now do a left rotation.
1
2
3
There’s a “kink” in the tree where the insertion happened.
Right Left Rotation
x
z
y
Rest of the tree
A
B
C
D
x
y
z
Rest of the tree
A
B
C
D
BALANCED
Right subtree is 1 longer
UNBALANCED
Right subtree is 2 longer
Left subtree is
1 longer
AVL Example: 8,9,10,12,11
CSE 373 SU 18 – BEN JONES
60
8
9
10
AVL Example: 8,9,10,12,11
CSE 373 SU 18 – BEN JONES
61
8
9
10
AVL Example: 8,9,10,12,11
CSE 373 SU 18 – BEN JONES
62
8
11
9
10
12
AVL Example: 8,9,10,12,11
CSE 373 SU 18 – BEN JONES
63
8
11
9
10
12
AVL Example: 8,9,10,12,11
CSE 373 SU 18 – BEN JONES
64
8
9
10
11
12
How Long Does Rebalancing Take?
How Long Does Rebalancing Take?
6
8
1
3
10
9
7
2
4
5
11
9
7
4
8
6
5
1
3
2
10
11
Deletion