Implementando una matriz dispersa usando un árbol de búsqueda binario [cerrado]

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

I believe the term you want is not "rare" but "sparse". -

What language are you going to do this in? That tag will be the most helpful. -

Like I said I don't know yet, I think python would be quicker than c++. But like I said not 100% sure -

python is quicker ... implementation speed not performance. -

@schoetbi While I did not say that outloud it is what I meant, and since my only tag is "homework" I thought it was obvious implementation speed is my only concern. -

1 Respuestas

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.

Respondido el 12 de junio de 12 a las 14:06

So if I wish to represent my matrix trough the triplet (line, column, value) where value != 0, I can use a BST (binary search tree) by saving the line and column as a single key ? What would I use as the root of the tree ? - Kalec

@kalec: yes, the key would be a hash of line and column. The value could be 0, you need to distingush between 0 and empty. Empty means there is no entry in the tree for this line, column tuple. The root could be the first node or for self balancing trees the root node will change during insertion of more cells. - Schoetbi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.