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
d4f3ab1f
Commit
d4f3ab1f
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
funclang series4: Implemented insert function for ass9.
parent
3143e5aa
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
+17
-10
17 additions, 10 deletions
funclang-taddeus/series4/ass9.ml
with
17 additions
and
10 deletions
funclang-taddeus/series4/ass9.ml
+
17
−
10
View file @
d4f3ab1f
...
...
@@ -9,24 +9,30 @@ let rec starts_with l s =
|
h
::
t
->
h
=
(
hd
l
)
&&
starts_with
(
tl
l
)
t
;;
(* Insert a new (key, value) pair into a trie *)
let
rec
insert
trie
key
value
=
match
trie
with
|
Empty
->
Node
(
key
,
value
,
[]
)
(* Root *)
|
Node
(
k
,
v
,
children
)
->
if
starts_with
key
k
then
(* Check all children for a longer prefix *)
if
(
length
k
)
=
(
length
key
)
-
1
then
(* Direct child *)
Node
(
k
,
v
,
children
@
[
Node
(
key
,
value
,
[]
)])
else
(* Add another nde between the matched node and the child,
* with the length of the matched node plus one *)
Empty
if
k
=
key
then
raise
(
Failure
"Inserted key already exists in trie"
)
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
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
)])
else
raise
(
Failure
"Inserted key does not start with node key"
)
;;
(**)
(*
Look up a value associated with a key
*)
let
rec
lookup
trie
key
=
match
trie
with
|
Empty
->
None
(* Trie is empty, so no result *)
...
...
@@ -49,6 +55,7 @@ let rec lookup trie key =
None
;;
(* Find all (key, value) pairs whose key start with a given prefix *)
let
rec
matches
trie
key
=
match
trie
with
|
Empty
->
[]
...
...
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