CSE 373 SP25 Section AI Week 4
Isomorphism
Check-in
What study tips or rituals do you have?
Exam Details
Micro-Teach: 2-3 Trees, LLRBs, and Tries/TSTs
2-3 Tree
2-3 Tree Construction, Insertion, and Search
General Flow
Isomorphism between 2-3 Tree and LLRB Tree
Micro-Teach: one 3-node to two binary-tree nodes
1
Steps (intuitively):
3
5
2 4
Original VALID 2-3 Tree:
1
3
5
Step 1:
2
4
1
3
5
Step 2:
2
4
1
3
5
Step 3:
2
4
Always a valid LLRB!
Red
LLRB
LLRB invariants:
a self-balancing BST
LLRB invariants:
must be BST
Trie
What operations does a Trie optimize for?
Contains:
[a, awls, sad, sam, same, sap]
TST
Invariants:
Trie vs TST
Trie | TST |
Faster lookup, no comparison logic | Support BST-like ordering, requires character comparison at each node |
Memory-heavy, large branching factor | More space efficient with 3-child constraint |
Works best with small, overlapping data | More flexible for large/ sparse datasets |
Worst Case Height: L | Worst Case Height: N + L |
Question 1: Invalid LLRBs
The following LLRB trees are invalid, meaning they break an invariant. Why?
Any Questions?
Question 2: Inserting into a Balanced Tree
Draw the left-leaning red-black tree that results after calling insert(25) using both of the following approaches:
Use scratch paper on back sheet as needed.
red
red
red
20
10
40
17
5
30
12
Inserting into a Balanced Tree (Solution #1 - Rotations)
Draw the left-leaning red-black tree after inserting 25.
20
10
40
17
5
30
12
Invariants:
25
Inserting into a Balanced Tree (Solution #1 - Rotations)
Draw the left-leaning red-black tree after inserting 25.
20
10
40
17
5
30
12
Invariants:
25
Inserting into a Balanced Tree (Solution #1 - Rotations)
Draw the left-leaning red-black tree after inserting 25.
Invariants:
20
10
40
17
5
30
12
25
Inserting into a Balanced Tree (Solution #1 - Rotations)
Draw the left-leaning red-black tree after inserting 25.
Invariants:
All invariants met!
20
10
40
17
5
30
12
25
Inserting into a Balanced Tree (Solution #2 - 2-3 tree)
Inserting into a Balanced Tree (Solution #2 - Expanded)
Design Decisions: Malicious Website Tracker
Kevin is working at an internet security company and wants to keep track of malicious websites that users have reported. Websites are represented by a String URL.
Kevin wants to find common domain patterns (such as “.com” and “.org”) in the urls of malicious websites. Additionally, Kevin wants to support international domain names, which may contain unicode characters (such as “.भारत” or “.한국”).
NOTE: Suppose Kevin values space efficiency the most and is willing to sacrifice speed.
Malicious Website Tracker: Task
Kevin designs an ADT with the following functionality:
public void addURL(String url)
public int searchForSuffix(String suffix)
For example, suppose the following urls have been added:
addURL(“washington.edu”)
addURL(“evilsite.com”)
addURL(“notNetflix.com”)
addURL(“korea.한국”)
addURL(“government.org”)
Then searchForSuffix(“.com”) would return 2
Hint: What data should a node store as fields?
Malicious Website Tracker: Possible Solution
Using a TST that allows for any Unicode Characters:
public void addURL(String url)
public int searchForSuffix(String suffix)
Any Questions?
Code Challenges: Tries
Why LeetCode Style Code Challenges?
Problems + Approaches
Each group (3-4) will be assigned one of the following problems:
Different Approaches to Use:
For instructions, search online for the problem by ID number, e.g. “LeetCode 219”.
Different Approaches Available - Leetcode 3042
Different Approaches Available - Leetcode 14
Step-by-Step Process
NO NEED to write up code for this activity.
Try to come up with solution approaches and how you would tackle the program on the high-level using specific data structures.
You’ll have about 20 minutes to complete:
Let's get started!
Think individually for 2 min
Group discussion for 4 min (no PC)
Group discussion for 4 min (with PC)
Presentation prep for 4 min
Presenting to another group for 6 min
Presentation Guidelines
Questions to Ask Yourself:
Presentation Guidelines
Complete the reflection activity in Gradescope and add your groupmates!
Trie Solution - Longest Common Prefix (14)
Trie Solution - Count Prefix and Suffix Pairs (3042)
Closing Announcements
Please don't hesitate to reach out if you have any
questions or concerns about the course, or if there's
anything else we can help you with! We're here to support
you however we can!