-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-search-tree.sc
72 lines (63 loc) · 1.69 KB
/
binary-search-tree.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
abstract sealed class BST {
def find(x: Int): Boolean
def insert(x: Int): BST
def remove(x: Int): BST
def min: Int
}
case object Empty extends BST {
override def find(x: Int): Boolean = false
override def insert(x: Int): BST = Node(x, Empty, Empty)
override def remove(x: Int): BST = throw new IllegalArgumentException
override def min: Int = throw new IllegalArgumentException
}
case class Node(v: Int, l: BST, r: BST) extends BST {
override def find(x: Int): Boolean = {
if (x == v) true
else if (x < v) l.find(x)
else r.find(x)
}
override def insert(x: Int): BST = {
if (x <= v)
Node(v, l.insert(x), r)
else
Node(v, l, r.insert(x))
}
override def remove(x: Int): BST = {
if (x < v)
Node(v, l.remove(x), r)
else if (x > v)
Node(v, l, r.remove(x))
else { // x == v
this match {
case Node(_, left, Empty) => left
case Node(_, Empty, right) => right
case Node(_, left, right) => {
// replace node with smallest value of right subtree
val min_right = right.min
Node(min_right, left, right.remove(min_right))
}
}
}
}
override def min: Int = {
this match {
// smallest value is always in left subtree
// or in the node itself if not existent
case Node(value, Empty, _) => value
case Node(_, left, _) => left.min
}
}
}
val t0 = Empty.insert(4).insert(3).insert(5)
val t1 = t0.insert(7).insert(1).insert(4)
t1.find(1)
t1.min
val t2 = t1.remove(1)
t2.find(1)
t2.min
val t3 = t2.remove(5).insert(5)
t3.find(5)
val t4 = t3.remove(3)
t4.find(3)
t4.min
t4.remove(4)