Hello:

I was hoping to get some advice about how to return a tree (basically a
linked list -- with each node containing a parent, left, and right  node
pointers) from a C routine back into R. Each node itself contains several
attributes (a double, a char *, an int, and a void * )

Initially I was thinking I could just return to R a SEXP containing a
pointer to the Root Node, but then realized that the pointer would be
useless since C probably frees the memory after I leave the routine.

Since trees vary in length (or height) I need to be able to loop thru the
tree until all Nodes have been visited and saved in a SEXP. I'm really new
to handling R objects from within C, and converting and returning a large
tree structure is daunting. Here is some rough code I was thinking about
using to do this.  Any suggestions or help will be greatly appreciated!

SEXP list ;

PROTECT( list = allocVector(VECSXP, tree->size) ) ;

for( i = 0; i < tree->size; ++i ){
     SEXP node, tree_double, tree_char, tree_int, tree_voidPTR ;

     PROTECT( tree_double = NEW_NUMERIC( tree->weight ) ) ;
     PROTECT( tree_char = NEW_CHARACTER( tree->name ) ) ;
     PROTECT( tree_int = NEW_INTEGER( tree->exons ) ) ;
     PROTECT( tree_voidPTR = NEW_CHARACTER( tree->data ) ) ;   ??? not sure
about this...

     PROTECT( node = allocVector( VECSXP, 4 ) ) ;
     SET_VECTOR_ELT( node, 0, tree_double) ;
     SET_VECTOR_ELT( node, 1, tree_char) ;
     SET_VECTOR_ELT( node, 2, tree_int) ;
     SET_VECTOR_ELT( node, 3, tree_voidPTR ) ;

     SET_VECTOR_ELT( list, i, node ) ;
}

UNPROTECT( tree->size * 4 ) ;  ??? Tricky..
return( list ) ;

Look reasonable ?

THANKS !

	[[alternative HTML version deleted]]

