By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

Initializing a Tree at compile Time

P: n/a
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
-

Oct 15 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Oct 14, 11:43 pm, hankyp...@gmail.com wrote:

<snip>
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};

Structure Node node[] = {leaf1, leaf2, leaf3};
<snip>
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?

Oct 15 '07 #2

P: n/a
No it does not work with const and static key word also.

On Oct 15, 9:58 am, Chris Johnson <effig...@gmail.comwrote:
On Oct 14, 11:43 pm, hankyp...@gmail.com wrote:

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

<snip>
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?

Oct 15 '07 #3

P: n/a
On Sun, 14 Oct 2007 21:43:44 -0700, hankypan1 wrote:
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.

Oct 15 '07 #4

P: n/a
ha*******@gmail.com writes:
I need a tree data structure for my application.
<snip>
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.

--
Ben.
Oct 15 '07 #5

P: n/a
On Oct 15, 2:06 am, hankyp...@gmail.com wrote:
On Oct 15, 9:58 am, Chris Johnson <effig...@gmail.comwrote:
On Oct 14, 11:43 pm, hankyp...@gmail.com wrote:
<snip>
Structure Node leaf1 = {1,1,1};
Structure Node leaf2 = {1,1,1};
Structure Node leaf3 = {1,1,1};
Structure Node node[] = {leaf1, leaf2, leaf3};
<snip>
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};

Oct 15 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.