Initializing a Tree at compile Time

H

hankypan1

Hi All,

I need a tree data structure for my application. It is the non -cyclic
simple tree where i can have any number of children node and each
child can recursively become a sub tree like a normal tree. Now the
thing is i can popullate my tree at compile time like a global data.
Since
i know my tree definition at compile time, instead of using pointers
to point to siblings or child nodes, i am planning to use the array
with undefined length like int a[] = {1,2}.

Issue is if i declare complete tree at one place like this, my code
become unreadable and unmanageable because of nested and high depth of
tree. Even if i format code with 2 white space, it will become very
big with around 10-20 levels and 20 nodes at each level. Also
declaring each node with some logical name instead of an array element
at some position will also make it easy to modify in future when
required.

I was planning to declare each node seperately and then use that in
tree as reference
like
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

and then
Structure Node node[] = {1,1,1, leaf1, leaf2, leaf3};

But the problem is i can not reference a variable like this as global
data above my main method like this

#include <stdio.h>

Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

Structure Node node[] = {leaf1, leaf2, leaf3};

int main()
{
/// do processing;
return 0;
}


It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.

Regards
-
 
C

Chris Johnson

On Oct 14, 11:43 pm, (e-mail address removed) wrote:

Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

Structure Node node[] = {leaf1, leaf2, leaf3};

It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.

Have you tried declaring your nodes as constant?
 
H

hankypan1

No it does not work with const and static key word also.

On Oct 14, 11:43 pm, (e-mail address removed) wrote:

Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};
Structure Node node[] = {leaf1, leaf2, leaf3};

It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.

Have you tried declaring your nodes as constant?
 
D

Duncan Muirhead

Hi All,

I need a tree data structure for my application. It is the non -cyclic
simple tree where i can have any number of children node and each
child can recursively become a sub tree like a normal tree. Now the
thing is i can popullate my tree at compile time like a global data.
Since
i know my tree definition at compile time, instead of using pointers
to point to siblings or child nodes, i am planning to use the array
with undefined length like int a[] = {1,2}.

Issue is if i declare complete tree at one place like this, my code
become unreadable and unmanageable because of nested and high depth of
tree. Even if i format code with 2 white space, it will become very
big with around 10-20 levels and 20 nodes at each level. Also
declaring each node with some logical name instead of an array element
at some position will also make it easy to modify in future when
required.

I was planning to declare each node seperately and then use that in
tree as reference
like
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

and then
Structure Node node[] = {1,1,1, leaf1, leaf2, leaf3};

But the problem is i can not reference a variable like this as global
data above my main method like this

#include <stdio.h>

Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

Structure Node node[] = {leaf1, leaf2, leaf3};

int main()
{
/// do processing;
return 0;
}


It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.

Regards
-
One way is to name the arrays, rather than the individual nodes:
typedef struct node_s
{ int nsub;
struct node_s* sub;
char* payload;
} node;

#define LEAF( s) { 0, NULL, s}
#define NODE( sub, s) { sizeof sub / sizeof sub[0], sub, s}

node a[] = { LEAF( "alice")
, LEAF( "bob")
};
node b[] = { NODE( a, "cath")
, LEAF( "dave")
};
node root = NODE( b, "ena");

However I'd be tempted to define a format that was
easy to read/validate, and then write a wee program to read in
files in the format and write out source code for the
initialised tree, for the real program.
 
H

hankypan1

Hi ,

I need a tree data structure for my application. It is the non -cyclic
simple tree where i can have any number of children node and each
child can recursively become a sub tree like a normal tree. Now the
thing is i can popullate my tree at compile time like a global data.
Since
i know my tree definition at compile time, instead of using pointers
to point to siblings or child nodes, i am planning to use the array
with undefined length like int a[] = {1,2}.
Issue is if i declare complete tree at one place like this, my code
become unreadable and unmanageable because of nested and high depth of
tree. Even if i format code with 2 white space, it will become very
big with around 10-20 levels and 20 nodes at each level. Also
declaring each node with some logical name instead of an array element
at some position will also make it easy to modify in future when
required.
I was planning to declare each node seperately and then use that in
tree as reference
like
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};
and then
Structure Node node[] = {1,1,1, leaf1, leaf2, leaf3};
But the problem is i can not reference a variable like this as global
data above my main method like this
#include <stdio.h>
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};
Structure Node node[] = {leaf1, leaf2, leaf3};
int main()
{
/// do processing;
return 0;
}
It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.
Regards
-

One way is to name the arrays, rather than the individual nodes:
typedef struct node_s
{ int nsub;
struct node_s* sub;
char* payload;

} node;

#define LEAF( s) { 0, NULL, s}
#define NODE( sub, s) { sizeof sub / sizeof sub[0], sub, s}

node a[] = { LEAF( "alice")
, LEAF( "bob")
};
node b[] = { NODE( a, "cath")
, LEAF( "dave")
};
node root = NODE( b, "ena");

However I'd be tempted to define a format that was
easy to read/validate, and then write a wee program to read in
files in the format and write out source code for the
initialised tree, for the real program.- Hide quoted text -

Can you pls explain what do you meant by source code for initialised
tree.
If you meant directly declaring in the array format enclosed within
"{", then i
just have one doubt that this source code may become unreadable when
the
tree depth is even in order of 10-20 or so.
 
D

Duncan Muirhead

Hi ,

I need a tree data structure for my application. It is the non -cyclic
simple tree where i can have any number of children node and each
child can recursively become a sub tree like a normal tree. Now the
thing is i can popullate my tree at compile time like a global data.
Since
i know my tree definition at compile time, instead of using pointers
to point to siblings or child nodes, i am planning to use the array
with undefined length like int a[] = {1,2}.
Issue is if i declare complete tree at one place like this, my code
become unreadable and unmanageable because of nested and high depth of
tree. Even if i format code with 2 white space, it will become very
big with around 10-20 levels and 20 nodes at each level. Also
declaring each node with some logical name instead of an array element
at some position will also make it easy to modify in future when
required.
However I'd be tempted to define a format that was
easy to read/validate, and then write a wee program to read in
files in the format and write out source code for the
initialised tree, for the real program.- Hide quoted text -

Can you pls explain what do you meant by source code for initialised
tree.
If you meant directly declaring in the array format enclosed within
"{", then i
just have one doubt that this source code may become unreadable when
the
tree depth is even in order of 10-20 or so.
I did indeed mean the array format. The point is that if this is produced
by a program there's no need to read it. What should be readable (and easy
to change, validate etc) is the input format to the program that
writes the array initialiser. Once you are confident that that program
is working, you don't need to read the (hideous, unreadable) output of
it any more than you need to read the output of the compiler.

BTW if each node has just 2 children then a 20 deep tree has something
like a million nodes. You might have to worry about bumping into compiler
limitations -- will it accept a million line source file?
 
B

Ben Bacarisse

I need a tree data structure for my application.
I was planning to declare each node seperately and then use that in
tree as reference
like
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

and then
Structure Node node[] = {1,1,1, leaf1, leaf2, leaf3};

But the problem is i can not reference a variable like this as global
data above my main method like this

The above is not C, so of course it does not work. It looks like you
need a structure with an pointer to an array of child nodes. You can
do this (as static data), but you need to name each of the child
arrays or C99 compound literals. Also, you will need to decide how
the number of child nodes is recorded. The two common options would
be an extra NULL pointer at the end of the child array, and a count
stored in the node structure.

This looks like coursework, so I'd prefer to see what you actually
have rather than post a solution.
 
C

Chris Johnson

On Oct 14, 11:43 pm, (e-mail address removed) wrote:
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};
Structure Node node[] = {leaf1, leaf2, leaf3};
It gives compilation error "error: initializer element is not
constant". Pls let me know how can i handle such situation.
Have you tried declaring your nodes as constant?

No it does not work with const and static key word also.

You should reply underneath the line to which you are replying.

And yeah, I was just guessing from the compiler error.

But with a little more sleep under my belt, the problem seems to be
that you're expecting procedural behavior from things outside of
procedures. You can fix this by moving the line that references the
variables inside your main method. Alternately, the following sort of
thing should suit your stated goals, and use less memory than your
proposed solution:

#define LEAF1 {1,1,1}
#define LEAF2 {1,1,1}
#define LEAF3 {1,1,1}

struct Node node[] = {LEAF1, LEAF2, LEAF3};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top