unsorted array to bst
C++
#include<iostream>
using namespace std;
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;
}
Also in C++:
- Title
- how do for loops on c++
- Category
- C++
- Title
- how to print a 2d array in c++
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- Html tabulation
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- map vs unordered_map in C++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- c++ while true loop
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- insertion sort in c++ program
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- inserting an element in an set c++
- Category
- C++
- Title
- range of int
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- compile c++ program
- Category
- C++
- Title
- Qt asynchronous HTTP request
- Category
- C++
- Title
- c++ isalphanum
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- cs1955 unity vector3
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- euler phi gfg
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- c++ random
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- How to traverse in a tree iterative C++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- namespace file linking c++
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- deque c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- compare string c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ string
- Category
- C++