help with a garbage value

Discussion in 'C++' started by andrew browning, May 16, 2006.

  1. this program is a binary tree using nodes. it should do an in-order
    print, which it does. however, it inserts a garbage value at the end
    of the print. ex: cin >> 1 2 3 4 5 prints 1 2 3 4 5 7884788. can
    anyone help? value_type is an integer.

    constructors:

    tree():data(value_type()),left(NULL), right(NULL){}

    tree(value_type vt, tree* l = NULL, tree* r = NULL):
    data(vt), left(l), right(r) {};

    insert function:

    tree::tree* tree::insert(tree* node, value_type data){
    if (node == NULL){
    return(new tree(data));
    }else

    if(data <= node->data){
    node->left = insert(node->left, data);
    }else
    if(data >= node->data){
    node->right = insert(node->right, data);
    }
    return node;
    }


    driver:

    int main(){

    tree t1;
    int user_input;
    tree* tree_ptr = new tree(user_input);


    while(cin >> user_input && cin.peek() != EOF){
    t1.insert(tree_ptr, user_input);
    }
    t1.inorder_print(tree_ptr);



    return 0;

    }
    andrew browning, May 16, 2006
    #1
    1. Advertising

  2. andrew browning

    Rolf Magnus Guest

    andrew browning wrote:

    > this program is a binary tree using nodes. it should do an in-order
    > print, which it does. however, it inserts a garbage value at the end
    > of the print. ex: cin >> 1 2 3 4 5 prints 1 2 3 4 5 7884788. can
    > anyone help? value_type is an integer.
    >
    > constructors:
    >
    > tree():data(value_type()),left(NULL), right(NULL){}
    >
    > tree(value_type vt, tree* l = NULL, tree* r = NULL):
    > data(vt), left(l), right(r) {};


    Just my opinion:
    There is no significant limit to the number of lines you can use in a
    program. You could increase readability by not putting everything in as few
    lines as possible.

    >
    > insert function:
    >
    > tree::tree* tree::insert(tree* node, value_type data){
    > if (node == NULL){
    > return(new tree(data));
    > }else
    >
    > if(data <= node->data){
    > node->left = insert(node->left, data);
    > }else
    > if(data >= node->data){
    > node->right = insert(node->right, data);
    > }
    > return node;
    > }
    >
    >
    > driver:
    >
    > int main(){
    >
    > tree t1;
    > int user_input;
    > tree* tree_ptr = new tree(user_input);
    >
    >
    > while(cin >> user_input && cin.peek() != EOF){


    Replace that with:

    while(cin >> user_input){

    > t1.insert(tree_ptr, user_input);
    > }
    > t1.inorder_print(tree_ptr);
    >
    >
    >
    > return 0;
    >
    > }



    And where is your inorder_print() implementation?
    Rolf Magnus, May 16, 2006
    #2
    1. Advertising

  3. Rolf Magnus wrote: And where is your inorder_print() implementation? :

    void inorder_print(tree* node_ptr){
    if(node_ptr != 0){
    inorder_print(node_ptr->get_left());
    std::cout << node_ptr ->get_data() << std::endl;
    inorder_print(node_ptr->get_right());

    i changed the driver as you recommended but i get a 0 at the begining
    of the output. as for the readability, i will keep that in mind when
    asking other people to read it:)
    andrew browning, May 16, 2006
    #3
    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. Cheung, Jeffrey Jing-Yen
    Replies:
    3
    Views:
    1,549
    Cheung, Jeffrey Jing-Yen
    Jul 10, 2003
  2. Replies:
    1
    Views:
    439
    mrstephengross
    Jul 25, 2005
  3. Replies:
    5
    Views:
    360
    Jim Langston
    Apr 23, 2006
  4. sandeep_agarwal

    return value from function became garbage

    sandeep_agarwal, Jul 11, 2008, in forum: C Programming
    Replies:
    0
    Views:
    293
    sandeep_agarwal
    Jul 11, 2008
  5. Benjie Chen
    Replies:
    6
    Views:
    167
    Caleb Clausen
    Jan 7, 2010
Loading...

Share This Page