I have the following code ( I'm creating a binary tree and then I want to print the values on it's leafs) but when I'm running it, it never enters in that if from the parcurgere function. What have I done wrong?

#include <iostream>
using namespace std;
class node
{
public: int val;
		static int niv;
		node *st;
		node *dr;
		node(int i)
		{
			val = i;
			st = NULL;
			dr = NULL;
		}
};

void creare(node *p)
{
	int x;

cin>>x;
if(x == 0) 
{
	p = NULL;
}
else 
{
	node *p = new node(x);
	cout<<"dati fiul stang al lui "<<x<<" ";
	creare(p->st);
	cout<<"dati fiul drept al lui "<<x<<" ";
	creare(p->dr);
}
}
void parcurgere(node *p)
{
	if(p != NULL)
	{
		cout<<p->val;
		parcurgere(p->st);
		parcurgere(p->dr);
	}
}
void main()
{
	node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);
	
}

Recommended Answers

All 5 Replies

I have the following code ( I'm creating a binary tree and then I want to print the values on it's leafs) but when I'm running it, it never enters in that if from the parcurgere function. What have I done wrong?

void creare(node *p)
{
	//your code
}
void parcurgere(node *p)
{
	if(p != NULL)
	{
		cout<<p->val;
		parcurgere(p->st);
		parcurgere(p->dr);
	}
}
void main()
{
	node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);
	
}

The function 'creare' does not change 't' , after the call 't' is still pointing to NULL and hence the 'if' condition in 'parcurgere' does not evaluate to TRUE. If you want 't' to point to a new object which is created in 'creare' then you should it pass the pointer by reference. So you function could be

void creare(node *& p)

And don't use void main

commented: Fast, cheap, good. Sounds like a winner to me. :) +13
commented: good answer +25

Well, t is NULL -- what do you expect?

node *t = NULL;
	cout<<"dati radacina arborelui ";
	creare(t);
	parcurgere(t);

Thank you for your answer! I passed the parameter by reference but I'm still having the same problem... t is still pointing to NULL...

#include <iostream>
using namespace std;
class node
{
public: int val;
        static int niv;
        node *st;
        node *dr;
        node(int i)
        {
            val = i;
            st = NULL;
            dr = NULL;
        }
};

void creare(node *&p)
{
    int x; 

cin>>x;
if(x == 0) 
{
    p = NULL;
}
else 
{
    node *p = new node(x);
    cout<<"dati fiul stang al lui "<<x<<" ";
    creare(p->st);
    cout<<"dati fiul drept al lui "<<x<<" ";
    creare(p->dr);
}

}
void parcurgere(node *p)
{
    cout<<"a";
    if(p != NULL)
    {
        cout<<p->val;
        parcurgere(p->st);
        parcurgere(p->dr);
    }
}
int main()
{
    node *t =NULL ;
    cout<<"dati radacina arborelui ";
    creare(t);
    parcurgere(t);
    return 0;

}

I think you have to re-implement this code from scratch, this just doesn't seem like an implementation of a binary tree. I would suggest that you read some tutorial on Binary trees before you start. Here are some good links Binary Trees , Binary search trees . Or just find some book and go through them and then try again.

I think you have to re-implement this code from scratch, this just doesn't seem like an implementation of a binary tree. I would suggest that you read some tutorial on Binary trees before you start. Here are some good links Binary Trees , Binary search trees . Or just find some book and go through them and then try again.

You were right ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.