Software Development

Hash Desk vs Trie – GeeksforGeeks

Hash Desk vs Trie – GeeksforGeeks
Written by admin


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

What’s Hash Desk?

An array that shops tips to information equivalent to a given aspect. An entry within the hash desk is NIL if no current aspect has a hash perform worth equal to the index for the entry.  In easy phrases, we are able to say {that a} hash desk is a generalization of the array. Hash desk offers the performance through which a set of knowledge is saved in such a method that it’s straightforward to seek out these objects later if required. This makes trying to find a component very environment friendly.

Benefits of Hash Desk over Trie:

  • Simple to implement and perceive.
  • Hash gives higher synchronization than different information buildings.
  • Hash tables are extra environment friendly than search bushes or different information buildings.
  • Hash gives fixed time for looking, insertion, and deletion operations on common.
  • The system will have already got a well-optimized implementation sooner than tries for many functions.
  • Keys needn’t have any particular construction.
  • Extra space-efficient than the clearly linked trie construction

Disadvantages of Hash Desk over Trie:

  • Hash is inefficient when there are numerous collisions.
  • Hash collisions are virtually not be averted for a big set of doable keys.
  • Hash doesn’t permit null values. 

Functions of Hash Desk:

  • Hash is utilized in databases for indexing.
  • Hash is utilized in disk-based information buildings.
  • In some programming languages like Python, JavaScript hash is used to implement objects. 
  • Hash is used for cache mapping for quick entry to the information.
  • Hash can be utilized for password verification.
  • Hash is utilized in cryptography as a message digest.:

Complexity evaluation of Hash Desk:

  • Time for Insertion: O(1)
  • Time for Deletion: O(1)
  • Time for Looking out: O(1)

What’s Trie?

Trie information construction is outlined as a Tree based mostly information construction that’s used for storing some assortment of strings and performing environment friendly search operations on them. The phrase Trie is derived from retrieval, which suggests discovering one thing or acquiring it.

Trie follows some property that If two strings have a typical prefix then they are going to have the identical ancestor within the trie. A trie can be utilized to type a set of strings alphabetically in addition to search whether or not a string with a given prefix is current within the trie or not.

Benefits of Trie over Hash Desk:

  • Predictable O(n) lookup time the place n is the scale of the important thing
  • Lookup can take lower than n time if it’s not there
  • Helps ordered traversal
  • No want for a hash perform
  • Deletion is simple
  • You may rapidly search for prefixes of keys, enumerate all entries with a given prefix, and many others
  • We are able to effectively do prefix search (or auto-complete) with Trie.
  • We are able to simply print all phrases in alphabetical order which isn’t simply doable with hashing.
  • There isn’t a overhead of Hash capabilities in a Trie information construction.
  • Trying to find a String even within the massive assortment of strings in a Trie information construction might be accomplished in O(L) Time complexity, The place L is the variety of phrases within the question string. This looking time may very well be even lower than O(L) if the question string doesn’t exist within the trie.

Disadvantages of Trie over Hash Desk:

  • The primary drawback of the trie is that it takes a whole lot of reminiscence to retailer all of the strings. For every node, we’ve too many node pointers that are equal to the no of characters within the worst case.
  • An effectively constructed hash desk(i.e. a very good hash perform and an affordable load issue) has O(1) as lookup time which is method sooner than O(l) within the case of a trie, the place l is the size of the string.

Functions of Trie: 

  • Autocomplete Function: Autocomplete gives options based mostly on what you sort within the search field. Trie information construction is used to implement autocomplete performance.  
  • Spell Checkers: If the phrase typed doesn’t seem within the dictionary, then it exhibits options based mostly on what you typed.
    It’s a 3-step course of that features :
    • Checking for the phrase within the information dictionary.
    • Producing potential options.
    • Sorting the options with larger precedence on prime.
  • Trie shops the information dictionary and makes it simpler to construct an algorithm for looking the phrase from the dictionary and gives the listing of legitimate phrases for the suggestion.
  • Longest Prefix Matching Algorithm(Most Prefix Size Match): This algorithm is utilized in networking by the routing gadgets in IP networking. Optimization of community routes requires contiguous masking that certain the complexity of lookup a time to O(n), the place n is the size of the URL tackle in bits. To hurry up the lookup course of, A number of Bit trie schemes have been developed that carry out the lookups of a number of bits sooner.

Complexity evaluation of Trie:

  • Time for Insertion: O(N)
  • Time for Deletion: O(N)
  • Time for Looking out: O(N)

Examine the Lookup operation of HashTable vs Trie:

HashTable

  • An effectively constructed hash desk(i.e. a very good hash perform and an affordable load issue) has O(1) as a lookup.
  • It all the time takes the identical time and doesn’t care about whether or not the aspect is current or not.
  • It’s sooner than Trie.
  • It isn’t predictable.

Trie:

  • Trie has a lookup time of O(n) the place n is the scale of the important thing. 
  • Lookup can take lower than n time if it’s not there.
  • It’s slower than HashTable.
  • It’s predictable.

About the author

admin

Leave a Comment