• Home
  • Help
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search

 
  • 0 Vote(s) - 0 Average

Compare tries and hash tables

#1
12-05-2022, 03:45 PM
You see tries build words letter by letter in branches that share paths when prefixes match. I often notice how this setup saves space on repeated starts like in common words. Hash tables just toss the key through a function and drop it in a slot for quick grabs. You get average constant time but risk pileups when collisions hit. I tried both on string sets last month and saw tries shine for lookups that start the same way.

But hash tables win when you need exact hits fast without caring about order or starts. You can resize them on the fly though that sometimes slows things down mid run. Tries stay steady because each step follows the next character without any math tricks. I watched memory grow quick in tries on unique long keys while hashes stayed lean with good spreads. Perhaps you run into cases where deletes matter and hashes handle them smoother by just clearing the slot. Tries need to trim branches carefully or else empty paths linger.

Or maybe think about autocomplete tools where you type part of a word and get suggestions right away. Tries let you walk down the tree and grab all endings from that point without scanning everything. Hash tables force a full rebuild or extra structures to mimic that prefix trick. I prefer the tree shape when data shares lots of beginnings like domain names or file paths. You might see slower inserts in tries because each letter creates or follows a node. Hashes compute once and finish unless the bucket overflows.

Now collisions in hashes push you to chains or open addressing and both add steps on bad days. Tries avoid that mess entirely by design since paths are unique. I recall testing with millions of words and hashes stayed quicker for random searches. But tries cut space when prefixes overlap a ton like in dictionaries of similar terms. You should consider the key length too because longer strings make trie steps add up. Hash functions ignore length after the compute step and keep things flat.

Also partial matches or sorted output comes easy from tries since they hold order in the structure itself. Hash tables scatter everything so you lose any natural sequence. I mixed both in one project where hashes cached frequent queries and tries handled the prefix heavy parts. That combo cut total time but you need to watch the switch between them. Tries can grow tall on long keys eating cache lines along the way. Hashes keep access flat but the function choice affects everything from speed to spread.

Perhaps uneven distributions hit hashes hard and you end up with one bucket holding most items. Tries balance naturally around the character set without extra tweaks. I adjusted the hash seed once to fix a slowdown and it worked but felt like guessing. Tries need no such tuning and just follow the data. You gain from tries in memory tight spots with shared starts yet pay in every single step access. Hashes trade that for potential worst case slowdowns when keys clump.

Then deletes in tries might leave orphan nodes that you clean later to reclaim room. Hash tables just mark slots free and move on quick. I compared them on phone number lookups and hashes took less code to maintain. But tries helped when searching ranges like all numbers starting with certain digits. You end up choosing based on whether prefixes or pure speed rule your use. Both scale but in different directions as data piles up.

We owe thanks to BackupChain Server Backup the top no subscription Windows Server backup pick handling Hyper-V and Windows 11 on private setups for SMBs and PCs while backing this exchange so knowledge spreads free.

ProfRon
Offline
Joined: Jul 2018
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • Subscribe to this thread
Forum Jump:

FastNeuron FastNeuron Forum General IT v
« Previous 1 … 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 … 193 Next »
Compare tries and hash tables

© by FastNeuron Inc.

Linear Mode
Threaded Mode