Search code examples
cdata-structurescode-organization

C data structures - storing a representation of a binary tree


I'm working on a project for an embedded processor where I have some data which is created at runtime, and pointers to this data need to be arranged into several binary tree-like data structures for use by the program.

These structures will be fixed for the duration of the program, however, I would like the option at compile time to be able to quickly change how the trees used are structured. Something hopefully more elegant and easily re configurable than say, a function or macro for every tree involved that repeatedly calls the "AddNode" function in the proper order for each underlying data element and node of the tree. Thanks for any suggestions!


Solution

  • How about using the M4 preprocessor to implement a high-level pseudo-language to generate the C code required to build trees? Your code may have a function called, for instance, initTrees() whose body would be autogenerated by M4 from a file whose content would be a description of the desired tree structure. The overall process would be as follows:

    sample.c.m4 ---> [M4] ---> sample.c ---> [C compiler] ---> Binary file

    http://www.gnu.org/software/m4/

    http://en.wikipedia.org/wiki/M4_(computer_language)