I can not figure out how to get rid of duplication of keys in the implementation of the Fibonacci pyramid. The fact is that the key can be deep, and we add a new element right to the root, compact it only later, how can we get rid of duplication?

    1 answer 1

    Often, when implementing a data structure, you can get rid of duplicate keys with the following trick: let's not store duplicate keys in the structure at all, but just for each key we will additionally store how many such keys lie in the structure.

    Then the add / delete operations will change a bit: 1. When adding, you must first try to find the necessary key in the structure. If you find it, then simply increase the counter to it. Not found - add a new key to the structure, set the counter to one. 2. When deleting, we find the existing key in the structure, reduce the counter, and remove the key from the structure only if the counter has dropped to zero.

    If you need to store any additional data along with the keys, you can replace the counter with a coherent list of these additional data.