Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created December 4, 2012 17:53
Show Gist options
  • Save jordanlewis/4206836 to your computer and use it in GitHub Desktop.
Save jordanlewis/4206836 to your computer and use it in GitHub Desktop.
PFDS Exercise 2.2
class UnbalancedTreeSet[T] private (tree: BinaryTree[T]) (implicit val ordering: Ordering[T]) extends Set[T] {
// ...
def member(x: T) = UnbalancedTreeSet.member(x, tree, None)
// ...
}
object UnbalancedTreeSet {
// ...
private def member[T](x: T, t: BinaryTree[T], c: Option[T])(implicit ordering: Ordering[T]): Boolean = (t, c) match {
case (BinaryTreeLeaf, None) => false
case (BinaryTreeLeaf, Some(d)) => ordering.equiv(x, d)
case (BinaryTreeNode(a, y, b), _) =>
if (ordering.lt(x, y)) member(x, a, c)
else member(x, b, Some(y))
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment