1

I'm trying to make an interval tree from some boilerplate red-black-tree code. To make an augmented red-black-tree that supports intervals (as described in Wikipedia) you need to augment it to store that max value of any range of any child. I understand how I can annotate each node when inserting as it's a simple compare and update, but I'm stuck blocked based on how to properly handle rotations. (At least I think that's my problem)

All the tutorials and videos kinda hand wave and say "update the annotation on rotation". Any tips, resources, or code (pseudo or otherwise) to see how an insertion/deletion with augmentation this is done?

Schneems
  • 14,918
  • 9
  • 57
  • 84
  • Not sure if this is right or not, but I _think_ it works: First insert, then rotate, then set the annotation based on the children. My annotation value is derived from the key (max of range). So instead of preserving annotation during swap, just reset based on key and update based on kid's keys. Do this recursively. If it works longer term, I'll post some code and update this question – Schneems Jan 08 '22 at 16:28
  • I got the insertion working: Make sure that on rotations, you're moving the annotation in addition to color, key, and value. Also after I've inserted a node (and rotations are done) then I update annotations based on children. Since this process is recursive in my implementation it starts at the bottom and works it's way up. – Schneems Jan 10 '22 at 02:23
  • I've found a bug when deleting nodes though. I wasn't handling annotations correctly there. I'm not quite sure where the bug is though – Schneems Jan 10 '22 at 02:24

0 Answers0