Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
Taddeüs Kroes
uva
Commits
f374d866
Commit
f374d866
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
funclang series4: Fixed insert function in ass9.
parent
d4f3ab1f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
funclang-taddeus/series4/ass9.ml
+27
-10
27 additions, 10 deletions
funclang-taddeus/series4/ass9.ml
with
27 additions
and
10 deletions
funclang-taddeus/series4/ass9.ml
+
27
−
10
View file @
f374d866
...
...
@@ -19,15 +19,32 @@ let rec insert trie key value =
else
if
starts_with
key
k
then
(* Inserted key should be in this node because it starts with
* the node's key *)
let
matches_key
=
function
|
Empty
->
false
|
Node
(
k
,
_
,
c
)
->
starts_with
key
k
let
rec
walk_nodes
=
function
|
[]
->
Empty
|
node
::
tail
->
match
node
with
|
Empty
->
Empty
|
Node
(
k
,
_
,
_
)
->
if
starts_with
key
k
then
Node
(
k
,
v
,
(
insert
node
key
value
)
::
children
)
else
walk_nodes
tail
in
(* 'move' are the children that are moved to be the children of the
* inserted node, 'siblings' are the children that remain in the
* current matched node *)
let
move
,
siblings
=
partition
matches_key
children
in
Node
(
k
,
v
,
siblings
@
[
Node
(
key
,
value
,
move
)])
(* First, check if the new pair should be inserted in one of the
* children. If not, prepend it to the children list while moving
* all children that are should be inside the new node *)
match
walk_nodes
children
with
|
Empty
->
let
matches_key
=
function
|
Empty
->
false
|
Node
(
k
,
_
,
_
)
->
starts_with
k
key
in
(* 'move' are the children that are moved to be the children of
* the inserted node, 'siblings' are the children that remain
* in the current matched node *)
let
move
,
siblings
=
partition
matches_key
children
in
Node
(
k
,
v
,
(
Node
(
key
,
value
,
move
))
::
siblings
)
|
node
->
node
else
raise
(
Failure
"Inserted key does not start with node key"
)
;;
...
...
@@ -68,6 +85,6 @@ let rec matches trie key =
@
match_nodes
children
;;
let
a
=
Node
([
1
;
2
;
3
]
,
"a"
,
[]
);;
(*
let a = Node ([1;2;3], "a", []);;
let b = Node ([1;3], "b", [a]);;
let
c
=
Node
([
3
;
4
]
,
"c"
,
[
b
]);;
let c = Node ([3;4], "c", [b]);;
*)
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