-
Binary search needed
Hi guys...cant work out to search for a customer name in my binary tree....any help wold be appreciated....keep getting lost with the tutorials
Code:
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
struct custdata
{
char cname[20];
char custemail[30];
int numorders;
};
struct node
{
struct node *left;
struct node *right;
struct custdata *cdata;
};
struct node * init(struct node *);
struct custdata * initcustdata(struct custdata *);
int menu(int);
struct custdata * getdetails(struct custdata *);
struct node * add(struct node *,struct custdata *);
struct node * display(struct node *);
int flag = 0;
int main()
{
int choice;
struct node * tree;
struct custdata * cdata;
tree = init (tree);
cdata = initcustdata (cdata);
do
{
choice = menu(choice);
switch(choice)
{
case 1 :
cdata = getdetails(cdata);
tree = add(tree,cdata);
break;
case 2 :
display(tree);
break;
case 3 :
printf("EXITING ! \n");
break;
case 4 :
break;
}
}while(choice != 4);
}
struct node * init (struct node *tree)
{
printf("in init\n");
tree = NULL;
return tree;
}
struct custdata * initcustdata (struct custdata *cdata)
{
cdata = NULL;
return cdata;
}
int menu (int choice)
{
printf("PRESS 1 to ADD to BINARY TREE\n");
printf("PRESS 2 to DISPLAY SORTED BINARY TREE\n");
printf("PRESS 3 to EXIT the BINARY TREE\n");
scanf("%d",&choice);
return choice;
}
struct custdata * getdetails(struct custdata *cdata)
{
cdata = malloc(30);
printf("Enter Customer Name to add to record \n");
fflush ( stdin );
gets(cdata->cname);
fflush ( stdin );
printf("Enter Customer email to add to record \n");
scanf("%s",cdata->custemail);
fflush ( stdin );
printf("Enter quantity required to add to record \n");
scanf("%d",&cdata->numorders);
fflush ( stdin );
return cdata;
}
struct node * add (struct node *tree,struct custdata *cdata)
{
if(tree == NULL)
{
tree = malloc(30);
tree->left = NULL;
tree->right= NULL;
tree->cdata = cdata;
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)<0)
{
tree->left= add(tree->left,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)==1)
{
tree->right = add(tree->right,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)==0)
{
printf("Name matches incrementing orders field \n");
tree->cdata->numorders++;
return tree;
}
}
struct node * display (struct node *tree)
{
if(tree!=NULL)
{
display(tree->left);
printf("Customer Name is %s \n",tree->cdata->cname);
printf("Customer Email is %s \n",tree->cdata->custemail);
printf("Number of Orders is %d \n",tree->cdata->numorders);
display(tree->right);
}
}
-
> cdata = malloc(30);
1. Where did you get 30 from?
2. Why are you overwriting the input parameter to the function.
> if(strcmp(cdata->cname,tree->cdata->cname)==1)
Read the manual for strcmp() again.
<0
==0
>0
Are the only tests you should be performing.
Also, try to indent your code better and avoid using tabs at all in your code editor.
It might be able to cope with tabs, but the board makes a right royal mess of them.
> fflush ( stdin );
> gets(cdata->cname);
Read the FAQ - both of these are truly evil
Forget about fflush(stdin) and read all input using fgets() to a temporary buffer.
Follow the fgets() call with data validation / conversion as appropriate.