1

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.

  • 2
    Hello Sejaad. Observe how I edited your question, formatting your code. Next time, it would be nice, if you would do it yourself. :) An advice to get help in Stackoverflow is to create an [MCVE](https://stackoverflow.com/help/mcve). – gsamaras Dec 18 '18 at 12:36
  • 3
    If for instance you have `arr` and size = `10`, then in your two recursive calls you pass `arr` and size = `5` respectively `arr+5` and size = `5`. – Blaze Dec 18 '18 at 12:38
  • 1
    You should post what you've done so far for the left side, the approach will be similar for the rest of the array. – vgru Dec 18 '18 at 13:04
  • 1
    Is there nothing stopping you having your function call another function that does have a start variable? – Chris Turner Dec 18 '18 at 13:53
  • This is impossible to answer without knowing what `BSTree` is. Some implementations just use NULL for the root, and then this prototype is fundamentally flawed and the problem cannot be solved. Please show the type definition for `BSTree` and how you call this function from main(). – Lundin Dec 18 '18 at 14:11
  • @gsamaras thank you for your help. – programmingnoob Dec 18 '18 at 17:26
  • @Groo i am not allowed to have a help function for this one. – programmingnoob Dec 18 '18 at 17:26
  • @Lundin i have edited and added the rest of the code. – programmingnoob Dec 18 '18 at 17:26
  • @4386427 the tree will not be balanced if i start from index zero, or have i misunderstood your question perhaps? – programmingnoob Dec 18 '18 at 17:27

1 Answers1

1

To answer your question, the "start variable" should be a pointer to a subarray. Given an array a[10], you should call the function with &a[0] with size 5 and another call with &a[5] with size 5 again.

Correct me if I misunderstood what you meant by start variable.

John
  • 1,012
  • 14
  • 22