BST insertion

R

Roshan

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) ;
if(tmp)
printf("%d Duplicate ", arr );
}

getch();

return 0 ;

}
 
B

Ben Bacarisse

Roshan said:
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) ;
if(tmp)
printf("%d Duplicate ", arr );
}

getch();

return 0 ;
}


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top