Skip to content
Browse files

Fixed a number of bugs, got a work-around for a bug in Range (Int.Max…

…Value to Int.MaxValue is empty).

At any rate, I now see trying to use RedBlack without modification really won't work. It will be necessary to follow Wikipedia's Augmented Tree algorithm, which requires keeping track of maximum high value. That would be better done with a finger tree, I think, but that's beside the point.

Nothing will come out of things as they stand, and RedBlack isn't particularly prone to subclassing.
  • Loading branch information...
1 parent 50a6f69 commit 4471b10a51f705cdcc74dd1ce61846dbec00cdcf @dcsobral committed Jan 11, 2010
Showing with 28 additions and 3 deletions.
  1. +24 −1 src/main/scala/IntervalTree.scala
  2. +4 −2 src/test/scala/IntervalTreeCheck.scala
View
25 src/main/scala/IntervalTree.scala
@@ -71,7 +71,7 @@ class IntervalTree {
def search(t: Tree[Node]): (List[Range], List[Range]) = t match {
case Empty => (Nil, Nil)
case ne: NonEmpty[_] =>
- val (byStart, byEnd) = if (ne.key < r) search(ne.left) else search(ne.right)
+ val (byStart, byEnd) = if (r < ne.key) search(ne.left) else search(ne.right)
((ne.value.byStart filter (r intersects _)) ++ byStart,
(ne.value.byEnd filter (r intersects _)) ++ byEnd)
}
@@ -87,5 +87,28 @@ class IntervalTree {
def iterator = root.elements map (n => (n._2.byStart, n._2.byEnd))
def iteratorByStart = root.elements flatMap (_._2.byStart.elements)
def iteratorByEnd = root.elements flatMap (_._2.byEnd.elements)
+
+ def rangeToString(r: Range) = "[%d%s]" format (r.first, if (r.last == r.first) "" else ","+r.last)
+
+ def treeToString(t: Tree[Node], level: Int): String = t match {
+ case Empty => "E\n"
+ case BlackTree(key, value, left, right) =>
+ "Black Center: %s\tRanges: %s\n%s|\n%s+L: %s%s|\n%s+R: %s" format
+ (key, value.byStart map rangeToString mkString ",",
+ "|"*level,
+ "|"*level, treeToString(left, level + 1),
+ "|"*level,
+ "|"*level, treeToString(right, level + 1))
+ case RedTree(key, value, left, right) =>
+ "Red Center: %s\tRanges: %s\n%s|\n%s+L: %s%s|\n%s+R: %s" format
+ (key, value.byStart map rangeToString mkString ",",
+ "|"*level,
+ "|"*level, treeToString(left, level + 1),
+ "|"*level,
+ "|"*level, treeToString(right, level + 1))
+ case other => error("Unexpected "+other)
+ }
+
+ override def toString = "\n"+treeToString(root, 0)
}
View
6 src/test/scala/IntervalTreeCheck.scala
@@ -16,7 +16,9 @@ object IntervalTreeCheck extends Properties("IntervalTree") {
implicit def arbIntervalTree: Arbitrary[IntervalTree] = Arbitrary(myIntervalTreeGen)
property("All intervals containing a point are returned") =
- forAll { (t: IntervalTree, n: Int) =>
- (t.iteratorByStart.toList.filter(_ contains n) == t.lookup(n)._1) :| (t.iteratorByStart.toList.filter(_ contains n)+"\t"+t.lookup(n)._1)
+ forAll { (t: IntervalTree, n: Int) => n < 1000 ==>
+ (t.iteratorByStart.toList.filter(_ contains n) == t.lookup(n)._1) :|
+ "Expected: "+(t.iteratorByStart.toList.filter(_ contains n).map(t.rangeToString)+
+ "\nGot: "+t.lookup(n)._1.map(t.rangeToString))
}
}

0 comments on commit 4471b10

Please sign in to comment.
Something went wrong with that request. Please try again.