#Implementing a HashSet
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
public class HashTable {
private final LinkedList<String>[] array;
private int size = 0;
private final int DEFAULT_LENGTH = 10;
public HashTable() {
array = new LinkedList[DEFAULT_LENGTH];
for (int i = 0; i < DEFAULT_LENGTH; i++) {
array[i] = new LinkedList<>();
}
}
public boolean add(String val) {
if (!contains(val)) {
int hash = val.hashCode();
int index = hash % array.length;
array[index].add(val);
size++;
return true;
}
return false;
}
}```
Seems pretty good
Note that hashtables switch from linkedlists to trees if the no of elements in that node exceed 9
U can implement that if you wish to
what if we were to increase the size of array? that should reduce the number of collisions or is it for larger number of inputs?
It looks good in terms of functionality, just remember to make DEFAULT_LENGTH static
Increasing the size of the array can help reduce the number of collisions and improve the performance actually (because there's more buckets to reduce the collisions)
right that slipped my mind, thanks
not familiar with trees implementation yet, so this should come handy 🙂
You can look at HashMap as that does, it switches to a balanced binary tree from a linkedlist
it does that because its faster to search through i.e. O(log n) vs O(n) at some overhead cost (which I think is slower insertions and removals?)
i'll look at HashMap's source code, trees are on my list. For now can't really comment on that😅
Yeah that's fine, hopefully you have some things to look into now
are you good with trees?
i have mostly struggled with understand trees, i mean i understand the basic concepts like how it's all functioning just still find myself struggling to get comfortable around it
at best i think i may have solved like 3 problems on it
yes it would
actually it depends on the class
binary tree is pretty straightforward
how the hashcodes are scatterred
1
/ \
2 3
/ \ \
4 5 6
yes that's right, i wasn't sure if it used a binary tree or a trie
so a better algo for hashing would matter as well
yes that too
hash map does this
the xor would scatter the hashes by quite a lot
final void treeifyBin(Node<K,V>[] tab, int hash) {
int n, index; Node<K,V> e;
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
resize();
else if ((e = tab[index = (n - 1) & hash]) != null) {
TreeNode<K,V> hd = null, tl = null;
do {
TreeNode<K,V> p = replacementTreeNode(e, null);
if (tl == null)
hd = p;
else {
p.prev = tl;
tl.next = p;
}
tl = p;
} while ((e = e.next) != null);
if ((tab[index] = hd) != null)
hd.treeify(tab);
}
}
```It then converts an LL to a Binary tree like this
and note that it uses a singly linked list, doubly is not needed here
what does treeify(tab) does here?
transforms any unbalanced subtrees into balanced ones
starting from the head, it does it recursively
ah