unsorted array to bst

Solutions on MaxInterview for unsorted array to bst by the best coders in the world

showing results for - "unsorted array to bst"
Roberto
10 May 2019
1#include<iostream>
2using namespace std;
3typedef struct node
4{
5    int value;
6    node * pLeft;
7    node * pRight;
8    node(int val = 0)
9    {
10        value = val;
11        pRight = NULL;
12        pLeft = NULL;
13    }
14}node;
15 
16void insert(node ** pRoot, int val)
17{
18    if(*pRoot == NULL)
19        *pRoot = new node(val);
20    else if((*pRoot)->value <= val)
21        insert(&((*pRoot)->pRight), val);
22    else if((*pRoot)->value > val)
23        insert(&((*pRoot)->pLeft), val);
24}
25 
26node * getBST(int * arr, int size)
27{
28    node * pRoot = NULL;
29    for(int i = 0; i < size; i++)
30        insert(&pRoot, arr[i]);
31    return pRoot;
32}
33 
34void inOrderTraversal(node * pRoot)
35{
36    if(pRoot && pRoot->pLeft)
37        inOrderTraversal(pRoot->pLeft);
38    if(pRoot)
39        std::cout<<pRoot->value<<" , ";
40    if(pRoot && pRoot->pRight)
41        inOrderTraversal(pRoot->pRight);
42 
43}
44int main()
45{
46    int arr[] = {10,5,15,5,6,7,8,89};
47    node * pRoot = getBST(arr, sizeof(arr)/sizeof(int));
48    inOrderTraversal(pRoot);
49    std::cout<<std::endl;
50    return 0;
51}
queries leading to this page
unsorted array to bst runtimeconstruct bst of given arraycreate binary search tree from unsorted arrayunsorted array to bstcreate balanced binary tree from unsorted arrayunsorted array create bst and searchjava create a bst from a unsorted arraybinary search tree from unsorted arrayunsorted array to bst javaconstruct bst from sortted arraybalanced bst from unsorted arraycreate bst from unsorted arrayconstruct a binary tree form an unsorted arrayjava unsorted array to bstsorted array into bstsorted array to bstconvert unsorted array to bstbst from unsorted arraysearch in binary search tree for array unsortedconstruct binary search tree from array in ccreating bst from a sorted array algorithmbst steps to sort unsorted arrayunsorted array to binary search treeinput numbers to make a balanced binary treeconstruct binary search tree from unsorted arrayunsorsted array to bstcreating bst from a sorted array algorithm pythona sorted array to bst in c 2b 2bbinary search tree array unsortedgiven an array of unique elements 2c construct a binary search tree after inserting every new node 2c print the height of the tree given an unsorted array create a bstcosntruct binary search tree from unsorted arraycreate balanced binary tree from unsorted array onlinecan we sort an array using bstconvert a sorted array to bstconvert sorted array to bstconstruct binary search tree from arrayhow to build a binary search tree from an arraydraw a bst from arrayarray to bstcreate a bst from list of numbersunsorted array to bst