LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2011, 11:46 PM   #1
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
I tried using this code for creating and displaying a binary search tree...


But it always displays the message for an "empty treee" when i call the display()...

Code:
#include<stdio.h>
#include<malloc.h>


struct node
	{ int data;
	  struct node *lchild;
	  struct node *rchild;
	}*root;


struct node * add(int,struct node *);
void display(struct node *,int);


int main()
{
	int i,n;
	root=NULL;
	do
	{
	printf("1)Add 2)Display 3)Quit");
	printf("\nEnter your choice:\n");
	scanf("%d",&i);
	switch(i)
	{
		case1:  printf("\nEnter the elememt to be addeed to the bst");
			scanf("%d",&n);
			root=add(n,root);  break;

		case 2: if(root==NULL)
			printf("\nEmpty tree"); /*This is the message whenever
			else                      display() called*/
			{
				display(root,1);
			}	
			break;


		case 3:  break;


		default: printf("\nInvalid Choice");
	}
	}while(i!=3);
	
	return(0);
}

struct node *add( int n,struct node *t_start)
{
	struct node *t=t_start;
	if(t==NULL)
	{	
		struct node *tmp=malloc(sizeof(struct node));
		tmp->data=n;
		tmp->lchild=tmp->rchild=NULL;
		t=tmp;		
	}
	else
	{
		if(n<(t->data))
		{	
			t->lchild=add(n,t->lchild);
		}
		else if(n>(t->data))
		{
			t->rchild=add(n,t->rchild);
		}
	}

return(t_start);
}
	
void display(struct node *t,int level)
{
	int i;
	if(t);
	{
		display(t->rchild,level+1);
		printf("\n");
		for(i=0;i<level;i++)
		{
		printf(" ");
		printf("%d",t->data);
		display(t->lchild,level+1);
		}
	}
}
Help me finding the error in the code........

Last edited by mscoder; 07-19-2011 at 05:22 AM.
 
Old 07-19-2011, 01:32 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hint:
Is "t" in your add() function ever actually NULL?
(trick question: the answer is "no, it isn't").

Suggestion:
Step through the code under the debugger.

If you're using "gcc", then "gdb" is a good choice of debugger:

http://cs.baylor.edu/~donahoo/tools/gdb/tutorial.html
<= One of many short, simple tutorials you can find.

Single step through the code (you won't have to go far).

Print out the values of key variables (like "t", when it's passed into "add()").

You should have your binary tree working in no time
 
0 members found this post helpful.
Old 07-19-2011, 05:11 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mscoder View Post
Code:
        if(n<(t->data))
        {    
            t->lchild=insert(n,t->lchild);
        }
        else if(n>(t->data))
        {
            t->rchild=insert(n,t->rchild);
        }
Help me finding the error in the code........
What is the insert function called from add? It looks like you intended to recursively call add.
 
0 members found this post helpful.
Old 07-20-2011, 09:56 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mscoder View Post
Code:
struct node *add( int n,struct node *t_start)
{
    struct node *t=t_start;
    if(t==NULL)
    {    
        struct node *tmp=malloc(sizeof(struct node));
        tmp->data=n;
        tmp->lchild=tmp->rchild=NULL;
        t=tmp;        
    }
...
return(t_start);
}
Notice that you create the new node and point to it using the variable t, but you return t_start, which is still NULL.
Quote:
Help me finding the error in the code.
I assume that you were the one who flagged the first two replies as not helpful. Both were constructive replies from which you should have gotten guidance. Flagging them as not helpful does not improve your chances of getting more help (though this time, I chose to overlook that).

Also, you edited only your original post to correct the error I pointed out in my original reply. An edit to just the original post is very easy to miss in this forum. You should have replied to my reply to at least acknowledge that you corrected that error (and to ask for more help, since it wasn't the only error you needed to correct).

Last edited by johnsfine; 07-20-2011 at 10:01 AM.
 
  


Reply

Tags
printf



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C Binary Search Tree bnixon10 Programming 4 04-05-2008 09:46 PM
Binary search tree in C Zeno McDohl Programming 3 01-27-2008 05:07 PM
In search of binary translator source code RevenantSeraph Programming 5 04-14-2007 03:49 PM
Binary Search Tree in Python remissed Programming 3 05-07-2006 11:28 PM
Binary search tree insertion in java ksgill Programming 6 02-12-2004 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration