Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jordanlewis/2573799 to your computer and use it in GitHub Desktop.
Save jordanlewis/2573799 to your computer and use it in GitHub Desktop.
PFDS Unbalanced Tree Set companion object
object UnbalancedTreeSet {
private def insert[T](x: T, t: BinaryTree[T])(implicit ordering: Ordering[T]): BinaryTree[T] = t match {
case BinaryTreeLeaf => BinaryTreeNode(BinaryTreeLeaf, x, BinaryTreeLeaf)
case tn @ BinaryTreeNode(a, y, b) =>
if (ordering.lt(x, y)) BinaryTreeNode(insert(x, a), y, b)
else if (ordering.lt(y, x)) BinaryTreeNode(a, y, insert(x, b))
else tn
}
private def member[T](x: T, t: BinaryTree[T])(implicit ordering: Ordering[T]): Boolean = (x, t) match {
case (_, BinaryTreeLeaf) => false
case (_, BinaryTreeNode(a, y, b)) =>
if (ordering.lt(x, y)) member(x, a)
else if (ordering.lt(y, x)) member(x, b)
else true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment