home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 4 / CDPD_IV.bin / fish / 911-930 / ff926 / treetool / treetoolexample.c < prev   
C/C++ Source or Header  |  1994-05-04  |  2KB  |  73 lines

  1. #include "TreeTool.h"
  2. #include <stdio.h>
  3.  
  4. /* Some prototypes. */
  5. void displayFunc(NODE_HANDLE);
  6. void killFunc(struct someData *);
  7.  
  8. struct someData
  9. {
  10.   int   a,b,c;
  11.   char  *string;
  12. }
  13.  
  14. main()
  15. {
  16.   NODE_HANDLE         a_tree=NULL;
  17.   struct someData     data1={1,2,3,"Root node."},data2,data3={7,8,9,"Sub-child node."};
  18.   struct someData     *ptrDat2;
  19.  
  20.   /* We create our tree root. */
  21.   a_tree=tt_NewNode(NULL,NULL);
  22.  
  23.   /* Let's create a child.    */
  24.   tt_NewNode(a_tree,&data2);
  25.  
  26.   /* Create a child of the child. */
  27.   tt_NewNode(tt_GetFirstSon(a_tree),&data3);
  28.  
  29.   /* Set the root node pointer. */
  30.   tt_SetNodeData(a_tree,&data1);
  31.  
  32.   /* Get the user data pointer to the child node. */
  33.   ptrDat2=tt_GetNodeData(tt_GetFirstSon(a_tree));
  34.  
  35.   /* We can now fill it with "meaningfull" information. */
  36.   if( ptrDat2 )
  37.   {
  38.     ptrDat2->a=4;
  39.     ptrDat2->b=5;
  40.     ptrDat2->c=6;
  41.     ptrDat2->string="Child node.";
  42.   }
  43.  
  44.   /* We will use our displayFunc to recursively display the message */
  45.   /* in each node.                                                  */
  46.   tt_ApplyFunction(a_tree,displayFunc);
  47.  
  48.   printf("\n");
  49.  
  50.   /* We kill all the allocated nodes before quitting.                 */
  51.   /* We could just have used tt_KillNode(a_tree,NULL) because data is */
  52.   /* static.                                                          */
  53.   tt_KillNode(a_tree,killFunc);
  54. }
  55.  
  56. void displayFunc(NODE_HANDLE node)
  57. {
  58.   struct someData *ptrData;
  59.  
  60.   if( ptrData=tt_GetNodeData(node) )
  61.   {
  62.     printf("This node contains the message: %s\n",ptrData->string);
  63.   }
  64. }
  65.  
  66. void killFunc(struct someData *ptrData)
  67. {
  68.  
  69.    /* Actually does nothing but could do something if dynamic data was  */
  70.    /* used.                                                             */
  71.    printf("Killing the node containing message: %s\n",ptrData->string);
  72. }
  73.