Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Created July 18, 2016 20:47
Show Gist options
  • Save haridutt12/3d1e74fd3443ab890f1d9661e22ab043 to your computer and use it in GitHub Desktop.
Save haridutt12/3d1e74fd3443ab890f1d9661e22ab043 to your computer and use it in GitHub Desktop.
#include<stdio.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));
scanf("%d",&h->data);
h->left=NULL;
h->right=NULL;
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;
if(t->data <= p->data)
{
p=p->left;
}
else
{
p=p->right;
}
}
if(t->data <= q->data)
{
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);
}
int search(struct bt *h ,int n)
{
struct bt *p,*q;
p=h;
while(p!=NULL)
{
q=p;
if(n <= p->data)
{
p=p->left;
}
else
{
p=p->right;
}
}
if(n == q->data)
{
return 1;
}
else
{
return -1;
}
}
int main()
{
struct bt *head;
head=create(head);
pre(head);
printf("enter the value u wanna search\n");
int t;
scanf("%d",&t);
int p;
p=search(head,t);
printf("%d",p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment