Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
9ff7ddd1
Commit
9ff7ddd1
authored
14 years ago
by
Utkarsh Upadhyay
Browse files
Options
Downloads
Patches
Plain Diff
Thanks to Swen's isSorted function, contains can be successfully verified.
parent
af0a468d
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
testcases/BinarySearchTree.scala
+6
-5
6 additions, 5 deletions
testcases/BinarySearchTree.scala
with
6 additions
and
5 deletions
testcases/BinarySearchTree.scala
+
6
−
5
View file @
9ff7ddd1
import
scala.collection.immutable.Set
import
scala.collection.immutable.Set
import
scala.collection.immutable.Multiset
//
import scala.collection.immutable.Multiset
object
BinarySearchTree
{
object
BinarySearchTree
{
sealed
abstract
class
Tree
sealed
abstract
class
Tree
...
@@ -15,8 +15,7 @@ object BinarySearchTree {
...
@@ -15,8 +15,7 @@ object BinarySearchTree {
sealed
abstract
class
Triple
sealed
abstract
class
Triple
case
class
SortedTriple
(
min
:
Option
,
max
:
Option
,
sorted
:
Boolean
)
extends
Triple
case
class
SortedTriple
(
min
:
Option
,
max
:
Option
,
sorted
:
Boolean
)
extends
Triple
def
isSorted
(
tree
:
Tree
)
:
SortedTriple
=
tree
match
{
def
isSorted
(
tree
:
Tree
)
:
SortedTriple
=
tree
match
{
case
Leaf
()
=>
SortedTriple
(
None
(),
None
(),
true
)
case
Leaf
()
=>
SortedTriple
(
None
(),
None
(),
true
)
case
Node
(
l
,
v
,
r
)
=>
isSorted
(
l
)
match
{
case
Node
(
l
,
v
,
r
)
=>
isSorted
(
l
)
match
{
case
SortedTriple
(
minl
,
maxl
,
sortl
)
=>
if
(!
sortl
)
SortedTriple
(
None
(),
None
(),
false
)
case
SortedTriple
(
minl
,
maxl
,
sortl
)
=>
if
(!
sortl
)
SortedTriple
(
None
(),
None
(),
false
)
...
@@ -140,7 +139,9 @@ object BinarySearchTree {
...
@@ -140,7 +139,9 @@ object BinarySearchTree {
res
.
left
!=
Leaf
()
&&
res
.
right
!=
Leaf
()
res
.
left
!=
Leaf
()
&&
res
.
right
!=
Leaf
()
)
)
def
contains
(
tree
:
Tree
,
value
:
Int
)
:
Boolean
=
tree
match
{
def
contains
(
tree
:
Tree
,
value
:
Int
)
:
Boolean
=
{
require
(
isSorted
(
tree
).
sorted
)
tree
match
{
case
Leaf
()
=>
false
case
Leaf
()
=>
false
case
n
@Node
(
l
,
v
,
r
)
=>
if
(
v
<
value
)
{
case
n
@Node
(
l
,
v
,
r
)
=>
if
(
v
<
value
)
{
contains
(
r
,
value
)
contains
(
r
,
value
)
...
@@ -149,7 +150,7 @@ object BinarySearchTree {
...
@@ -149,7 +150,7 @@ object BinarySearchTree {
}
else
{
}
else
{
true
true
}
}
}
}
}
ensuring
(
_
||
!(
contents
(
tree
)
==
contents
(
tree
)
++
Set
(
value
)))
def
contents
(
tree
:
Tree
)
:
Set
[
Int
]
=
tree
match
{
def
contents
(
tree
:
Tree
)
:
Set
[
Int
]
=
tree
match
{
case
Leaf
()
=>
Set
.
empty
[
Int
]
case
Leaf
()
=>
Set
.
empty
[
Int
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment