#include <iostream>
usingnamespace std;
struct node
{
int key_value;
node* p_left;
node* p_right;
};
node* insert (node *p_tree, int key)
{
if ( p_tree == NULL )
{
node* p_new_tree = new node;
p_new_tree->p_left = NULL;
p_new_tree->p_right = NULL;
p_new_tree->key_value = key;
return p_new_tree;
}
if( key < p_tree->key_value )
{
p_tree->p_left = insert( p_tree->p_left, key );
}
else
{
p_tree->p_right = insert( p_tree->p_right, key );
}
return p_tree;
}
int main()
{
///there was no sample int main. only insert function
node* p_tree=NULL;///declaring main head of binary tree
p_tree=insert(p_tree,5);/// for the first insert function, we have to
///p_tree=
insert(p_tree,35);///from second one onward, no need to do that. right???
}
edit: sorry i was a little careless din't realize that was a recursive function so here we go:
1 2 3 4 5 6 7 8 9 10 11 12 13
void insert(node*& p_tree,int value)
{
if(p_tree==nullptr)
{
p_tree=new node(value,nullptr,nullptr);
}
elseif(p_tree.key_value<value)
insert(value,p_tree->p_left);
else
insert(value,p_tree->p_rigth);
}
///on each insert you have to explicitly call the function.
p_tree.key_value never mind i just woke up sometimes ago so you can expect that, for the & sign i prefer passing plain pointers by reference to passing them by value coz sometimes they can get nasty ending you up into some serious trouble, so reference could save you some debugging time.
1 2
elseif(p_tree.key_value<value)
insert(value,p_tree->p_left);///arguments could well be placed other way
To comment on your original code, insert(p_tree,35);///from second one onward, no need to do that. right??? is wrong. You always need to do p_tree = insert(p_tree,35);. That comes in handy if you're doing AVL trees and need to rotate sub-trees to balance the whole tree.
@fg109 i don't quite get what you meant. either 1) commenting in original code is wrong(why???) or 2) while calling insert, i need to always write p_tree= or 3) both?
I meant 2. Since the original function takes a pointer by value and not by reference, the pointer in your main function is not changed unless you use p_tree=.
@fg109 @andy1992 the topic should have been closed by now. sorry for dragging it more but i don't really understand.
i am simply following jumping into c++ now and it, till now, don't have passing pointer by reference/value. so i had to Google it a bit. can't understand the necessity of passing by reference in this case. and @fg109 the necessity of writing p_tree=
example:
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
int x=5;
int* p_x=&x;
experiment(p_x);///argument p_x and p_y(p_y in prototype function below) both refer to same thing.
cout<<*p_x;///modifying p_y below changes p_x as well
}
void experiment(int* p_y)
{
*p_y+=5;///modifying value of p_y
cout<<*p_y<<endl;
}
contextual to question,
for clarity, let me change insert function's argument's name node* insert (node *p_dummy_tree, int key)///p_tree changed to p_dummy_tree
1) p_dummy_tree and p_tree(when passed to function) would point to same thing. simply modifying one would modify other. why pointer by reference than simple pointer in this case?
@fg109 2) since modifying p_dummy_tree would change p_tree, why use p_tree=. p_tree itself will change without using p_treethe code seems to work fine without p_tree when i checked.