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

1. 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. 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.)

3. 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. 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;
}```