From acbef5d0ec5e102e2aeb02010efbbc13e3020d08 Mon Sep 17 00:00:00 2001 From: wael Date: Sun, 7 Nov 2021 14:47:00 +0200 Subject: [PATCH] Clean up TODO items --- list_todo.sh | 2 +- progress_and_notes | 1 - tests/bst_tests.c | 2 -- tests/queue_tests.c | 2 -- trees/binary_search_tree/binary_search_tree.c | 12 ++++++++++-- trees/binary_search_tree/binary_search_tree.h | 2 ++ 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/list_todo.sh b/list_todo.sh index 80e283b..6750c0e 100755 --- a/list_todo.sh +++ b/list_todo.sh @@ -1,3 +1,3 @@ #!/bin/zsh # This script lists files with TODO while ignoring backup files, object files and compiled executables. -grep "TODO" . --line-number --with-filename --recursive --context=2 | sed '/~/d' | sed '/\.o/d' +grep "TODO" . --line-number --with-filename --recursive --context=2 | sed '/~/d' | sed '/\.o/d' | sed '/\.git/d' diff --git a/progress_and_notes b/progress_and_notes index 2d97cef..14888dc 100644 --- a/progress_and_notes +++ b/progress_and_notes @@ -52,4 +52,3 @@ Array list (?). Dynamic array/vector. Misc tasks: -Write a script to locate all TODO occurances throughout the project. diff --git a/tests/bst_tests.c b/tests/bst_tests.c index 5910eaa..cbbf327 100644 --- a/tests/bst_tests.c +++ b/tests/bst_tests.c @@ -3,8 +3,6 @@ #include #include "../trees/binary_search_tree/binary_search_tree.h" -/* TODO: Check the functions which BST added (in comparison to regular trees). */ - /* Define constants. */ #define PRINT_INFO_TESTING_START "Starting tests.\n" #define PRINT_INFO_TESTING_END "Done testing.\n" diff --git a/tests/queue_tests.c b/tests/queue_tests.c index ee1019f..b0d61ad 100644 --- a/tests/queue_tests.c +++ b/tests/queue_tests.c @@ -1,5 +1,3 @@ -/* TODO: Check the code and make sure that these tests pass! */ - /* Include statements. */ #include #include diff --git a/trees/binary_search_tree/binary_search_tree.c b/trees/binary_search_tree/binary_search_tree.c index 0c00575..5923488 100644 --- a/trees/binary_search_tree/binary_search_tree.c +++ b/trees/binary_search_tree/binary_search_tree.c @@ -184,7 +184,6 @@ tree_node_t* search_in_binary_search_tree(tree_node_t *root, void *data, int (*c /* Check if we found the right node, and recurse accordingly. */ comparison_result = (*comparison_function)(root->data, data); - /* TODO: sanity check on this code, it should be right, but it seems a bit off. */ return ((comparison_result == 0) ? root : ((comparison_result == 1) ? search_in_binary_search_tree(root->left, data, comparison_function) : search_in_binary_search_tree(root->right, data, comparison_function))); } @@ -248,7 +247,7 @@ void **sorted_array_from_binary_search_tree(tree_node_t *root, size_t *length) { height = find_binary_search_tree_depth(root); /* Derive the array's (maximal) size based on said height (assumes a balanced and near-full tree). */ - array_size = (size_t) (pow(2, height + 1) - 1); /* TODO: Check if this calculation agrees with the height-finding function. */ + array_size = (size_t) (pow(2, height + 1) - 1); /* Attempt to allocate the array. */ array = (void**) calloc(array_size, sizeof(void*)); @@ -264,6 +263,15 @@ void **sorted_array_from_binary_search_tree(tree_node_t *root, size_t *length) { free(array); return NULL; } + /* Set the array's length. */ + *length = ++index; + /* Resize the array if needed. */ + if (index < array_size) { + /* Check for (non-fatal) failure. */ + if (rallocarray(array, index, sizeof(void*)) == ENOMEM) + printf(BINARY_SEARCH_TREE_RESIZING_ARRAY_FAILURE_CODE); + } + /* Success. */ return array; } diff --git a/trees/binary_search_tree/binary_search_tree.h b/trees/binary_search_tree/binary_search_tree.h index 1661900..ba5f88a 100644 --- a/trees/binary_search_tree/binary_search_tree.h +++ b/trees/binary_search_tree/binary_search_tree.h @@ -16,6 +16,8 @@ #define BINARY_SEARCH_TREE_COMPARISON_EQUAL_TO 0 /* Return code for comparison, larger. */ #define BINARY_SEARCH_TREE_COMPARISON_LARGER_THAN 1 +/* Error text for when resizing fails. */ +#define BINARY_SEARCH_TREE_RESIZING_ARRAY_FAILURE_CODE "Failed to resize/shrink the array in sorted_array_from_binary_search_tree().\n" /* Define all the functions. */ /* This function initializes a single node. */