Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Last active August 29, 2015 13:57
Show Gist options
  • Save Gerjo/9486837 to your computer and use it in GitHub Desktop.
Save Gerjo/9486837 to your computer and use it in GitHub Desktop.
Pseudo code for space-efficiently saving number ranges.
node::insert(int value) {
// Value falls in range
if(value >= this.min && value <= this.max) {
}
// Extend range
if(value == this.min - 1) {
this.min = value;
if(this.right.max == value) {
// Merge right with this
}
}
// Extend range
if(value == this.max + 1) {
this.max = value;
if(this.left.min == value) {
// Merge left with this
}
}
if(value > this.max) {
this.left.insert(value);
}
if(value < this.min) {
this.right.insert(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment