Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By laserlight

Problems with binary tree

This is a discussion on Problems with binary tree within the C++ Programming forums, part of the General Programming Boards category; i tried to make a binary tree, but when i print it, it shows nothing. can someone help me? here's ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Problems with binary tree

    i tried to make a binary tree, but when i print it, it shows nothing.
    can someone help me? here's the code:

    Code:
    #include<iostream>
    #include<Windows.h>
    #include<string>
    #include<fstream>
    #include<cstdarg>
    
    using namespace std;
    
    
    struct data
    {
        int num;
        struct data *left, *right;
    }*root = NULL;
    
    void insert(int num, struct data *node)
    {
        if(node == NULL)
        {
            node = new data;
            node->num = num;
            node->left = node->right = NULL;
        }
        else
        {
            if(num < node->num)
            {
                insert(num, node->left);
            }
            else if(num > node->num)
            {
                insert(num, node->right);
            }
        }
    }
    
    void print(struct data *node)
    {
        if(node!=NULL)
        {
            print(node->left);
            cout << node->num << " ";
            print(node->right);
        }
    }
    
    int main()
    {
        insert(6, root);
        insert(4, root);
        insert(7, root);
        insert(3, root);
        insert(8, root);
        print(root);
        cin.get();
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    1. Why make root global?

    2. Since this is C++, you don't need to put "struct" in front of data all the time.

    3. Your problem is that insert only modifies it's local copy of root. Use a reference to the root pointer like this:
    Code:
    #include<iostream>
    using namespace std;
    
    struct data {
        int num;
        data *left, *right;
    };
    
    void insert(int num, data*& node) {
        if(node == 0) {
            node = new data;
            node->num = num;
            node->left = node->right = NULL;
        }
        else
            if(num < node->num)
                insert(num, node->left);
            else if(num > node->num)
                insert(num, node->right);
    }
    
    void print(struct data *node) {
        if(node!=NULL) {
            print(node->left);
            cout << node->num << " ";
            print(node->right);
        }
    }
    
    int main() {
        data* root = 0;
        insert(6, root);
        insert(4, root);
        insert(7, root);
        insert(3, root);
        insert(8, root);
        print(root);
        return 0;
    }
    aquatorrent likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    lol, i forget about the reference thing :P
    anyway, i make my root global in case i need it later.
    should it be declared on main? i tried to make it on global and it also works

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,633
    Quote Originally Posted by aquatorrent
    anyway, i make my root global in case i need it later.
    That is not a good reason to declare a variable global since you can always declare it when you do need it later. You should declare variables near first use.

    Quote Originally Posted by aquatorrent
    should it be declared on main?
    Probably.

    Quote Originally Posted by aquatorrent
    i tried to make it on global and it also works
    Yes, it also works. However, your program is extremely trivial. Global variables make it more difficult to reason about a larger, more complex, program.
    aquatorrent likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems creating Binary Tree
    By darren78 in forum C++ Programming
    Replies: 4
    Last Post: 07-13-2010, 06:45 AM
  2. Binary Tree Problems
    By P4R4N01D in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2009, 01:16 AM
  3. Problems implementing a binary tree
    By noops in forum C Programming
    Replies: 3
    Last Post: 07-22-2008, 02:39 AM
  4. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  5. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 11:47 PM

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