I have a function that I got as a school assignment to build a sorted binary search tree from a sorted array. I'm not allowed to edit the function signature. The function signature should be:
static void buildTreeSortedFromArray(BSTree* tree, const int arr[], int size);
//tree
struct treeNode
{
int data;
struct treeNode* left;
struct treeNode* right;
};
typedef struct treeNode* BSTree;
//this is how far i have got
static void buildTreeSortedFromArray(BSTree* tree, const int arr[], int size)
{
int mid = size / 2;
if (size > size * 2) {
return NULL;
}
//works for the first time only
if (sizeof(arr) == size) {
mid = (size - 1) / 2;
}
*tree = createNode(arr[mid]);
BSTree treeTemp = *tree;
buildTreeSortedFromArray(&(treeTemp)->left, arr,mid-1);
buildTreeSortedFromArray(&(treeTemp)->right, arr, mid+1);
}
I have no problem recursively building the left side of the tree because I only need to divide the array by 2 until I reach index 0 but the right side is the tricky bit for me.
I can't grasp how I should reach the right half of the array if I'm not allowed to add a start variable as an argument to the function signature?
I appreciate any advice.