Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=right=NULL;
- }
- };
- void Inorder(Node* root){
- if(root==NULL)
- return;
- Inorder(root->left);
- cout<<" "<<root->data;
- Inorder(root->right);
- }
- void Insert(Node **root_ref,int key){
- if(*root_ref==NULL){
- *root_ref=new Node(key);
- return;
- }
- if((*root_ref)->data<key)
- Insert(&((*root_ref)->right),key);
- else
- if((*root_ref)->data>key)
- Insert(&((*root_ref)->left),key);
- }
- Node* ConvertIntoBST(int arr[],int n){
- Node* root=NULL;
- for(int i=0;i<n;i++)
- Insert(&root,arr[i]);
- return root;
- }
- int main(){
- Node *root=NULL;
- cout<<"enter total no. of elements in an array ";
- int n;
- cin>>n;
- cout<<"enter elements in sorted order ";
- int arr[n];
- for(int i=0;i<n;i++)
- cin>>arr[i];
- root=ConvertIntoBST(arr,n);
- Inorder(root);
- }
Add Comment
Please, Sign In to add comment