Skip to content
Snippets Groups Projects
Commit ea4d82ee authored by 20041679's avatar 20041679
Browse files

bst ex. 2 (finished)

parent 776d5a30
No related branches found
No related tags found
No related merge requests found
...@@ -54,7 +54,7 @@ upo_bst_t upo_bst_create(upo_bst_comparator_t key_cmp) ...@@ -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)); upo_bst_t tree = malloc(sizeof(struct upo_bst_s));
if (tree == NULL) if (tree == NULL)
{ {
perror("Unable to create a binary search tree"); perror("Unable to create a binary search tretioe");
abort(); abort();
} }
...@@ -386,7 +386,7 @@ void upo_bst_delete_max(upo_bst_t tree, int destroy_data) ...@@ -386,7 +386,7 @@ void upo_bst_delete_max(upo_bst_t tree, int destroy_data)
static void *upo_bst_floor_impl( static void *upo_bst_floor_impl(
upo_bst_node_t *node, upo_bst_node_t *node,
void *key, const void *key,
upo_bst_comparator_t cmp upo_bst_comparator_t cmp
) )
{ {
...@@ -411,7 +411,7 @@ void* upo_bst_floor(const upo_bst_t tree, const void *key) ...@@ -411,7 +411,7 @@ void* upo_bst_floor(const upo_bst_t tree, const void *key)
static void *upo_bst_ceil_impl( static void *upo_bst_ceil_impl(
upo_bst_node_t *node, upo_bst_node_t *node,
void *key, const void *key,
upo_bst_comparator_t cmp upo_bst_comparator_t cmp
) )
{ {
...@@ -434,30 +434,128 @@ void* upo_bst_ceiling(const upo_bst_t tree, const void *key) ...@@ -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)); 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: (void)value;
* Remove the following two lines and put here your implementation. */ upo_bst_key_list_t *list = context;
fprintf(stderr, "To be implemented!\n"); *list = upo_bst_key_list_add(*list, key);
abort();
} }
upo_bst_key_list_t upo_bst_keys(const upo_bst_t tree) upo_bst_key_list_t upo_bst_keys(const upo_bst_t tree)
{ {
/* TO STUDENTS: upo_bst_key_list_t ret = NULL;
* Remove the following two lines and put here your implementation. */ upo_bst_traverse_in_order(tree, upo_bst_key_list_add_key_visit, &ret);
fprintf(stderr, "To be implemented!\n"); return ret;
abort();
} }
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: if (!node) {
* Remove the following two lines and put here your implementation. */ return 1;
fprintf(stderr, "To be implemented!\n"); }
abort(); 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 ****/ /**** EXERCISE #2 - END of EXTRA OPERATIONS ****/
......
...@@ -53,6 +53,12 @@ struct upo_bst_s ...@@ -53,6 +53,12 @@ struct upo_bst_s
static upo_bst_node_t *upo_bst_node_create(void *key, void *value); 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. * \brief Clears the subtree rooted at the given node.
* *
...@@ -112,12 +118,25 @@ static upo_bst_node_t *upo_bst_delete_max_impl( ...@@ -112,12 +118,25 @@ static upo_bst_node_t *upo_bst_delete_max_impl(
); );
static void *upo_bst_floor_impl( static void *upo_bst_floor_impl(
upo_bst_node_t *node, upo_bst_node_t *node,
void *key, const void *key,
upo_bst_comparator_t cmp upo_bst_comparator_t cmp
); );
static void *upo_bst_ceil_impl( static void *upo_bst_ceil_impl(
upo_bst_node_t *node, 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 upo_bst_comparator_t cmp
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment