# Create a Binary Search Tree from an array

Lets discuss how to create a BST From an array.

Tree’s node structure is as follows,

```typedef struct node
{
int value;
node * pLeft;
node * pRight;
node(int val = 0)
{
value = val;
pRight = NULL;
pLeft = NULL;
}
}node;
```

Now create a getBST function that accepts an integer pointer and size of array.
Inside this function it creates a pointer to root node and points it to NULL.
Then traverses through all of the array elements and calls the insert function on each of them with root node pointer and current element as parameters. This insert function inserts the element in BST.

```node * getBST(int * arr, int size)
{
Â Â  Â node * pRoot = NULL;
Â Â  Â for(int i = 0; i < size; i++)
Â Â  Â Â Â  Â insert(&pRoot, arr[i]);
Â Â  Â return pRoot;
}
```

Lets create the insert function,

Insert function accepts a pointer to a node pointer and value to be inserted in BST.
It first checks that if passed node pointer is NULL then assigns the new node to passed pointer.
But if the passed pointer is not NULL then it compares the new value with the value of root node.
If it’s value is less than or equal to root node’s value the in will recursively call the insert function on left child of root node else it will recursively call the insert function on right child of root node.

```void insert(node ** pRoot, int val)
{
Â Â  Â if(*pRoot == NULL)
Â Â  Â Â Â  Â *pRoot = new node(val);
Â Â  Â else if((*pRoot)->value <= val)
Â Â  Â Â Â  Â insert(&((*pRoot)->pRight), val);
Â Â  Â else if((*pRoot)->value > val)
Â Â  Â Â Â  Â insert(&((*pRoot)->pLeft), val);
}
```

Lets see how to use the above code,

```    int arr[] = {10,5,15,5,6,7,8,89};
Â Â  Â node * pRoot = getBST(arr, sizeof(arr)/sizeof(int));
inOrderTraversal(pRoot);
```

Complete executable code is as follows,

[code language=”css” collapse=”true”]
#include<iostream>

typedef struct node
{
Â Â  Â int value;
Â Â  Â node * pLeft;
Â Â  Â node * pRight;
Â Â  Â node(int val = 0)
Â Â  Â {
Â Â  Â Â Â  Â value = val;
Â Â  Â Â Â  Â pRight = NULL;
Â Â  Â Â Â  Â pLeft = NULL;
Â Â  Â }
}node;

void insert(node ** pRoot, int val)
{
Â Â  Â if(*pRoot == NULL)
Â Â  Â Â Â  Â *pRoot = new node(val);
Â Â  Â else if((*pRoot)->value <= val)
Â Â  Â Â Â  Â insert(&((*pRoot)->pRight), val);
Â Â  Â else if((*pRoot)->value > val)
Â Â  Â Â Â  Â insert(&((*pRoot)->pLeft), val);
}

node * getBST(int * arr, int size)
{
Â Â  Â node * pRoot = NULL;
Â Â  Â for(int i = 0; i < size; i++)
Â Â  Â Â Â  Â insert(&pRoot, arr[i]);
Â Â  Â return pRoot;
}

void inOrderTraversal(node * pRoot)
{
Â Â  Â if(pRoot && pRoot->pLeft)
Â Â  Â Â Â  Â inOrderTraversal(pRoot->pLeft);
Â Â  Â if(pRoot)
Â Â  Â Â Â  Â std::cout<<pRoot->value<<" , ";
Â Â  Â if(pRoot && pRoot->pRight)
Â Â  Â Â Â  Â inOrderTraversal(pRoot->pRight);

}
int main()
{
Â Â  Â int arr[] = {10,5,15,5,6,7,8,89};
Â Â  Â node * pRoot = getBST(arr, sizeof(arr)/sizeof(int));
Â Â  Â inOrderTraversal(pRoot);
Â Â  Â std::cout<<std::endl;
Â Â  Â return 0;
}

[/code]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top