Thread: a strange problem :(

  1. #1
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    a strange problem :(

    I study the data structure,
    met a strange problem.
    I used preorder method to create a binary tree;
    code :
    Code:
    #include<stdio.h>
    typedef struct node {
            int   data;
            struct node *left;
            struct node *right;}btree;
    
    btree *Binarytree(btree *bt)
    { 
      int data;
      scanf("%d",&data);
      if(data==0)
        bt=NULL;
      else
      { bt=(btree *)malloc(sizeof(btree));
        bt->data=data;
        Binarytree(bt->left);
        Binarytree(bt->right);
      }
      return bt;
    }
    
    main()
    {
      btree *bt;
      bt=(btree *)malloc(sizeof(btree));
      printf("\n\t\tThe Binary Tree:\n");
      printf("\nPlease input the data of binary tree(0 for NULL) :\n");
      Binarytree(bt);
    }
    Now I want to traverse the tree,and put out the result---
    each node's data;
    And the problem comes, when I traverse,it can't traverse the
    left child and right child;

    Thanx very much!
    there is angel,who love to see u.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You use preorder to traverse the tree not to create it.
    I can tell your function won't work because it will never
    modify bt->left or bt->right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  2. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  3. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  4. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM