Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
alg-1-lab-assignments
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
20041679 .
alg-1-lab-assignments
Commits
ea4d82ee
Commit
ea4d82ee
authored
1 year ago
by
20041679
Browse files
Options
Downloads
Patches
Plain Diff
bst ex. 2 (finished)
parent
776d5a30
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
alglib/src/bst.c
+115
-17
115 additions, 17 deletions
alglib/src/bst.c
alglib/src/bst_private.h
+21
-2
21 additions, 2 deletions
alglib/src/bst_private.h
with
136 additions
and
19 deletions
alglib/src/bst.c
+
115
−
17
View file @
ea4d82ee
...
...
@@ -54,7 +54,7 @@ upo_bst_t upo_bst_create(upo_bst_comparator_t key_cmp)
upo_bst_t
tree
=
malloc
(
sizeof
(
struct
upo_bst_s
));
if
(
tree
==
NULL
)
{
perror
(
"Unable to create a binary search tree"
);
perror
(
"Unable to create a binary search tre
tio
e"
);
abort
();
}
...
...
@@ -386,7 +386,7 @@ void upo_bst_delete_max(upo_bst_t tree, int destroy_data)
static
void
*
upo_bst_floor_impl
(
upo_bst_node_t
*
node
,
void
*
key
,
const
void
*
key
,
upo_bst_comparator_t
cmp
)
{
...
...
@@ -411,7 +411,7 @@ void* upo_bst_floor(const upo_bst_t tree, const void *key)
static
void
*
upo_bst_ceil_impl
(
upo_bst_node_t
*
node
,
void
*
key
,
const
void
*
key
,
upo_bst_comparator_t
cmp
)
{
...
...
@@ -434,30 +434,128 @@ void* upo_bst_ceiling(const upo_bst_t tree, const void *key)
return
upo_bst_ceil_impl
(
tree
->
root
,
key
,
upo_bst_get_comparator
(
tree
));
}
upo_bst_key_list_t
upo_bst_keys_range
(
const
upo_bst_t
tree
,
const
void
*
low_key
,
const
void
*
high_key
)
static
upo_bst_key_list_t
upo_bst_key_list_add
(
upo_bst_key_list_t
list
,
void
*
key
)
{
upo_bst_key_list_t
ret
;
ret
=
malloc
(
sizeof
(
upo_bst_key_list_node_t
));
ret
->
key
=
key
;
ret
->
next
=
list
;
return
ret
;
}
upo_bst_key_list_t
upo_bst_keys_range_impl
(
upo_bst_node_t
*
node
,
const
void
*
low_key
,
const
void
*
high_key
,
upo_bst_key_list_t
list
,
upo_bst_comparator_t
cmp
)
{
if
(
!
node
)
{
return
list
;
}
if
(
cmp
(
node
->
key
,
low_key
)
<
0
)
{
// outside lower bound, exclude left subtree
return
upo_bst_keys_range_impl
(
node
->
right
,
low_key
,
high_key
,
list
,
cmp
);
}
if
(
cmp
(
node
->
key
,
high_key
)
>
0
)
{
// outside upper bound, exclude right subtree
return
upo_bst_keys_range_impl
(
node
->
left
,
low_key
,
high_key
,
list
,
cmp
);
}
// inside range
return
upo_bst_keys_range_impl
(
node
->
right
,
low_key
,
high_key
,
upo_bst_keys_range_impl
(
node
->
left
,
low_key
,
high_key
,
upo_bst_key_list_add
(
list
,
node
->
key
),
cmp
),
cmp
);
}
upo_bst_key_list_t
upo_bst_keys_range
(
const
upo_bst_t
tree
,
const
void
*
low_key
,
const
void
*
high_key
)
{
return
upo_bst_keys_range_impl
(
tree
->
root
,
low_key
,
high_key
,
NULL
,
upo_bst_get_comparator
(
tree
)
);
}
static
void
upo_bst_key_list_add_key_visit
(
void
*
key
,
void
*
value
,
void
*
context
)
{
/* TO STUDENTS:
* Remove the following two lines and put here your implementation. */
fprintf
(
stderr
,
"To be implemented!
\n
"
);
abort
();
(
void
)
value
;
upo_bst_key_list_t
*
list
=
context
;
*
list
=
upo_bst_key_list_add
(
*
list
,
key
);
}
upo_bst_key_list_t
upo_bst_keys
(
const
upo_bst_t
tree
)
{
/* TO STUDENTS:
* Remove the following two lines and put here your implementation. */
fprintf
(
stderr
,
"To be implemented!
\n
"
);
abort
();
upo_bst_key_list_t
ret
=
NULL
;
upo_bst_traverse_in_order
(
tree
,
upo_bst_key_list_add_key_visit
,
&
ret
);
return
ret
;
}
int
upo_bst_is_bst
(
const
upo_bst_t
tree
,
const
void
*
min_key
,
const
void
*
max_key
)
static
int
upo_bst_is_bst_impl
(
upo_bst_node_t
*
node
,
const
void
*
min_key
,
const
void
*
max_key
,
upo_bst_comparator_t
cmp
)
{
/* TO STUDENTS:
* Remove the following two lines and put here your implementation. */
fprintf
(
stderr
,
"To be implemented!
\n
"
);
abort
();
if
(
!
node
)
{
return
1
;
}
if
(
cmp
(
node
->
key
,
min_key
)
<
0
||
cmp
(
node
->
key
,
max_key
)
>
0
)
{
return
0
;
}
return
upo_bst_is_bst_impl
(
node
->
left
,
min_key
,
node
->
key
,
cmp
)
&&
upo_bst_is_bst_impl
(
node
->
right
,
node
->
key
,
max_key
,
cmp
);
}
int
upo_bst_is_bst
(
const
upo_bst_t
tree
,
const
void
*
min_key
,
const
void
*
max_key
)
{
return
upo_bst_is_bst_impl
(
tree
->
root
,
min_key
,
max_key
,
upo_bst_get_comparator
(
tree
)
);
}
/**** EXERCISE #2 - END of EXTRA OPERATIONS ****/
...
...
This diff is collapsed.
Click to expand it.
alglib/src/bst_private.h
+
21
−
2
View file @
ea4d82ee
...
...
@@ -53,6 +53,12 @@ struct upo_bst_s
static
upo_bst_node_t
*
upo_bst_node_create
(
void
*
key
,
void
*
value
);
static
upo_bst_key_list_t
upo_bst_key_list_add
(
upo_bst_key_list_t
list
,
void
*
key
);
static
void
upo_bst_node_destroy
(
upo_bst_node_t
*
node
,
int
destroy_data
);
/**
* \brief Clears the subtree rooted at the given node.
*
...
...
@@ -112,12 +118,25 @@ static upo_bst_node_t *upo_bst_delete_max_impl(
);
static
void
*
upo_bst_floor_impl
(
upo_bst_node_t
*
node
,
void
*
key
,
const
void
*
key
,
upo_bst_comparator_t
cmp
);
static
void
*
upo_bst_ceil_impl
(
upo_bst_node_t
*
node
,
void
*
key
,
const
void
*
key
,
upo_bst_comparator_t
cmp
);
upo_bst_key_list_t
upo_bst_keys_range_impl
(
upo_bst_node_t
*
node
,
const
void
*
low_key
,
const
void
*
high_key
,
upo_bst_key_list_t
list
,
upo_bst_comparator_t
cmp
);
static
int
upo_bst_is_bst_impl
(
upo_bst_node_t
*
node
,
const
void
*
min_key
,
const
void
*
max_key
,
upo_bst_comparator_t
cmp
);
...
...
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