Hi all,

Say i have 2 nodes of a tree.

struct tree{
    int data;
    tree* left;
    tree* right;
    };

NODE1
data = 3;
left = NULL;
right = NULL

and

NODE2
data = 6;
left = NULL;
right = NULL

How do i say Node 2 if the left link of node 1 so it creates the start of a tree.

So it looks like

    NODE1
    /   \
 NODE2  NULL

I thought I could say node1->left = &node2; // left = the address of node 2.

Is that right?

Thanks

its customary to name the top of the tree (node 1) "head"

struct tree* head = NULL;

After allocating a new tree node check of head == NULL, if it does then just set head = newnode.

I thought I could say node1->left = &node2; // left = the address of node 2.

Is that right?

What happened when you tried it? Didn't it work?

And what The Dragon said is true. Make a pointer named head that will point to the first node. That way if the first node has to change it's easy to add a node and relink to the new first node

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.