//To implement binary search tree using linked list.
#include<stdio.h>
int count = 0;
typedef struct Btree{
int data;
struct Btree *left;
struct Btree *right;
};
struct Btree* insert(struct Btree *bt,int n){
if(bt==NULL){
bt= (struct Btree *) malloc(sizeof(struct Btree));
bt->data=n;
bt->left=NULL;
bt->right=NULL;
}
else if(n<=bt->data)
bt->left = insert(bt->left,n);
else
bt->right = insert(bt->right,n);
return bt;
}
int searchElement(struct Btree *bt,int item){
if(bt!=NULL){
count++;
if(item<bt->data)
searchElement(bt->left,item);
else if(item>bt->data)
searchElement(bt->right,item);
else if(item == bt->data)
return count;
}
else{
return 0;
}
}
int main(){
struct Btree *bt;
int loc;
bt=NULL;
int item;
while(1){
count = 0;
printf("which operation you want to perform....\n");
printf("1. Insert an element.\n");
printf("2. Search an element.\n");
int op;
int sEle;
scanf("%d",&op);
switch(op){
case 1:
printf("Enter a number.\n");
scanf("%d",&item);
bt = insert(bt,item);
break;
case 2:
printf("Enter an number for search.");
scanf("%d",&sEle);
loc = searchElement(bt,sEle);
if(loc==0)
printf("Element not found.");
else
printf("element found at %d ",loc);
printf("\n");
break;
default:
printf("Please choose correct option...\n\n");
}
}
}
It's very useful blog post with informative and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now. Go through Blog post of crs info solutions. I just read these Reviews of crs really great. You can now Contact Us of crs info solutions. You enroll for Pega Training at crs info solutions.
ReplyDelete