Created
May 2, 2012 04:49
-
-
Save jordanlewis/2573799 to your computer and use it in GitHub Desktop.
PFDS Unbalanced Tree Set companion object
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
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