Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Warning in search function

Posted on 2011-09-14
3
Medium Priority
?
271 Views
Last Modified: 2012-05-12
I am getting warning in search function...

Warning is "function should return a value.."

How to get rid of this?.
#include<stdio.h>
#include<conio.h>

struct Node
{
 int info;
 struct Node *left;
 struct Node *right;
};

typedef struct Node node;

node *root=NULL;

//FUNCTION TO CREATE BST
void create(int item)
{
 node *new,*currptr,*ptr;

 new=(node*)malloc(sizeof(node));
 new->info=item;
 new->left=NULL;
 new->right=NULL;

 if(root==NULL)
 root=new;

 else
 {
  currptr=root;

  while(currptr!=NULL)
  {
   ptr=currptr;
   currptr=(item>currptr->info)?currptr->right:currptr->left;
  }

  if(item<ptr->info)
  ptr->left=new;
  else
  ptr->right=new;
 }
}

//FUNCTION TO DISPLAY BST
disp(node *ptr,int level)
{
 int i;

 if(ptr!=NULL)
 {
  disp(ptr->right,level+1);
  for(i=0;i<level;i++)
  printf("    ");
  printf("%3d\n",ptr->info);
  disp(ptr->left,level+1);
 }
 return 0;
}

//FUNCTION TO SEARCH AN ELEMENT IN BST
node *search(node *temp,int item)
{
 if(temp==NULL)
  return (NULL);
 else if(item<temp->info)
  search(temp->left,item);
 else if(item>temp->info)
  search(temp->right,item);
 else
  return (temp);
}

//FUNCTION TO DISPLAY BST USING PRE-ORDER TRAVERSAL.
void pre_order(node *ptr)
{
 if(ptr)
 {
  printf("%3d",ptr->info);
  pre_order(ptr->left);
  pre_order(ptr->right);
 }
}

//FUNCTION TO DISPLAY BST USING IN-ORDER TRAVERSAL.
void in_order(node *ptr)
{
 if(ptr)
 {
  in_order(ptr->left);
  printf("%3d",ptr->info);
  in_order(ptr->right);
 }
}

//FUNCTION TO DISPLAY BST USING POST-ORDER TRAVERSAL.
void post_order(node *ptr)
{
 if(ptr)
 {
  post_order(ptr->left);
  post_order(ptr->right);
  printf("%3d",ptr->info);
 }
}

void main()
{
 int item,ch,n,i;
 clrscr();

 printf("\nBINARY SEARCH TREE OPERATION");
 printf("\n****************************");
 printf("\n1.CREATE BST\n2.DISPLAY BST\n3.SEARCH\n4.DISPLAY BST IN PRE-ORDER\n5.DISPLAY BST IN IN-ORDER\n6.DISPLAY BST IN POST-ORDER\n7.EXIT");

 while(1)
 {
  printf("\n\nEnter Your Choice: ");
  scanf("%d",&ch);

  switch(ch)
  {
   case 1:
	  printf("\nEnter The Number Of Nodes: ");
	  scanf("%d",&n);
	  for(i=0;i<n;i++)
	  {
	   printf("\nEnter The Data For The Node: ");
	   scanf("%d",&item);
	   create(item);
	  }
	  break;

   case 2:
	  printf("\nThe Binary Search Tree Is: \n\n");
	  disp(root,1);
	  break;

   case 3:
	  printf("\nEnter The Item To Be Searched: ");
	  scanf("%d",&item);

	  search(root,item);

	  if((search(root,item))!=NULL)
	  printf("Item Found");

	  else
	  {
	   create(item);
	   printf("\nItem Not Found In BST And Hence Inserted Now In BST\n");
	  }
	  break;

   case 4:
	  printf("\nThe Elements Of BST Using Pre-Order Traversal\n");
	  pre_order(root);
	  break;

   case 5:
	  printf("\nThe Elements Of BST Using In-Order Traversal\n");
	  in_order(root);
	  break;

   case 6:
	  printf("\nThe Elements Of BST Using Post-Order Traversal\n");
	  post_order(root);
	  break;

   case 7:
	  exit(0);

   default:
	  printf("\nPlease Enter The Choice Between 1 to 7");
  }
 }
}

Open in new window

0
Comment
Question by:adamssap
  • 3
3 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 36538697
node *search(node *temp,int item)

Not all paths appear to return a value. Look at how you've coded the if clauses. Probably the simplest solution is to remove the final 'else' and then it will always return a value when it gets there.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 36538705
actually, I suspect this is what you want...
node *search(node *temp,int item)
{
 if(temp==NULL)
  return (NULL);
 else if(item<temp->info)
  return search(temp->left,item);
 else if(item>temp->info)
  return search(temp->right,item);
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 36538720
Ooops, I got a bit carried away with my editing and accidentally removed the final return value. Ignore the previous code.
node *search(node *temp,int item)
{
 if(temp==NULL)
  return (NULL);
 else if(item<temp->info)
  return search(temp->left,item);
 else if(item>temp->info)
  return search(temp->right,item);

  return temp;
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question