0

Does lexical comparation of Dates (or DateTimes) offer any advantages when constructing Binary Search Trees over direct comparation of Dates (or DateTimes) when constructing the tree using the date as a key? The newer dates will always follow after the dates that were already added, which would result in a degenerate tree when comparing the dates. I would like to know if using a lexical comparation of the string representation of the date will help with this issue.

P.S. If this question is not appropriate for SO, please comment, I will remove it.

dodekja
  • 537
  • 11
  • 24
  • No, the way you compare keys does not help to avoid a degenerate tree, although it depends on which string representation you have in mind. But the standard ISO format has the same ordering as their chronological meaning. You have tagged this question with `tree-balancing`, so you already seem to know what to do... It might even be that a tree is not what you need. There is no context in the question so we cannot know. – trincot Nov 11 '22 at 09:46
  • @trincot Yes, I know what to do to get a balanced tree, I just wanted to know if comparing the string representations would yield different, e.g. non degenerate trees which would mean that a rebalancing is not necessary, even if the tree could not be called balanced. – dodekja Nov 11 '22 at 09:49
  • Yes, if you have some string representation that has a different ordering than the chronological order, you could get a better balanced tree, but then the inorder traversal of that tree will not give the dates in their chronological order. If you don't need that the inorder traversal has that property, then the question becomes why you would need the tree in the first place. Then a hashmap would be enough. – trincot Nov 11 '22 at 09:50

0 Answers0