BST insertion
- From: Roshan <kumarvis@xxxxxxxxx>
- Date: Fri, 28 Jan 2011 01:58:24 -0800 (PST)
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 ;
}
.
- Follow-Ups:
- Re: BST insertion
- From: Ben Bacarisse
- Re: BST insertion
- Prev by Date: Re: inline vs. function pointers
- Next by Date: Re: inline vs. function pointers
- Previous by thread: [newbie] return local variable (or pointer to object) from function
- Next by thread: Re: BST insertion
- Index(es):
Relevant Pages
|