help ,tree's

This is a discussion on help ,tree's within the C Programming forums, part of the General Programming Boards category; Code: struct node { char *string; struct node *left; struct node *right; }; struct node *leaf; main(){ leaf = (struct ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    155

    help ,tree's

    Code:
    struct node
    {
      char  *string;
      struct node *left;
      struct node *right;
    };
    
    struct node *leaf;
    main(){
    leaf = (struct node *)malloc( sizeof( struct node ) );
    
    FILE *in=fopen("c:\\str6.txt","r");
    char str[40];
    fscanf(in,"%s",str);
    leaf->string=str;
    fscanf(in,"%s",str);
    (leaf->left)=(struct node *)malloc( sizeof( struct node ) );
    ((leaf->left)->string)=str;
    printf("\n\n");
    
    
    printf("%s",(leaf->string));
    printf("%s",((leaf->left)->string));
    
    }


    the first two strings in the file are "qwert" ,"asdfg"


    but the output is showing as
    asdfgasdfg

    when i want
    qwertasdfg


    whats wrong with the code?,i am just learning tree's.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > leaf = (struct node *)malloc( sizeof( struct node ) );
    Don't cast malloc in C, see the FAQ
    Also, you should begin with an empty tree with something like

    struct node *tree = NULL;

    > ((leaf->left)->string)=str;
    You're just assigning one pointer to another here. If you update str, then all the nodes which point at str also change as well.

    You should really have a separate function, which you would call with
    tree = addNode ( tree, str );

    addNode then does
    - allocates a new node
    - allocates space for a copy of str
    - finds the correct place to insert the node into the tree
    - returns the (possibly changed) tree

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155

    help ,tree traversing.

    Code:
    struct node 
    {
      char  string[20];
      struct node *left;
      struct node *right;
    }
     addnode(struct node **tree,char *str){
    struct node *temp;
    temp=(struct node *)malloc( sizeof( struct node ) );
    memcpy (temp->string,str,strlen(str)+1);
    if(*tree==NULL){
    (*tree=temp);
    }
     else { (((*tree)->left)=temp);(*tree=temp);}
    }
    
    
    
    
    
    
    main(){
    
    
    struct node root ;
    struct node *tree=NULL;
    struct node **tree_p;
    tree_p= &tree;
    FILE *in=fopen("c:\\str6.txt","r");
    char *str=(char *)malloc(30);
    for(int inc=1;inc<=10;inc++){
    fscanf(in,"%s",str);
    addnode(tree_p,str,start);
    }
    }


    addnode function updates tree to point to the latest position.
    how do i get to the root now?
    please advise me over this,i am just learning tree's and i have very little experience on how to creat functions for handling tree's.
    this tree is being used as link list,that is why only adding to the left.
    Last edited by qqqqxxxx; 02-02-2006 at 03:39 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > temp=(struct node *)malloc( sizeof( struct node ) );
    OK, did you read the FAQ?

    > addnode(tree_p,str,start);
    Your function apparently takes only 2 parameters.
    Also, you don't need the extra pointer, you should just do
    addnode( &tree,str);

    > memcpy (temp->string,str,strlen(str)+1);
    Better known as
    strcpy (temp->string,str);

    Most tree functions are recursive, you follow a sequence of left and right nodes until you find an empty one, then you insert the new node.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21