-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchFile2.sc
50 lines (40 loc) · 1.3 KB
/
BinarySearchFile2.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
sealed trait Tree[+T]
case object Leaf extends Tree[Nothing]
case class Branch[T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T]
type BST[T] = (Tree[T], (T,T) => Boolean)
def insert[T](a: T, bst: BST[T]): BST[T] = {
val(tr, isLower) = bst
def insertTree(tree: Tree[T]): Tree[T] = tree match {
case Leaf => Branch(a, Leaf, Leaf)
case Branch(v,l,r) => if(v==a) tree else if(isLower(a, v)) Branch(v, insertTree(l), r) else Branch(v, l, insertTree(r))
}
(insertTree(tr), isLower)
}
def build[T](list: List[T],isLower: (T,T)=>Boolean): BST[T] = {
list.foldLeft((Leaf: Tree[T], isLower))((tree:BST[T], element)=>insert(element, tree))
}
def isBigger(a: Int, b: Int) = {a>b}
def contains[T](bst: BST[T], a: T): Boolean = {
def iterate(tree: Tree[T]): Boolean = tree match {
case Leaf => false
case Branch(v,l,r) => {
if(a==v) true
else if(isBigger(a.asInstanceOf[Int],v.asInstanceOf[Int]))
iterate(r)
else
iterate(l)
}
}
iterate(bst._1)
}
val tre2 = build(List(1,2,3,8,4), (a: Int, b: Int) => a<b)
var tre3: BST[Int] = (Leaf, (a: Int, b: Int) => a<b)
tre3 = insert(1, tre3)
tre3 = insert(2, tre3)
tre3 = insert(2, tre3)
tre3 = insert(3, tre3)
tre3 = insert(8, tre3)
tre3 = insert(4, tre3)
contains(tre3, 2)
contains(tre3, 8)
contains(tre3, 11)