Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   BST insertion (http://www.velocityreviews.com/forums/t742725-bst-insertion.html)

Roshan 01-28-2011 09:58 AM

BST insertion
 
I m Trying to make BSTand checking if node already there just
return ,trying to return 1
but here its returning 0 ,

lease help


// btree.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"





#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int data ;
struct Node *left ;
struct Node *right ;
} ;

int insert_node(struct Node **bt ,int num )
{
struct Node *tmp ;
if(*bt==NULL)
{
tmp=(struct Node *)malloc(sizeof(struct Node ));
*bt=tmp ;
(*bt)->data=num ;
(*bt)->left=NULL ;
(*bt)->right=NULL ;
}
else
{
if(num < (*bt)->data)
insert_node(&((*bt)->left),num);
else
{
if (num == (*bt)->data)
return 1 ;
else
if (num > (*bt)->data)
insert_node(&((*bt)->right),num);

}
}
return 0 ;
}


int _tmain(int argc, _TCHAR* argv[])
{

struct Node * bt =NULL ;
int arr[5]= { 1,2,3,4,4 } ;
for(int i = 0 ;i<5 ;i++)
{
int tmp=insert_node(&bt ,arr[i]) ;
if(tmp)
printf("%d Duplicate ", arr[i] );
}

getch();

return 0 ;

}

Ben Bacarisse 01-28-2011 02:16 PM

Re: BST insertion
 
Roshan <kumarvis@gmail.com> writes:

> I m Trying to make BSTand checking if node already there just
> return ,trying to return 1
> but here its returning 0 ,
>
> lease help
>
>
> // btree.cpp : Defines the entry point for the console application.


You may be programming in C++ (.cpp files are often treated as C++ files
by the compiler). If you are, then there are a whole lots of other
issues that should be mentioned. Post in comp.lang.c++ for find out.

> //
>
> #include "stdafx.h"
>
>
>
>
>
> #include<stdio.h>
> #include<stdlib.h>
> #include<conio.h>
> struct Node
> {
> int data ;
> struct Node *left ;
> struct Node *right ;
> } ;
>
> int insert_node(struct Node **bt ,int num )
> {
> struct Node *tmp ;
> if(*bt==NULL)
> {
> tmp=(struct Node *)malloc(sizeof(struct Node ));
> *bt=tmp ;
> (*bt)->data=num ;
> (*bt)->left=NULL ;
> (*bt)->right=NULL ;


FYI, I'd write this as follows:

*bt = tmp = malloc(sizeof *tmp);
tmp->left = tmp->right = NULL;
tmp->data = num;

but I'd also want to take action if malloc fails. You could, for
example, return -1.

> }
> else
> {
> if(num < (*bt)->data)
> insert_node(&((*bt)->left),num);


This insert may produce 0 or 1. In both cases you ignore that return
value and fall down to the "catch-all" return 0 at the bottom.

> else
> {
> if (num == (*bt)->data)
> return 1 ;


Only when the root of the tree contains num will you return 1.

> else
> if (num > (*bt)->data)
> insert_node(&((*bt)->right),num);


Again, zero is returned regardless of what happens in this recursive call.

>
> }
> }
> return 0 ;
> }
>
>
> int _tmain(int argc, _TCHAR* argv[])


This must be what C calls a "freestanding implementation". main is the
more usual entry point.

> {
>
> struct Node * bt =NULL ;
> int arr[5]= { 1,2,3,4,4 } ;
> for(int i = 0 ;i<5 ;i++)
> {
> int tmp=insert_node(&bt ,arr[i]) ;
> if(tmp)
> printf("%d Duplicate ", arr[i] );
> }
>
> getch();
>
> return 0 ;
> }


BTW, your layout is rather peculiar not consistent. It makes the code
harder to read.

--
Ben.


All times are GMT. The time now is 05:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.