1 of 33

ADT and Tries

CSE 332 (Section 1)

Original slides by: Louis Maliyam Monty Nitschke

2 of 33

IceBreakers!!

3 of 33

Abstract Data Type

(ADT)

4 of 33

ADT Recap

  • An ADT is a mathematical description of a “thing” with operations on that “thing”
    • Expected behaviors (for a set of operations) are defined, NOT implementation
  • Operation vs. Behavior: Stack of lunch trays
    • Can push and pop trays from the top (operations)
    • Trays come on and off in LIFO order (behavior)
  • You likely have seen ADT before
    • Java Examples: List, Queue, Stack, Map, Set, etc.
    • List ADT can be implemented using array (ArrayList)�or linked list (LinkedList)
    • We expect to be able to insert, access, iterate, and�remove elements according to the order they appear�in the “list”�

5 of 33

By the end of the quarter, you will get a good sense of what data structure is being implemented under the hood in order to satisfy behaviors of an ADT

6 of 33

Problem 0

Discuss with your group and do as much as you can

7 of 33

Problem 0(a)

You’re designing a tool that checks code to verify that all opening brackets, braces, parentheses, have closing counterparts.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

8 of 33

Problem 0(a)

You’re designing a tool that checks code to verify that all opening brackets, braces, parentheses, have closing counterparts.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

- A stack allows us to process the most recent match first

-For 351 veterans, the Linked List solution presents cache locality issues, though both are asymptotically the same

9 of 33

Problem 0(b)

Disneyland has hired you to find a way to improve the processing efficiency of their long lines at attractions. There is no way to forecast how long the lines will be.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

10 of 33

Problem 0(b)

Disneyland has hired you to find a way to improve the processing efficiency of their long lines at attractions. There is no way to forecast how long the lines will be.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

-We would use a queue since we want the first person in line to ride first

-Linked list with only a front pointer is going to become very slow for a long line

-Can argue that the Linked List with both pointers is better since we won’t have to resize the array as the line length changes

11 of 33

Problem 0(c)

A sandwich shop wants to serve customers in the order that they arrived, but also wants to look ahead to know what people have ordered.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

12 of 33

Problem 0(c)

A sandwich shop wants to serve customers in the order that they arrived, but also wants to look ahead to know what people have ordered.

    • Stack or Queue ADT?
    • Array, Linked List with front, Linked List with front and back

-We would use a queue since we process in the order that customers arrive

-We would choose an Array since this would efficiently allow us to take a look at anybody's order. (Also, there's probably a maximum capacity of customers that can be physically in the shop, so we don’t even need to worry about resizing in the long term)

13 of 33

Hypothetical Situation

USPS wants a simple tool where people can type in a zip code and see the location it corresponds to.

  1. 98105 - Seattle, WA
  2. 98104 - Faketown, WA
  3. 98111 - Alsofake, WA

What data structure would you use?

14 of 33

Tries

15 of 33

16 of 33

Trie (Pronounced like “try”)

A Data Structure representing the ADT set

The set ADT supports the following operations:

  • Add
  • Find
  • Remove

17 of 33

Adding to a Trie

add(“sap”):

s

s

a

s

a

p

18 of 33

Adding to a Trie

add(“sad”):

s

s

a

a

p

p

d

19 of 33

Adding to a Trie

add(“awls”):

s

s

s

s

s

a

a

a

a

a

a

a

a

p

p

p

p

w

w

w

l

l

d

d

d

d

20 of 33

Adding to a Trie

s

s

a

a

p

w

l

d

add(“a”):

s

s

a

a

p

w

l

d

21 of 33

Searching in Tries

22 of 33

Removing from a Trie: Lazy Deletion

remove(“awls”):

s

s

a

a

p

w

l

d

m

e

s

s

a

a

p

w

l

d

m

e

23 of 33

Removing from a Trie: Normal Deletion

remove(“awls”):

s

s

a

a

p

w

l

d

m

e

s

a

a

p

w

l

d

m

e

s

a

a

p

w

d

m

e

s

a

a

p

d

m

e

24 of 33

Observation

The nodes in the trie currently only store if the key is in or not�(boolean value)

We can make it store arbitrary value, and use trie as a map

This is what you will do in P1

25 of 33

Problem 1

26 of 33

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?

27 of 33

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

28 of 33

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

29 of 33

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?

30 of 33

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)

31 of 33

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)

32 of 33

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? Ans: 12 (delete all nodes representing strings of length 2 and length 3)

33 of 33

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? Ans: 12 (delete all nodes representing strings of length 2 and length 3)