Computer Science 1511
Computer Science I
Programming Assignment 8
Pointer Processing & Recursion (20 extra credit points)
Due Friday, December 15, 2000
NO LATE ASSIGNMENTS ACCEPTED FOR PROGRAM 8
The Problem
You are going to use an array of pointers to represent the tree. The tree will be specified in three arrays by "hand-coded" assignments, as illustrated below. You can do your testing with the following tree:

The dark nodes are the branch points of tree. The numbers beside the dark nodes are the numbers of the branch points. The "B"s in white circles are berries on the tree (branch points 2 and 4 have berries on them). This is the food for the robot insect. The insect always starts at the root of the tree (node 0). Your program should work for other tree structures, but you can do your testing with this tree. You will use three arrays to represent your tree. The following partial program represents the above tree using three arrays.
#define MAX_NODES 10
#define NUM_COLUMNS 3
// column names
#define PARENT 0
#define NUMBER 1
#define FOOD 2
int main()
{
int *left[MAX_NODES]; // connections to left "child"
int nodes[MAX_NODES][NUM_COLUMNS]; // branch points of tree
int *right[MAX_NODES]; // connections to right "child"
int N;
int i;
int currNode; // robot insect's current position in tree
N = 5; // 5 nodes in current tree
// Set the number of each node to its own number
for (i=0; i < N; i++) {
nodes[i][NUMBER] = i;
}
// Connections for node 0 (root of tree)
left[0] = &nodes[1][NUMBER]; // left of root connects to node 1
right[0] = &nodes[2][NUMBER]; // right of root connects to node 2
nodes[0][PARENT] = -1; // parent of root does not exist (-1 value)
nodes[0][FOOD] = 0; // no food here
// Connections for node 1
left[1] = NULL; // no left child
right[1] = NULL; // no right child
nodes[1][PARENT] = 0; // parent of node 1 is root (node 0)
nodes[1][FOOD] = 0; // no food here
// Connections for node 2
left[2] = &nodes[3][NUMBER]; // left child is node 3
right[2] = &nodes[4][NUMBER]; // right child is node 4
nodes[2][PARENT] = 0; // root is parent
nodes[2][FOOD] = 1; // food here!
// Connections for node 3
left[3] = NULL; // no left child
right[3] = NULL; // no right child
nodes[3][PARENT] = 2; // parent is node 2
nodes[3][FOOD] = 0; // no food here
// Connections for node 4
left[4] = NULL; // no left child
right[4] = NULL; // no right child
nodes[4][PARENT] = 2; // parent is node 2
nodes[4][FOOD] = 1; // food here!
currNode = 0; // start robot insect at root of tree
RobotInsect(left, nodes, right, &currNode);
printf("Press return to continue\n");
getchar();
return (0);
}
An array of pointers to integers is used to represent the "left" child of each branch point or node. A second array of pointers
is used to represent the "right" child of each node. For example, left[0] gives a pointer
to the number of the left child of node 0, the root node of the tree and right[0] gives a pointer to the number of the right child of node 0. The "nodes" array
gives the number of the node (same as its index), the node number of the parent of this node (parents are down, closer to the root of the tree),
and a flag (0 or 1) indicating the absence or presence of food at the node.
Just before the RobotInsect function executes, you will have the following configuration of the arrays:

RobotInsect: Position: 0 RobotInsect: Position: 1 RobotInsect: Position: 0 RobotInsect: Position: 2 -- RobotInsect: Eating food! RobotInsect: Position: 3 RobotInsect: Position: 2 RobotInsect: Position: 4 -- RobotInsect: Eating food! RobotInsect: Position: 2 RobotInsect: Position: 0
Some details
(1) Do not use global variables.
(2) The six (6) functions as discussed in class: IsFoodHere(), EatFood(), IsBranch(), TakeLeftBranch(), TakeRightBranch(), and ReturnFromBranch() will need various parameters. For example, some of them will need the nodes array, and some of them will need the left and right array. Some of them may need all of the arrays, some of them may only need a few. Only pass the parameters that are used (needed) by the particular function.
Hand in your program, a lab report, and output of your program.
EXTRA CREDIT(5 points) Modify your program so that it can initialize the arrays in one other way. Make two additional functions, one for each way in which the tree is initialized. The first function will correspond to the five node tree given in the example program above. The second function will initialize the arrays with a new tree structure. This tree structure needs to have at least 10 nodes in it (change #define constants if need be) and should have 1/2 of the nodes with food on them. Ask the user, with a menu choice, which tree he or she want the robot insect to climb.
(8 points) Modify your program so that each node can have 0, 1, or 2 branches. Create, as in the above extra credit, an additional test tree using functions, where the test tree has some nodes with 0, some nodes with 1, and some nodes with 2 branches. Also as above, ask the user, with a menu choice, which tree he or she wants the robot insect to climb.