Next: Deleting Nodes Up: Basic FARFALLA Programming Previous: Inputting a Tree

Navigating a FARFALLA Tree

Suppose a FARFALLA tree exists in memory and you wish to access some of the data in the tree. To begin with, you must have a pointer to the top node of the tree.

Given a pointer to any node in the tree, you may obtain a pointer to any of its children by calling the function F_getChild(). F_getChild() F_getChild() takes three arguments. The first is a pointer to the parent. The second is a pointer to the child; it will be changed by F_getChild() to point at the desired child. The type of the second argument is important because it is used to determine what type of child you are trying to access. The third argument is an index; if the parent has more than one child of the desired type, the index specifies which child is desired. If there is only one child, the index should be 0. If there are two children, the index may be 0 or 1, etc.

In the following code, assume we have a tree in memory with the structure shown in Figure . We have been given evPtr, a pointer to the eventNode at the top of the tree.


erpNode *erpPtr;
F_getChild(evPtr,erpPtr,0);       // erpPtr is made to point at 0th erp child
if (erpPtr->boxnum == 101) ...

F_getChild(evPtr,erpPtr,1);       // erpPtr is made to point at 1st erp child
if (erpPtr->boxnum == 118) ...

wireTrackNode *wtrkPtr;
F_getChild(evPtr,wtrkPtr,0);      // wtrkPtr is made to point at Wtrk child
if (wtrkPtr->slope == 1.20) ...

Often you will want to loop over all the children of a given type. The member function numChildren() numChildren() is provided to make this simple. It takes a single argument of type F_NodeType, and returns the number of children the node has of that type. The following code uses a for loop to examine all the erpNode children of the top-level node. Note that the argument to numChildren() is the F_NodeType variable erpType which was declared when the erpNode class was defined (this declaration will be seen in Section ).


for (int i = 0; i < evPtr->numChildren(erpType); i++)
{
  erpNode *erpPtr;              // declare the pointer to erpNodes
  F_getChild(evPtr,erpPtr,i);   // make erpPtr point at the ith child
  if (erpPtr->adc0u == 3968) ...
}

Usually in analysis programs you know ahead of time what nodes you are interested in and where to find them. Occasionally you may need to explore a tree without knowing what you expect to find on it; for that purpose two additional navigation routines are provided: numTypesChildren() and childType(). Most programmers will never need these routines, so we will defer describing them until the reference section (Section ).



Next: Deleting Nodes Up: Basic FARFALLA Programming Previous: Inputting a Tree


walter@
Wed Aug 10 11:53:26 PDT 1994