Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Last active July 18, 2016 18:44
Show Gist options
  • Save haridutt12/76d52bca74c355d939cdc242dffa5ca7 to your computer and use it in GitHub Desktop.
Save haridutt12/76d52bca74c355d939cdc242dffa5ca7 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<malloc.h>
struct bt
{
int data;
struct bt *left;
struct bt *right;
};
struct bt* create(struct bt *h)
{
struct bt *p,*q;
int side,y=1;
h=(struct bt *)malloc(sizeof(struct bt));
printf("enter the root value\n");
scanf("%d",&h->data);
h->left=NULL;
h->right=NULL;
int flag;
while(y)
{
p=h;
struct bt *t=(struct bt *)malloc(sizeof(struct bt));
printf("enter value to be added ");
scanf("%d",&t->data);
t->left=NULL;
t->right=NULL;
while(p!=NULL)
{
q=p;
while(p!=NULL){
flag=0;
if(t->data<p->data){
p=p->left;
flag=flag+1;
}
else{
p=p->right;
}
}
}
if(flag>0)
q->left=t;
else
q->right=t;
printf("1 to cont: ");
scanf("%d",&y);
}
return h;
}
void pre(struct bt *h)
{
if(h==NULL){
return;
}
printf("%d\n",h->data);
pre(h->left);
pre(h->right);
}
void main()
{
struct bt *head;
head=create(head);
pre(head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment