Social Icons

banner image

TREE in data structure

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int info;
struct node *left,*right;
}*root=NULL;
int insert(int n)
{
struct node *ptr=root,*save=NULL;
struct node *temp=new node;
temp->info=n;
temp->left=NULL;
temp->right=NULL;
if(root==NULL)
{
root=temp;
}
else
{
while(ptr!=NULL)
{
if(ptr->info>n)
{
save=ptr;
ptr=ptr->left;
}
else
{
save=ptr;
ptr=ptr->right;
}
if(save->info>n)
{
save->left=temp;
}
else
{
save->right=temp;
}
}
}
while(ptr!=NULL)
{
cout<<ptr->info;
}
}
int preorder(struct node *root)
{
node *ptr=root;
if(root==NULL)
{
return 0;
}
else
{
cout<<ptr->info;
preorder(ptr->left);
preorder(ptr->right);
}
}
int main()
{  
insert(26);
insert(45);
insert(72);
insert(10);
preorder(root);
return 0;
}
TREE in data structure TREE  in data structure Reviewed by Shobhit Goel on November 07, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.