0

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

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by WaltP
0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.