I have to implement a sparse matrix (a matrix that has predominantly zeroes, so you only record values different than 0), but I have to implement it using a binary search tree.
So now I'm thinking of implementing it by using the line / column as a key, but what do I use as the root of that tree ?
I hoped once I researched binary search trees I would understand how this implementation would be beneficial, or at the very least possible, but I for the life of me can't figure it out.
I have tried google to no avail, and I myself cannot imagine how to even attempt in doing so.
I haven't decided on the language I shall be implementing this in yet, so I need no code examples, my problem is logic. I need to see how this would even work.
P.S. I have no idea what tags to use, if someone could edit some in, It'd be much appreciated.
preguntado el 12 de junio de 12 a las 14:06
To use a binary tree you need to have a key that is distinct for each (possible) entry in the matrix. So if you want to lookup (2, 4) in a matrix [100, 100] then the key could be something like "002004". With this key you can insert a value into the tree.
For each dimension the key would be longer, so you also might consider a hash function to hash the coordinates of the cell and within the tree you have a list of entries for this hash key. The tree is then only an index to the right list. Within the list you need to perform a sequential search then. Or if you order the list you could improve by using a binary search.