Binary search needed

This is a discussion on Binary search needed within the C Programming forums, part of the General Programming Boards category; Hi guys...cant work out to search for a customer name in my binary tree....any help wold be appreciated....keep getting lost ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    8

    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);
    
        }
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  2. deleting all nodes of a binary search tree...
    By sachitha in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2004, 07:19 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. Binary tree search efficiency
    By ExCoder01 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-23-2003, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21