Kirill486 / functional-red-black-tree

A purely functional red-black tree data structure
MIT License
5 stars 1 forks source link

is there a reason why key type is fixed to "number" #9

Closed kavaro closed 3 years ago

kavaro commented 3 years ago

Thanks for making this Typescript version available.

I would like to use the tree with a custom key type (e.g. string) and a comparator function that compares custom keys.

Is there a way to achieve this ?

Thanks ...

Kirill486 commented 3 years ago

Yes, there's a good way to do so. It's called HashFunction.

type hashFunction =  (valueType: ValueType) => number;

const hash = hashFunction(somethingIWantToStore);
tree.insert(hash, somethingIWantToStore);

Here are a few hash functions for strings.

kavaro commented 3 years ago

A hash would for sure be a solution.

However with the javascript version the hash would not be required. By providing a custom compare function to the tree constructor one can use any datatype as key. Unless I am missing something ...

Kirill486 commented 3 years ago

12 Created