Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
f374d866
Commit
f374d866
authored
Nov 27, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
funclang series4: Fixed insert function in ass9.
parent
d4f3ab1f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
10 deletions
+27
-10
funclang-taddeus/series4/ass9.ml
funclang-taddeus/series4/ass9.ml
+27
-10
No files found.
funclang-taddeus/series4/ass9.ml
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]);;
*)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment