(Last Revision: 11/08/2005)
Discussion of Commands
LOADT
Basically you can implement with these steps:
- mark all the nodes in the array of nodes "free"
- copy all the node information from the file according to where the
indices say they should go - mark each "target" "not free".
- link all the nodes that are still "free" into a new free list.
There's more to do but that is basically what is required.
INSRT
To implement this command it is necessary to check to see if a node with the
same name already exists. If not then the program must allocate a node off
the free list, put data in it, and insert it into the binary search tree. To
allocate a node, it has to be deleted from the free list and marked "not
free". Something must also be done to initialize the pointers in the node.
Probably the mother, father, and child pointers of a node should be
initialized to a value that stands for "NIL" - but probably it is logical to
initialize sibling pointers so they point back to the node itself.
The algorithm for finding and inserting in the binary search tree should be
the "standard" ones discussed in chapter 10. One may implement the operations
recursively or non-recursively. It should be possible to write the code by
"almost copying" the code from the textbook. Some things will have to be a
little different because the array of nodes is being used as the underlying
storage for the data.
Remember that each time you allocate a node off the heap, it may be a node
that was used for something else previously. Therefore it is very important
to make sure that everything is initialized properly - the names and key, and
also all the seven pointers that indicate tree and family relationships.
MOTHR and FATHR
For this job it might be good to have an operation called "Parentize" that has
four string parameters and one parameter that is the data type you use for the
indices of your pointer vectors. The idea would be to pass to Parentize the
names of a parent and a child, plus a parameter that is either "mother" or
"father" - so that Parentize knows which type of parent-child relationship it
is creating.
Basically, the job of Parentize would be to
- find both the parent node and the child node within the tree,
- figure out whether maternal or paternal siblings of the child need to be
managed.
- set the appropriate parent pointer of the child and its appropriate
siblings. (For example if the parent is a mother then we have to set the
mother pointer of the child to point to the mother, and we also have to
set the mother pointers of all the known maternal siblings of the child
to point to the same mother.)
- Check the child pointer of the parent node to see if it is "NIL".
- If the child pointer is "NIL" point it at the (new) child.
- If the child pointer is not "NIL" then merge the ring of known siblings
with the ring of unknown siblings.
For finding nodes in the binary search tree, I wrote a function called
FindNode for the tree class. It has three parameters and it returns true or
false, depending on whether the node sought is found. One parameter is a
search key. The other two are integers - reference parameters used to return
the location (indices) of the found node and the (BST) parent of the found
node. If the node is not found or there is no parent (because the found node
is the root) then the corresponding parameter comes back with the value "NIL".
STREE and FTREE
To implement these commands the program needs to find the node corresponding
to the first and last names, and then traverse a tree which is rooted at that
node. Only one set of functions is needed for both commands. A parameter or
two can "tell" a function whether to use family tree pointers or search tree
pointers. The code can print the contents of the nodes encountered during a
reverse-order traversal. The tree is traversed only to a certain depth - the
depth is passed as a parameter to the functions that need to know about it.
The Wprint procedure described in the
wprintAlg file
shows the basics of a technique for printing simple tree diagrams. In your
implementation you have to be careful to add something to prevent the
recursion from going too deep into the tree. Also you have to insure that
"right-child" and "left-child" are interpreted correctly.