-1

I am implementing my own data structure for storing objects, these objects have an ID and also a date attached. The operations I must implement require me to sometimes return say an array in date order, or find an object by its ID.

How can I minimise the time complexity for both date and ID in this scenario, would the best approach be to have two separate trees and accept the storage complexity cost?

Any guidance and help is appreciated, thanks!

Harrison W.
  • 197
  • 11

1 Answers1

1

That's possible, but not useful, and very confusing (since — for example — you'll need separate methods for rebalancing a node with respect to one tree vs. the other, so you'll basically have to write two copies of your AVL tree implementation). Instead, you should have two separate trees, but their nodes can (and should) contain pointers (references) to the same objects. You can (and should) still wrap this up in a single object, so that client code doesn't have to worry about the existence of two underlying trees.

Note, by the way, that a pair of trees has the same asymptotic complexity as a single tree, because 2 is just a constant factor (and there are no more-than-polynomial complexities involved).

ruakh
  • 175,680
  • 26
  • 273
  • 307