BST insertion

Discussion in 'C Programming' started by Roshan, Jan 28, 2011.

  1. Roshan

    Roshan Guest

    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 ;

    }
    Roshan, Jan 28, 2011
    #1
    1. Advertising

  2. Roshan <> 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) ;
    > if(tmp)
    > printf("%d Duplicate ", arr );
    > }
    >
    > getch();
    >
    > return 0 ;
    > }


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

    --
    Ben.
    Ben Bacarisse, Jan 28, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Edwards

    BST & recursion

    Andrew Edwards, Jul 30, 2003, in forum: C++
    Replies:
    12
    Views:
    760
    Karl Heinz Buchegger
    Aug 4, 2003
  2. Rupert harrison

    BST

    Rupert harrison, Sep 15, 2003, in forum: C++
    Replies:
    2
    Views:
    544
    Karl Heinz Buchegger
    Sep 15, 2003
  3. Ridimz

    BST: remove less than

    Ridimz, Oct 4, 2003, in forum: C++
    Replies:
    8
    Views:
    520
    Josh Sebastian
    Oct 7, 2003
  4. Ice
    Replies:
    4
    Views:
    4,996
  5. puzzlecracker

    LL TO BST

    puzzlecracker, Jan 19, 2005, in forum: C++
    Replies:
    2
    Views:
    384
    Joseph Turian
    Jan 19, 2005
Loading...

Share This Page