1 of 59

CSE 373 SU23 Section 4

Trees

2 of 59

Agenda

  • Announcements

  • AVL Tree Review

  • Tries

3 of 59

Announcements

Mon (7/10)

Tues (7/11)

Wed (7/12)

Thurs (7/13)

Fri (7/14)

Sat (7/15)

EX2 due @ 11:59 pm

EX2 late due @ 11:59 pm

Mon (7/17)

Tues (7/18)

Wed (7/19)

Thurs (7/20)

Fri (7/21)

Sat (7/22)

EX3 due @ 11:59 pm

P2 due @ 11:59 pm

EX3 late due @ 11:59 pm

  • EX2 due this Wednesday
  • EX3 released and due next Wednesday
  • P2 due next Friday

4 of 59

MicroTeach: AVL Trees

5 of 59

Review: Binary Search Trees

Binary Search Trees (BSTs) are sorted trees, where each node has a left and right child

BST Invariants

  • for any node n:
    • all values in it’s left subtree are less than n
    • all values in its right subtree are greater than n

6 of 59

But, a problem...

Normal BSTs can become unbalanced.��So find is slow.

7 of 59

What exactly does balanced mean?

For every node, compare the heights of its two sub trees.

The tree is balanced when the difference in height between sub trees is no greater than 1 for every node.

Note: Balanced tree with n items has height of O(log(n)).

8 of 59

Solution: AVL Trees

These special BSTS remain balanced no matter what order items are inserted in.��They do this by rebalancing themselves using rotations each time an item gets inserted.

9 of 59

Don’t forget to check out the AVL rotation walkthrough too!

10 of 59

Question 1A-C: Valid BST/AVL

11 of 59

Problem 1A

6

Valid BST?

7

43

2

12 of 59

Problem 1A

Valid BST?

No!

6

7

43

2

13 of 59

Problem 1A

Valid AVL?

6

7

43

2

14 of 59

Problem 1A

Valid AVL?

No!

(Must be BST to be AVL!)

6

7

43

2

15 of 59

Problem 1B

Valid BST?

6

7

43

42

59

63

11

21

16 of 59

Problem 1B

Valid BST?

Yes!

6

7

43

42

59

63

11

21

17 of 59

Problem 1B

Valid AVL?

6

7

43

42

59

63

11

21

18 of 59

Problem 1B

Valid AVL?

No!

42 violates

Balance condition.

6

7

43

42

59

63

11

21

19 of 59

Problem 1C

Valid BST?

6

7

43

21

59

63

11

42

20 of 59

Problem 1C

Valid BST?

Yes!

6

7

43

21

59

63

11

42

21 of 59

Problem 1C

Valid AVL?

6

7

43

21

59

63

11

42

22 of 59

Problem 1C

Valid AVL?��Yes!

6

7

43

21

59

63

11

42

23 of 59

Problem 5A-C: AVL Big-O

24 of 59

5A: BigO

Write down a tight big-O for each of the following. Unless otherwise noted, give a bound in the worst case

(a) Insert and find in a BST

25 of 59

5A: Big O

(a) Insert and find in a BST.

Worst Case: Linked List like Tree (Degenerate Tree)

Insert: Inserting biggest/smallest item at the bottom of the tree = O(n)

Find: Finding Biggest/smallest item in the skewed binary tree = O(n)

26 of 59

5B: Big O

Write down a tight big-O for each of the following. Unless otherwise noted, give a bound in the worst case

(b) Insert and find in AVL Tree

27 of 59

5B: Big O

(a) Insert and find in AVL Tree

Let’s think about worst case: Insert/find at the bottom of our tree

Insert: Balanced tree has a height of log(N) = O(log n)

Find: Balanced tree has a height of log(N) = O(log n)

28 of 59

5C: Big O

Write down a tight big-O for each of the following. Unless otherwise noted, give a bound in the worst case

(c) Finding the minimum value in an AVL tree containing

N elements

29 of 59

5C: Big O

(c) Finding the minimum value in an AVL tree containing N elements

Find: AVL tree is balanced.

Find value by starting from root and traverse to the left most branch = O(log n)

30 of 59

Problem 6A-C: Analyzing Dictionary Implementations

31 of 59

Problem 6a)

a) What are the constraints on the data types you can store in an AVL Tree?

Keys needs to be comparable!�

Since AVL trees maintain their keys in sorted order, we have to be able to compare keys to decide whether to go left or right at each node.

Note that there are no restrictions on our value’s data type since AVL trees order data by keys, not values.

32 of 59

Problem 6b)

b) When is using an AVL tree preferred over a hash table?

*Caveat: In practice, you avoid this worst case scenario by

having a good hash function.

One Example: Better worst case for get()! AVL trees guarantee O(log n) at all times. A hash table in the worst case, where every key is added to the same bucket, takes O(n) time.

33 of 59

Problem 6c)

c) When is using a BST preferred over an AVL tree?

  • BSTs are waaaaaay easier to implement and debug!
  • Keys are inserted in a presumably random order such that the BST remains balanced.
    • Avoids runtime to check invariants and perform rotations.

34 of 59

MicroTeach: Tries

35 of 59

36 of 59

Trie (Pronounced like “try”)

A Data Structure that represents the dictionary or set ADT

In order for a Trie to implement one of those ADTs, it needs the following functionalities:

  • Adding an element
  • Finding an element
  • Removing an element

37 of 59

Adding to a Trie

s

add(“sap”)

s

a

s

a

p

38 of 59

Adding to a Trie

add(“sad”)

s

a

p

s

a

p

d

39 of 59

Adding to a Trie

add(“awls”)

s

a

p

d

a

s

a

p

d

a

w

s

a

p

d

a

w

l

s

a

p

d

a

w

l

s

40 of 59

Adding to a Trie

add(“a”)

s

a

p

d

a

w

l

s

s

a

p

d

a

w

l

s

41 of 59

Searching in Tries

42 of 59

Removing from a Trie: Lazy Deletion

remove(“awls”)

s

a

p

d

a

w

l

s

m

e

s

a

p

d

a

w

l

s

m

e

43 of 59

Removing from a Trie: Normal Deletion

remove(“awls”)

s

a

p

d

a

w

l

s

m

e

s

a

p

d

a

w

l

m

e

s

a

p

d

a

w

m

e

s

a

p

d

a

m

e

44 of 59

Trie Problem 1: Trie to Delete 0’s and 1’s?

45 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

  1. If we deleted all binary numbers of length 2, how many nodes would we have to delete?

46 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

  1. If we deleted all binary numbers of length 2, how many nodes would we have to delete? 0, since we still have pointers to the nodes for length-3 binary strings

47 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

  1. If we deleted all binary numbers of length 2, how many nodes would we have to delete? Ans – 0, since we still have pointers to the nodes for length-3 binary strings

48 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

(b) After part a, if we deleted all binary numbers of length 3, how many nodes would we have to delete?

49 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

(b) After part a, if we deleted all binary numbers of length 3, how many nodes would we have to delete? 12 (delete all nodes representing strings of length 2 and length 3)

50 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

0

0

0

0

1

1

1

1

1

1

1

(b) After part a, if we deleted all binary numbers of length 3, how many nodes would we have to delete? Ans: 12 (delete all nodes representing strings of length 2 and length 3)

51 of 59

Trie to Delete 0’s and 1’s (TA)

0

0

0

1

1

1

(b) After part a, if we deleted all binary numbers of length 3, how many nodes would we have to delete? 12 (delete all nodes representing strings of length 2 and length 3)

52 of 59

Trie to Delete 0’s and 1’s (TA)

0

1

(b) After part a, if we deleted all binary numbers of length 3, how many nodes would we have to delete? 12 (delete all nodes representing strings of length 2 and length 3)

53 of 59

Problem 2a-c): Call Me Maybe

54 of 59

Problem 2a)

a) Suppose you want to transfer someone’s phone book to a data structure so that you can access all the phone numbers with a particular area code efficiently. What data structure would you use? How would you implement it? There are a few answers here.

  • We could use a HashMap where the keys are area codes and values are a list of corresponding phone numbers. We would have to parse each phone number for its area code and identify all keys with that area code.

  • We could also use a Trie where we use phone numbers as our Trie “routes”. We would insert all phone numbers into our Trie and visit all children of the node corresponding to a particular area code.

55 of 59

Problem 2b)

b) What is the (in-practice) time complexity of your solution?

  • HashMap:
    • θ(n) to insert all of our phone numbers
    • θ(e) to look up all of the phone numbers with a particular area code where e is the number of phone numbers with that area code

  • Trie:
    • θ(n) to insert all of our phone numbers
    • θ(e) to look up all of the phone numbers with a particular area code

56 of 59

Problem 2c)

c) What is the space complexity?

  • HashMap:
    • O(n), where n is the number of phone numbers

  • Trie:
    • O(n), where n is the number of phone numbers
    • In theory, though, Tries will be more space-efficient as Tries will store near-duplicate phone numbers in less space than a HashMap

57 of 59

Problem 3): Let’s Trie to be Old School

58 of 59

Problem 3)

59 of 59

Problem 3)

  • We can use a Trie where the routes/branches are represented by the digits and the node’s values are a collection of words.

  • To populate the Trie, we would iterate through each word in the dictionary, convert the word into the corresponding sequence of number, and finally use that sequence as the key/route to traverse the Trie and add the word.