I am trying to create multiple trees after calling tree_holder. The number of tree is unknown until compile time.
Since there are many trees, and i want to access them right away just like an array. I do not use linked list since it is too slow.

I have written the code to insert data in a tree. it can insert data into a tree without problems. The following is the code for create a tree.

//**********************************************
typedef struct trn { 
                                    struct trn *right;
                                   struct trn  *left;
                                    int key;
                    } treenode;

treenode *create_tree(void)
{  treenode *node;
   node = get_node();
   node->left = NULL;
   return(node );
}
//************************************************

In the main function, i write the following to create a tree.

treenode *onetree;
onetree = create_tree();

The above code runs perfectly(there are also get_node function, insert data,delete data function.They are too long,so i omit in the post) ,but i want to create multiple trees after calling a tree_holder variable,so i write a new struct and create_many_trees function :

//**************************************

typedef struct  m_t{
                   treenode *tree; 
                    }tree_holder;
//***********************************
tree_holder *create_many_trees(int length){
     treenode *a;
     tree_holder *b;
            a=(treenode *) malloc(length*sizeof(treenode)); 
.              .....some statements to catch a_t==Null

             for(int i=0;i<length; i++){
                           a[i]=create_tree();}

            b->tree=a;

return (b);
}
//************************************************

Now in the main function i want to create multiple trees(let say 5) instead on a single tree. I replace the old code in main with new code

//********************
Old code:
treenode *onetree;
onetree = create_tree();
//******************
New code:
tree_holder *th;
th=create_many_trees(5);

Now when it compiles, it gives a "incompatible types in assignment error" at this line "a[i]=create_tree();" inside the tree_holder *create_many_trees(int length) function.

Question: Is there other ways to create multiple tree and store it using a struct variable? Why it said "incompatible types"?
create_tree() returns treenode, and "variable a" inside the create_many_trees is also treenode.

Recommended Answers

All 2 Replies

Use code tags for your code
[code=c] . . . Your code here . . .

[/code]

I am trying to create multiple trees after calling tree_holder. The number of tree is unknown until compile time.
Since there are many trees, and i want to access them right away just like an array. I do not use linked list since it is too slow.

I have written the code to insert data in a tree. it can insert data into a tree without problems. The following is the code for create a tree.

typedef struct m_t{
treenode *tree;
}tree_holder;
treenode *create_tree(void)
{ treenode *node;
node = get_node();
node->left = NULL;
return(node );
}

In the main function, i write the following to create a tree.

treenode *onetree;
onetree = create_tree();

The above code runs perfectly(there are also get_node function, insert data,delete data function.They are too long,so i omit in the post) ,but i want to create multiple trees after calling a tree_holder variable,so i write a new struct and create_many_trees function :

typedef struct m_t{
treenode *tree;
}tree_holder;
tree_holder *create_many_trees(int length){
treenode *a;
tree_holder *b;
a=(treenode *) malloc(length*sizeof(treenode));
. .....some statements to catch a_t==Null

for(int i=0;i<length; i++){
a[i]=create_tree();}

b->tree=a;

return (b);
}

Now in the main function i want to create multiple trees(let say 5) instead on a single tree. I replace the old code in main with new code
old code

treenode *onetree;
onetree = create_tree();

new code:

tree_holder *th;
th=create_many_trees(5);

Now when it compiles, it gives a "incompatible types in assignment error" at this line "a=create_tree();" inside the tree_holder *create_many_trees(int length) function.


Question: Is there other ways to create multiple tree and store it using a struct variable?

After i create it How do i call the fisrt ,second and third tree?Like this
th->tree[0]; th->tree[1]; th->tree[2];?????

Why it said "incompatible types"?
create_tree() returns treenode, and "variable a" inside the create_many_trees is also treenode.

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.