int main(int argc, char *argv[]) { int arr[] = { 1, 4, 5, 7, 9, 11, 13}; tree_t *t = NULL; t = convert_to_bst(arr, 0, 6); print(t); }
//Converts a sorted array to BST struct tree *convert_to_bst(int left,int right){ struct tree *temp=NULL; int mid; if(left>right) return NULL; mid=left+(right-left)/2; temp=newnode(a[mid]); temp->left=convert_to_bst(left,mid-1); temp->right=convert_to_bst(mid+1,right); return temp; }
void main(){ root=convert_to_bst(0,5); preorder(root); }
tree_t *convert_to_bst(int arr[], int start, int end) { tree_t *n = NULL; int split; if (start > end) { return (NULL); } if (start == end) { n = create_new_node(arr[start]); return (n); } split = find_split(start, end); printf("convert_to_bst (start, end, split) : %d, %d, %d\n", start, end, split); n = create_new_node(arr[split]); n->right = convert_to_bst(arr, start, split - 1); n->left = convert_to_bst(arr, split + 1, end); return (n); }