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
89629bdf
Commit
89629bdf
authored
14 years ago
by
Viktor Kuncak
Browse files
Options
Downloads
Patches
Plain Diff
red black tree example
parent
07fcf25b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
testcases/RedBlackTree.scala
+46
-0
46 additions, 0 deletions
testcases/RedBlackTree.scala
with
46 additions
and
0 deletions
testcases/RedBlackTree.scala
0 → 100644
+
46
−
0
View file @
89629bdf
import
scala.collection.immutable.Set
//import scala.collection.immutable.Multiset
object
RedBlackTree
{
sealed
abstract
class
Color
case
class
Red
()
extends
Color
case
class
Black
()
extends
Color
sealed
abstract
class
Tree
case
class
Empty
()
extends
Tree
case
class
Node
(
color
:
Color
,
left
:
Tree
,
value
:
Int
,
right
:
Tree
)
extends
Tree
def
content
(
t
:
Tree
)
:
Set
[
Int
]
=
t
match
{
case
Empty
()
=>
Set
.
empty
case
Node
(
_
,
l
,
v
,
r
)
=>
content
(
l
)
++
Set
(
v
)
++
content
(
r
)
}
def
ins
(
x
:
Int
,
t
:
Tree
)
:
Tree
=
(
t
match
{
case
Empty
()
=>
Node
(
Red
(),
Empty
(),
x
,
Empty
())
case
Node
(
c
,
a
,
y
,
b
)
=>
if
(
x
<
y
)
balance
(
c
,
ins
(
x
,
a
),
y
,
b
)
else
if
(
x
==
y
)
Node
(
c
,
a
,
y
,
b
)
else
balance
(
c
,
a
,
y
,
ins
(
x
,
b
))
})
ensuring
(
content
(
_
)
==
content
(
t
)
++
Set
(
x
))
def
add
(
x
:
Int
,
t
:
Tree
)
:
Tree
=
{
makeBlack
(
ins
(
x
,
t
))
}
ensuring
(
content
(
_
)
==
content
(
t
)
++
Set
(
x
))
def
balance
(
c
:
Color
,
a
:
Tree
,
x
:
Int
,
b
:
Tree
)
:
Tree
=
(
Node
(
c
,
a
,
x
,
b
)
match
{
case
Node
(
Black
(),
Node
(
Red
(),
Node
(
Red
(),
a
,
xV
,
b
),
yV
,
c
),
zV
,
d
)
=>
Node
(
Red
(),
Node
(
Black
(),
a
,
xV
,
b
),
yV
,
Node
(
Black
(),
c
,
zV
,
d
))
case
Node
(
Black
(),
Node
(
Red
(),
a
,
xV
,
Node
(
Red
(),
b
,
yV
,
c
)),
zV
,
d
)
=>
Node
(
Red
(),
Node
(
Black
(),
a
,
xV
,
b
),
yV
,
Node
(
Black
(),
c
,
zV
,
d
))
case
Node
(
Black
(),
a
,
xV
,
Node
(
Red
(),
Node
(
Red
(),
b
,
yV
,
c
),
zV
,
d
))
=>
Node
(
Red
(),
Node
(
Black
(),
a
,
xV
,
b
),
yV
,
Node
(
Black
(),
c
,
zV
,
d
))
case
Node
(
Black
(),
a
,
xV
,
Node
(
Red
(),
b
,
yV
,
Node
(
Red
(),
c
,
zV
,
d
)))
=>
Node
(
Red
(),
Node
(
Black
(),
a
,
xV
,
b
),
yV
,
Node
(
Black
(),
c
,
zV
,
d
))
case
Node
(
c
,
a
,
xV
,
b
)
=>
Node
(
c
,
a
,
xV
,
b
)
})
ensuring
(
res
=>
content
(
res
)
==
content
(
Node
(
c
,
a
,
x
,
b
)))
def
makeBlack
(
n
:
Tree
)
:
Tree
=
n
match
{
case
Node
(
Red
(),
l
,
v
,
r
)
=>
Node
(
Black
(),
l
,
v
,
r
)
case
_
=>
n
}
}
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