Thread: Jumping into C++ cha 17 number 1 (binary tree and sorting)

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    11

    Jumping into C++ cha 17 number 1 (binary tree and sorting)

    Hi guys I m going trough the Jumping into C++ book and im currently on the binary tree chapter. There s an exercise that tells me to write a code where I can add values to a binary tree list and then show them in a sorted order (lowest->highest). here s the code I got so far:
    Code:
    #include <iostream>
    using namespace std;
    struct node
    {
        int key;
        node* p_left = NULL;
        node* p_right = NULL;
    };
    node* insert (node* p_tree, int key)
    {
        if (p_tree == NULL)
        {
            node* p_new_node = new node;
            p_new_node->key = key;
            p_new_node->p_left = NULL;
            p_new_node->p_right = NULL;
            return p_new_node;
        }
        if (key < p_tree->key)
            insert (p_tree->p_left, key);
        else
            insert (p_tree->p_right, key);
    }
    void print (node* p_tree)
    {
        if (p_tree->p_left)
            {
               print (p_tree->p_left);
            }
        cout<< p_tree->key << '\n';
    }
    void Delete (node* p_tree)
    {
        if (!p_tree)
            return;
        Delete(p_tree->p_left);
        Delete(p_tree->p_right);
        delete p_tree;
    }
    int main ()
    {
        node* p_tree = NULL;
        int key;
        int input;
        cout << "1. Add value" <<endl<<"2. See values in sorted order"<<endl<<"3. Quit"<<endl;
        cin>> input;
        while (input != 3)
        {
          switch (input)
          {
            case 1: cout<< "Enter the value you want to add" << endl;
                    cin >> key;
                    p_tree = insert (p_tree, key);break;
            case 2: print (p_tree);
          }
          cout << "1. Add value" <<endl<<"2. See values in sorted order"<<endl<<"3. Quit"<<endl;
          cin>>input;
        }
        Delete(p_tree);
    }
    Apperently there's something wrong cause every time I run the program and I try to print the values set it only prints le last node I entered instead of the full and sorted list. Any help with that?
    p.s. I don' t know what vectors are yet as well as other functions of the Standard Template Library so I must use an approach similar to this one... I guess



  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You're ignoring the return value of insert in the recursive calls.
    You aren't returning anything at the end of insert.
    (You've already been told this elsewhere.)
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    11
    You're ignoring the return value of insert in the recursive calls.
    You aren't returning anything at the end of insert.
    Yeah but even doin like this the error remains the same
    Code:
    node* insert (node* p_tree, int key)
    {
        if (p_tree == NULL)
        {
            node* p_new_node = new node;
            p_new_node->key = key;
            p_new_node->p_left = NULL;
            p_new_node->p_right = NULL;
            return p_new_node;
        }
        if (key < p_tree->key)
            p_tree = insert (p_tree->p_left, key);
        else
            p_tree = insert (p_tree->p_right, key);
        return p_tree;
    
    }
    Sorry if I keep doin something that may seem very silly but I m still learning be patient xD. I feel like I m getting more used to try solving these problems and the confusion I ve had in my mind is slowly goin away

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    As you were shown elsewhere, that's not how it's done. It needs to be
    Code:
    node* insert (node* p_tree, int key)
    {
        if (p_tree == nullptr)
        {
            node* p_new_node = new node;
            p_new_node->key = key;
            p_new_node->p_left = nullptr;
            p_new_node->p_right = nullptr;
            return p_new_node;
        }
        if (key < p_tree->key)
            p_tree->p_left = insert (p_tree->p_left, key);
        else
            p_tree->p_right = insert (p_tree->p_right, key);
        return p_tree; 
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number between 1 and 100 (console app) - Jumping into C++
    By Jasper Dunn in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2016, 03:03 AM
  2. Help with binary tree with bubble sorting
    By RudoDouglas in forum C Programming
    Replies: 4
    Last Post: 12-08-2014, 12:32 PM
  3. Question about computing the number of nodes in a Binary Tree
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 12-12-2009, 01:10 PM
  4. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  5. Binary sort number arranging tree
    By newtocpp in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2006, 05:19 AM

Tags for this Thread