简体   繁体   中英

How can build a Binary Search Tree from an array of integers in C?

I try to write a function which is used to build a BST from an array of integers. It takes 2 arguments: pointer to the array and the size of the array

  • create the BST with successive inserts and return the pointer to the tree
  • if size is 0, return NULL

sample;

int a[3] = {2,1,3};
return build(a, 3);

My work is here, but there is a problem in the recursion part, I cannot find my mistake, actually I know that I cannot use for loops correctly, but cannot solve.

In my code, firstly I changed format of array in an ascending order, then took the middle number, and made it root, then for left and right parts, I should do same things.

By the way, I have an insert function implementation, but I am not sure, can we need or use it in build function.

typedef struct TreeNode{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;


TreeNode* build(int *array, int size) {
    TreeNode *root = malloc(sizeof(TreeNode));
    int a,i,j;
    int mid = size/2;
    if(size==0)
        return NULL;
    else {
        for(i=0;i<size;i++) {
            for(j=i+1;j<size;j++) {
                if(array[i] > array[j]) {
                    a = array[i];
                    array[i] = array[j];
                    array[j] = a;
                }
            }
        }
        root->val = array[mid];
        for(i=0;i<mid;i++)
            root->left = build(array, mid);
        for(i=(mid+1);i<size;i++)
            root->right = build(array, mid);
        return root;
    }
}

Modify according to your concept:

#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

TreeNode* build(int *array, int size) {
    if(size == 0)
        return NULL;
    else {
        TreeNode *root = malloc(sizeof(TreeNode));//in the case of return NULL there is before if-block, cause of memory leak
        int i, j, mid;
        int swap = 1;

        j = size-1;
        while(swap){//To deter a repeat of the sort
            swap = 0;
            for(i = 0; i < j; ++i) {
                if(array[i] > array[i+1]) {
                    int temp = array[i];
                    array[i] = array[i+1];
                    array[i+1] = temp;
                    swap = 1;
                }
            }
            --j;
        }
        root->val = array[mid = size / 2];
        root->left  = build(array, mid);
        root->right = build(array + mid + 1, size - mid -1);
        return root;
    }
}

void print(TreeNode *p){
    if(p){
        print(p->left);
        printf("%d ", p->val);
        print(p->right);
    }
}

int main(void){
    //test
    int a[] = {1,5,9,2,6,4,8,3,7,0};
    TreeNode *root = build(a, 10);
    print(root);
    //deallocate
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM