473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Initializing a Tree at compile Time

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
5 3680
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
5
by: Mike | last post by:
Why does this code insert a node into a binary search tree correctly? If I only inserting going by first digit it works properly but when I try inserting going by the whole ip and the port number...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
9
by: Christian Christmann | last post by:
Hi, I have problems to initialize a static struct. Here is the meaningful part of the code: int main() { int pA = -100; struct globalMixed4 {
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
5
by: Jon Smirl | last post by:
Is there some way to tell a dictionary object that I am going to load 1M objects into it and have it pre-allocate enought slots to hold all of the entries? Thus avoiding many thousand memory...
34
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.