added assignment 3 ex 3.1
This commit is contained in:
		| @@ -4,6 +4,7 @@ abstract class IntSet() { | ||||
|   def foreach(f: Int => Unit): Unit | ||||
|   def union(other: IntSet): IntSet | ||||
|   def intersect(other: IntSet): IntSet | ||||
|   def excl(x: Int): IntSet | ||||
| } | ||||
|  | ||||
| class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() { | ||||
| @@ -37,6 +38,12 @@ class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet() { | ||||
|     base.union(this.left.intersect(other)) | ||||
|         .union(this.right.intersect(other)) | ||||
|   } | ||||
|  | ||||
|   def excl(x: Int): IntSet = { | ||||
|     if (x < elem) new NonEmpty(elem, this.left.excl(x), this.right) | ||||
|     else if (x > elem) new NonEmpty(elem, this.left, this.right.excl(x)) | ||||
|     else this.left.union(this.right) | ||||
|   } | ||||
| } | ||||
|  | ||||
| object Empty extends IntSet() { | ||||
| @@ -47,6 +54,7 @@ object Empty extends IntSet() { | ||||
|   def foreach(f: Int => Unit): Unit = {} | ||||
|   def union(other: IntSet): IntSet = other | ||||
|   def intersect(other: IntSet): IntSet = Empty | ||||
|   def excl(x: Int): IntSet = this | ||||
| } | ||||
|  | ||||
| val t1 = Empty | ||||
| @@ -69,3 +77,8 @@ val s2 = Empty.add(3).add(4).add(6).add(2) | ||||
|  | ||||
| s.union(s2) | ||||
| s.intersect(s2) | ||||
|  | ||||
| s2.excl(0) | ||||
| s2.excl(6) | ||||
| s2.excl(2) | ||||
| s2.excl(4) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user