Last active
August 29, 2015 13:57
-
-
Save Gerjo/9486837 to your computer and use it in GitHub Desktop.
Pseudo code for space-efficiently saving number ranges.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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