n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373...

32
CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona Kazi Keanu Vestil Howard Xiao Aaron Johnston Instructor TAs Siddharth Vaidyanathan LEC 09 BSTs, AVL Trees BEFORE WE START pollev.com/uwcse373 1. What’s the load factor? 2. Which of the following is a good choice of threshold for the load factor before we resize? n log n 1

Transcript of n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373...

Page 1: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

CSE 373

Timothy AkintiloBrian ChanJoyce ElauriaEric FanFarrell Fileas

Melissa HovikLeona KaziKeanu Vestil

Howard Xiao

Aaron JohnstonInstructor

TAs

Siddharth Vaidyanathan

L E C 0 9

BSTs, AVL Trees

BEFORE WE START

pollev.com/uwcse373

1. What’s the load factor?

2. Which of the following is a good choice of threshold for the load factor before we resize?

n log n 1

Page 2: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Announcements• Current Assignments: P2 (Due in 1.5 weeks), EX2 (Due Friday)

- Tonight is the late cutoff for P1, Wednesday is the late cutoff for EX1- P2: Iterator Overview video now available!

• Lectures are now also available via Panopto- Panoptos will be linked under each lecture on website within a few hours- Still available via Zoom tab on Canvas – use whichever one you prefer

• Anonymous feedback: sound quality (lecture, review videos)! Thanks for letting us know J

- For section videos: find higher-quality audio in video description

• Course Feedback survey sent tonight- Now that you’ve seen lectures, sections, projects, and exercises – please let

us know what’s working for you and what could be improved!

Page 3: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Exam I Logistics• You’ll have 48 hours (no time limit once you start

or anything. Just submit by the deadline!)• Released 7/24 12:01 AM PDT• Due 7/25 11:59 PM PDT

- No late submissions!

• Open notes and open friends: you’ll be able to work on and submit in groups• Covers content up through this Friday’s lecture

(Memory & Caching, B-Trees)• Will focus on conceptual questions. Be prepared

to explain why things are true and defend your reasoning

LEC 01 - 11

SEC 01 - 05 P 0 - 2

EX 1 - 2

EXAM I

• Topics list released tonight so you can start looking things over, more thorough review materials will be published later

• Great place to start: Learning Objectives at the beginning of every lecture!

STUDYING

Page 4: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Learning Objectives

1. Describe the properties of a good hash function and the role of a hash function in making a Hash Map efficient

2. Evaluate invariants based on their strength and maintainability, and come up with the invariants for data structure implementations

3. Compare and contrast the properties and runtimes of BTs, BSTs, and AVL Trees

4. Describe the AVL invariant, explain how it affects AVL Tree runtimes, and compare it with the BST invariant

After this lecture, you should be able to...

Page 5: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Lecture Outline• Hash Map (Wrap-Up)

- Hashing & Applications

• Binary Trees & Binary Search Trees (BSTs)

• Case Analysis on BSTs

• Choosing a Good Invariant (AVL Trees)

Page 6: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

(7,blue) (77,aqua)

(4,orange)

Review Separate Chaining• If two values want to live in the same index,

let’s just let them be roommates!• Each index is a “bucket”

- Linked Nodes are a common implementation for these bucket “chains”

• When item x hashes to index h:- If bucket at h is empty, create new list with x- Else, add x to the list

• But if multiple keys can hash to the same index, need to store the key too!

0

1

2

3

4

5

6

7

8

9

(1,red) (21,pink)

(22,tan)

Page 7: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Review Separate Chaining... In Practice• A well-implemented separate chaining

hash map will stay very close to the best case

- Most of the time, operations are fast. Rarely, do an expensive operation that restores the map close to best case.

• How to stay close to best case?- Good distribution & Resizing!

• We can describe the “in-practice” case as what almost always happens:

- (1) items are fairly evenly distributed- (2) assume resizing doesn’t occur on this

particular operation- This is similar to the concept of “amortized”

Operation Case Runtime

put(key,value)

best Θ(1)

In-practice Θ(1)

worst Θ(n)

get(key)

In-practice Θ(1)

average Θ(1)

worst Θ(n)

remove(key)

best Θ(1)

In-practice Θ(1)

worst Θ(n)

Page 8: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Review When to Resize?• In ArrayList, we were forced to resize when we ran out of room

- In SeparateChainingHashMap, never forced to resize, but we want to make sure the buckets don’t get too long for good runtime

• How do we quantify “too full”?- Look at the average bucket size: number of elements / number of buckets

LOAD FACTOR λ

n: total number of key/value pairsc: capacity of the array (# of buckets)

𝜆 =𝑛𝑐

• If we resize when λ hits some constantvalue like 1:

- We expect to see 1 element per bucket: constant runtime!

- If we double the capacity each time, the expensive resize operation becomes less and less frequent

Page 9: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Hashing• What about non-integer data?

- Remember the definition -- Hash Function: any function that can be used to map data of an arbitrary size to fixed-size values.

HASH FUNCTION

“Melissa”

“Joyce”

“Howard”

• Considerations for Hash Functions:1. Deterministic – same input should generate the same output2. Efficient – reasonable runtime3. Uniform – inputs spread “evenly” across output range

504

1

9002

Page 10: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

HashingImplementation 1: Simple aspect of valuespublic int hashCode(String input) {

return input.length();}

Implementation 2: More aspects of valuepublic int hashCode(String input) {

int output = 0;for(char c : input) {

out += (int)c;}return output;

}

Implementation 3: Multiple aspects of value + math!public int hashCode(String input) {

int output = 1;for (char c : input) {

int nextPrime = getNextPrime();out *= Math.pow(nextPrime, (int)c);

}return Math.pow(nextPrime, input.length());

}

Pro: super fastCon: lots of collisions!

Pro: still really fastCon: some collisions

Pro: few collisionsCon: slower, gigantic integers

Page 11: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Hashing• Fortunately, experts have made most of these design decisions for us!

- All objects in Java have a .hashCode() method that does some magic to make a “good” hash for any object type (e.g. String, ArrayList, Scanner)

- The built-in hashCode() has a good distribution/not a lot of collisions

• More precisely, hashCode() just gets us an int representation: then we % by size

504

1

9002

01234

HASH FUNCTION

“Melissa”

“Joyce”

“Howard”

1. call key.hashCode() to get int representation of object

2. Mod (%) by the number of buckets to get our index

Page 12: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Content Hashing: Applications• Caching:

- You’ve downloaded a large video file. You want to know if a new version is available. Rather than re-downloading the entire file, compare your file’s hash value with the server’s hash value.

• File Verification / Error Checking:- Same implementation- Can be used to verify files on your machine,

files spread across multiple servers, etc.

• Fingerprinting- Git hashes (“identification”) - Ad tracking (“identification”): see https://panopticlick.eff.org/- YouTube ContentID (“duplicate detection”)

Page 13: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Content Hashing: Defining a Salient Feature• Hash function implementors can choose what’s salient:

- hash(“cat”) == hash(“CAT”) ???

• What’s salient in detecting that an image or video is unique?

• What’s salient in determining that a user is unique?

13

Page 14: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Review Iterators• Iterator: a Java interface that dictates how a collection of data should be

traversed. Can only move forward and in a single pass.

hasNext() – returns true if the iteration has more elements yet to be examined

next() – returns the next element in the iteration and moves the iterator forward to next item

ArrayList<Integer> list;

Iterator itr = list.iterator();while (itr.hasNext()) {

int item = itr.next();}

ArrayList<Integer> list;

for (int i : list) {int item = i;

}

Iterator Interface

BehaviorhasNext() – true if elements remain next() – returns next element

Two ways to use an iterator in Java:

Page 15: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

P2 Reminders• Implementing an iterator for a Hash Map is complex!

- You need to iterate through the elements of a bucket, but when you reach the end of the chain, have to move to the next bucket

- “you’re not iterating over some linear data structure, you’re playing 2D chess”– Howard Xiao

• Start early! P2 available for over 1.5 weeks, but for good reason!

- Especially the ChainedHashMap iterator

• Remember to read the entire Tips section of the instructions!

(7,blue) (77,aqua)

(4,orange)

0

1

2

3

4

5

6

7

8

9

(1,red) (21,pink)

(22,tan)

Page 16: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Lecture Outline• Hash Map (Wrap-Up)

- Hashing & Applications

• Binary Trees & Binary Search Trees (BSTs)

• Case Analysis on BSTs

• Choosing a Good Invariant (AVL Trees)

Page 17: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

143 Review Binary Trees• A binary tree is a collection of nodes where

each node has at most 1 parent and anywhere from 0 to 2 children

- Similar to linked lists (just add an extra child field!)

• Root node: the single node with no parent, “top” of the tree. Often called the ‘overallRoot’• Leaf node: a node with no children• Subtree: a node and all its descendants• Height: the number of edges contained in the

longest path from root node to any leaf node 17

1

2 5

3 6 7

4 8

public class Node<K> {K data;Node<K> left;Node<K> right;

}

Page 18: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

143 Review Tree Height• What is the height of the following binary trees?

- Height: the number of edges contained in the longest path from root node to any leaf node

18

2 Minutes

1

2 5

7

overallRoot

1

overallRoot overallRoot

null

height: 2 height: 0 height: -1 (or N/A)

Page 19: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Other Useful Binary Tree Numbers

Max number of leaves: 2%

Max number of nodes: 2%&' − 1

h=3

Min number of leaves: 1Min number of nodes: ℎ + 1

For a binary tree of height ℎ:

Page 20: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

143 Review Binary Search Tree (BST)• Invariants

- The rules for your data structure or algorithm- Whenever you implement any operation on your

data structure:- You know the invariants are true at the beginning.

Great! Simpler code, fewer cases- But you must leave the invariants true at the end.

- Accidentally violating invariants a common source of bugs. Defensive programming: check invariants at beginning/end of methods!

20

9

3 10

1 5 30

2 14

Binary Search Tree Invariant:For every node with key 𝑘:

• The left subtree has only keys smaller than 𝑘.

• The right subtree has only keys greater than 𝑘.

INVA

RIA

NT

public boolean containsKeyBST(node, key) {if (node == null) {

return false;} else if (node.key == key) {

...

return result;}

}

INVARIANT

INVARIANT

Page 21: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

BST Ordering Applies Recursively

9

3 10

1 5 309

3 10

1 5 30

< 9 > 9

9

3 10

1 5 30

< 9 > 9

< 3 & < 9 > 3 & < 9

Page 22: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Aside Anything Can Be a Map• Want to make a tree implement the Map ADT?

- No problem – just add a value field to the nodes, so each node represents a key/value pair.

public class Node<K, V> {K key;V value;Node<K, V> left;Node<K, V> right;

}

• For simplicity, we’ll just talk about the keys- Interactions between nodes are based off of keys (e.g. BST sorts by keys)- In other words, keys determine where the nodes go

1

aqua

Page 23: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Lecture Outline• Hash Map (Wrap-Up)

- Hashing & Applications

• Binary Trees & Binary Search Trees (BSTs)

• Case Analysis on BSTs

• Choosing a Good Invariant (AVL Trees)

Page 24: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

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 thatare searched

Page 25: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Binary Tree vs. BST: containsKey(5)

public boolean containsKeyBinaryTree(node, key) {if (node == null) {

return false;} else if (node.key == key) {

return true;} else {

return containsKeyBT(node.left) || containsKeyBT(node.right);

}}

public boolean containsKeyBST(node, key) {if (node == null) {

return false;} else if (node.key == key) {

return true;} else {

if (key <= node.key) {return containsKeyBST(node.left);

} else {return containsKeyBST(node.right);

}}

}

1 Halving the Input

RECURSIVE PATTERN

2 Constant size Input

RECURSIVE PATTERN

Θ (log n)

Where n is the number of nodes in the tree

Θ (n)

Page 26: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

pollev.com/uwcse373

BST containsKey runtimepublic boolean containsKeyBST(node, key) {

if (node == null) {return false;

} else if (node.key == key) {return true;

} else {if (key <= node.key) {

return containsKeyBST(node.left);} else {

return containsKeyBST(node.right);}

}}

For containsKey on a BST, what are some interesting cases for us to consider?

What’s the best? The worst? Are there other interesting cases?For example, consider what values of key could change the behavior on the above tree.

9

3 10

1 5 30

2 14

Page 27: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

BST containsKey Cases: Varying key

𝑇 𝑛 = $𝑇𝑛2+ 1 if 𝑛 > 1

3 otherwise

𝑇(𝑛) = Θ(log 𝑛)

9

3 10

1 5 30

2 14

For containsKey on a BST, what are some interesting cases for us to consider?

What’s the best? The worst? Are there other interesting cases?For example, consider what values of key could change the behavior on this tree.

We can analyze with recurrences:Best Case

Worst Case

key=9 seems pretty nice! Find it immediately. Θ(1)

key=100 is terrible. Have to search the entire tree (and you don’t even get the satisfaction of returning true). Θ(log n)

Page 28: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

• Sources of variation that affect the runtime:

Is it possible to do worse than 𝚯(𝐥𝐨𝐠𝒏)? 😈

Our new recurrence:

𝑇 𝑛 = $𝑇 𝑛 − 1 + 1 if 𝑛 > 13 otherwise

𝑇 𝑛 = Θ(𝑛)

Can we do worse by varying this?

Number of elements in the tree (n)

Position of searched-for key in tree

Arrangement of elements in tree

0

1

2

3

15

...

1

2

3

Page 29: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

BST Extremes• Here are two different extremes our BST could end up in:

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

0

1

2

3

15

...

Page 30: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Can we do better?• Key observation: what ended up being important was the height of

the tree!- Height: the number of edges contained in the longest path from root node to

any leaf node - In the worst case, this is the number of recursive calls we’ll have to make

• If we can limit the height of our tree, the BST invariant can take care of quickly finding the target

- How do we limit?- Let’s try to find an invariant that forces the height to be short

INVARIANT

INVARIAN

TINVA

RIAN

T

INVA

RIANT

Page 31: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

Lecture Outline• Hash Map (Wrap-Up)

- Hashing & Applications

• Binary Trees & Binary Search Trees (BSTs)

• Case Analysis on BSTs

• Choosing a Good Invariant (AVL Trees)

Page 32: n log n 1 BSTs, AVL Trees · 2020-07-14 · LEC 09: BSTs, AVL Trees CSE 373 Summer 2020 CSE 373 Timothy Akintilo Brian Chan Joyce Elauria Eric Fan Farrell Fileas Melissa Hovik Leona

CSE 373 Summer 2020LEC 09: BSTs, AVL Trees

In Search of a “Short BST” Invariant: Take 1• What about this?

BST Height InvariantThe height of the tree must not exceed Θ(logn)

INVA

RIA

NT

public void insertBST(node, key) {...

}

INVARIANT

INVARIANT

• This is technically what we want (would be amazing if true on entry)• But how do we implement it so it’s true on exit?

- Should the insertBST method rebuild the entire tree balanced every time? This invariant is too broad to have a clear implementation

• Invariants are tools – more of an art than a science, but we want to pick one that is specific enough to be maintainable

??