Trie Data Structure Implementation (LeetCode)

AlgosWithMichael • August 20, 2020
Video Thumbnail
AlgosWithMichael Logo

AlgosWithMichael

View Channel

About

Hi! My name is Michael. I make coding tutorials to help people pass their technical interviews. I release videos each week and plan to branch out to project videos as well so stay tuned!

Video Description

📚 Trie Deep Dive Video - https://www.youtube.com/watch?v=K5gYn7qL3lE 🎧 Join the community Discord: https://discord.gg/aVWsAaaCtT 💰 Support me on Patreon: https://www.patreon.com/michaelmuinos 🔗Follow me on LinkedIn: https://www.linkedin.com/in/michael-muinos 📂Follow me on Github: https://github.com/MichaelMuinos Check out my interview prep platform for learning the patterns! 📢 Interview Prep Platform: https://algoswithmichael.com 00:00 - Intro 00:19 - Trie Summary 00:44 - Problem Description 01:30 - Example Walk Through 03:55 - Code Walk Through 10:53 - Time and Space Complexity A trie is a tree data structure where the nodes store letters inside of the alphabet. When these nodes are connected, they form words in which then you can use this structure to search prefixes and full words in an efficient time. For this problem, we must implement a trie data structure by completing the functions insert, search, and startsWith. Our insert function will add a word inside of the trie. The search function will return true or false as to whether a word is inside. Finally, the startsWith function will return true or false if a prefix is inside of the trie. Since we only have to worry about words containing lowercase letters, we can create an array of nodes of size 26 where each index will be responsible for storing an individual lowercase letter. To implement insertion, we will have node called 'curr' which starts at our root node. We move this 'curr' node down the branches of our tree creating nodes along the way for each character in our word. Our search and startsWith functions will have very similar logic where we move down the branches and return the last character in the word we are looking for. The time complexity for all 3 functions will be big oh O(M) where M is the number of characters we have in our word. We must traverse M nodes in the worst case when performing these functions. The space complexity of insert will be big oh O(M) because we must create M nodes per insert. Finally, our space complexity of search and startsWith will be big oh O(1) constant time since we do not initialize extra memory in those functions. ---------------------------------------------------- LAKEY INSPIRED - Blue Boi https://soundcloud.com/lakeyinspired https://www.youtube.com/channel/UCOmy8wuTpC95lefU5d1dt2Q

You May Also Like

Kitchen Essentials for Speedy Meals

AI-recommended products based on this video

Loading...
SwissQlip Deep Carry Pocket Clip Compatible with 91mm Victorinox Swiss Army Knife Models (Black)

SwissQlip Deep Carry Pocket Clip Compatible with 91mm Victorinox Swiss Army Knife Models (Black)

(813)
$63.94$32.77
FREE delivery Jun 23 - 27
Loading...
Victorinox Huntsman Swiss Army Knife, 15 Functions, Swiss Made Pocket Knife with Large Blade, Screwdriver, Scissors and Wood Saw - White

Victorinox Huntsman Swiss Army Knife, 15 Functions, Swiss Made Pocket Knife with Large Blade, Screwdriver, Scissors and Wood Saw - White

(645)
$66.99
$7.09 delivery Jul 8 - 29
Loading...
Victorinox Swiss-Army-Cutlery Rosewood Curved Boning Knife, Semi-Stiff Blade, 6-Inch

Victorinox Swiss-Army-Cutlery Rosewood Curved Boning Knife, Semi-Stiff Blade, 6-Inch

(1,453)
$128.36
Loading...
Victorinox Swiss Army Multi-Tool, Evolution Pocket Knife

Victorinox Swiss Army Multi-Tool, Evolution Pocket Knife

(5,356)
$152.86
FREE delivery Jun 18 - 19
Loading...
Lip Stain Peel Off Kit Tint Long Lasting Gloss Sets Colored Colors Nonstick Cup Tear Lipsticks Makeup For Women Girls,2.5ml Girls Clear1012 (A, One Size)

Lip Stain Peel Off Kit Tint Long Lasting Gloss Sets Colored Colors Nonstick Cup Tear Lipsticks Makeup For Women Girls,2.5ml Girls Clear1012 (A, One Size)

(0)
$5.69
$1.99 delivery Jun 13 - 18
Loading...
Victorinox Swiss Army Classic SD Pocket Knife, Camo

Victorinox Swiss Army Classic SD Pocket Knife, Camo

(45,533)
$33.00
$4.99 delivery Jun 19 - 25
Loading...
Canoly Cold Press Juicer, 350W Slow Juicer Machine with 4.5" Wide Feed Chute, Masticating Juicer Supports Whole Fruits & Vegetables, Reverse Function, Easy to Clean with Brush, Space Black

Canoly Cold Press Juicer, 350W Slow Juicer Machine with 4.5" Wide Feed Chute, Masticating Juicer Supports Whole Fruits & Vegetables, Reverse Function, Easy to Clean with Brush, Space Black

(564)
$123.49
500+ bought in past month
Loading...
Liahelianx Mini Citrus Press - Handheld Lemon & Lime Juicer with Screw Top Lid, Compact Manual Juice Extractor for Cocktails, Drinks, Kitchen - Durable, Easy Clean, Portable Fruit Squeezer

Liahelianx Mini Citrus Press - Handheld Lemon & Lime Juicer with Screw Top Lid, Compact Manual Juice Extractor for Cocktails, Drinks, Kitchen - Durable, Easy Clean, Portable Fruit Squeezer

(0)
$7.69$6.45
FREE delivery Jun 18 - 20
Loading...
3 4 5 Year Old Girl Gifts, Exssary Kids Knife Set for Real Cooking Toys for 3 4 5 6-8 Year Old Girls Montessori Toddler Kitchen Tools Cutting Board Knives Birthday Gift Girls Toys Age 4-6 7-10 Pink

3 4 5 Year Old Girl Gifts, Exssary Kids Knife Set for Real Cooking Toys for 3 4 5 6-8 Year Old Girls Montessori Toddler Kitchen Tools Cutting Board Knives Birthday Gift Girls Toys Age 4-6 7-10 Pink

(68)
$23.79
FREE delivery Sat, Jun 14 on your first order
100+ bought in past month