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