Problem:-
Given a sorted array with unique integer element , write an algorithm to create a binary search tree with minimal height.
Solution :-
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *left ,*right;
};
struct node *root=NULL;
// function for create createnode
struct node *createNode()
{
struct node *n;
n=new node;
return n;
}
void insert(int m)
{
node *temp,*t;
temp=createNode();
temp->data=m;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
root=temp;
else
{
t=root;
while(t!=NULL)
{
if(t->data>m)
{
if(t->left==NULL)
{
t->left=temp;
t=t->left;
}
t=t->left;
}
else
{
if(t->right==NULL)
{
t->right=temp;
t=t->right;
}
t=t->right;
}
}
}
}
void CreateBinarySearchTree(int *a,int l,int r)
{
if(l<=r)
{
int mid=(l+r)/2;
insert(a[mid]);
CreateBinarySearchTree(a,l,mid-1);
CreateBinarySearchTree(a,mid+1,r);
}
}
// function for preorder traversal
void preorder_traversal(struct node *temp)
{
if(temp)
{
printf("%d ",temp->data);
preorder_traversal(temp->left);
preorder_traversal(temp->right);
}
}
int main()
{
int arr[50];
int n;
cout<<"Enter the number of the element in the array"<<endl;
cin>>n;
cout<<"Enter the element in the array"<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
CreateBinarySearchTree(arr,0,n-1);
printf("Preorder traversal of the tree\n");
preorder_traversal(root);
return 0;
}
output:-
Enter the number of the element in the array
10
Enter the element in the array
1 3 5 6 11 12 13 14 23 34
Preorder traversal of the tree
11 3 1 5 6 14 12 13 23 34
Recommended Post:-
- Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-Data structure:-- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
MCQs:-
0 Comments