5

Since splay trees are used for caching I was wondering what are the advantages of Splay Tree over HashTable when I want to cache efficiently?

When should I prefer splay tree over hash table?

I guess it is a more specialized case than BST, there please don't link to BST vs Hashtable answer.

Oleg
  • 1,479
  • 3
  • 21
  • 42
denis631
  • 1,765
  • 3
  • 17
  • 38

1 Answers1

1

It really depends on what you mean by efficiency.

If its on the storage/space consumed by the data structure, they would be the same. On the other hand, when we talk about time complexity, it would still depend on how your data structure is being used.

If your users per say, use your data structure in a way that they will be accessing very similar data (related data everytime), caching using the splay tree would be great since its worst case revolves around O(log n), whereas a hashtable has a worst case of O(n).

An instance where a hash table would work really bad is when your hashes are stored in less than 3 buckets or worse just 1 bucket or chain or thread. I suppose you could imagine what happens when you try to access the data.

but in general when it comes to caching, Hash table would work very well since it has an average time complexity of O(1).

you could also think of it this way. If what you want is to make your data structure access the "most recent"/"most accesssed" data faster then you could consider working with splay trees. but if you want your data structure to be able to access different kinds of data at an ease then you would want to consider using the hash table.

Also you would want to note that it is very important to make sure to choose a good hash function, table size and data structure to use in a bucket to make it work well with your use case.

In fact combining the two could also work :)

This is really just based on my opinion so I hope I helped! It's really a very shallow comparison, I suggest you just google how both works when it comes to caching and make your own comparison! Kudos!

Vincent Pakson
  • 1,891
  • 2
  • 8
  • 17
  • but doesn't hash table require more space, in the sense that HashMap is not full to 100%? – denis631 Nov 28 '17 at 09:15
  • by efficiency I mean when I would use one over another. Splay tree is a nice data structure, but when would I use it, if HashMap just beats it? – denis631 Nov 28 '17 at 09:18
  • hmm. Theoretically they both use the same amount of space acutally. I don't really understand what you meant by "not full to 100%". – Vincent Pakson Nov 28 '17 at 09:20
  • ill edit my answer to add an example where a hash tree would work really bad – Vincent Pakson Nov 28 '17 at 09:24
  • 1
    @VincentParkson due to randomness of the data and randomness of the hash-function the hash-table can have gaps in between. Meaning, if no data is hashed to bucket at index 2, then, well, it is empty. Therefore memory is not used to the fullest. – denis631 Nov 28 '17 at 09:34
  • well if thats the case, you're thinking of just using arrays. If you think about it, you could use structures similar to a Python's dictionary where you store the data in certain points generated by your hash function. you could do rehashing to restrict your hash function to a certain point in your array if you really want to use arrays. – Vincent Pakson Nov 28 '17 at 09:40
  • there are tons of work arounds for hash table's memory inefficiency – Vincent Pakson Nov 28 '17 at 09:41
  • while hash-table is inefficient space-wise, splay tree nodes require at least two pointers...so for a 1-byte char key, it takes 16 extra bytes in 64 bit system...(I think :() – Shihab Shahriar Khan Nov 28 '17 at 10:26