I still don't know if this is right.

Ted: can you tell me if I am doing it right/wrong?

...here was the printout to file:

Starting from the top, again...

Attaching to left side!

Starting from the top, again...

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Starting from the top, again...

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Taking a left!

Attaching to left side!

Here's the printing code...

Code:

#include <stdio.h>
FILE *fp;
typedef struct TREE{
TREE *right;
TREE *left;
TREE *back;
int info;
};
int add_node(TREE *t, int info) {
TREE * ptr = t;
ptr->back = NULL; ///...redundant but innocuous...will always be top node...
fprintf(fp,"\n\nStarting from the top, again...\n");
while (ptr) //...could just as well be "while(1)"
{
if(ptr->info == info)
{ fprintf(fp,"Duplicate !\n"); return -1; }
if (info < ptr->info)
{
if(ptr->right != NULL)
{ fprintf(fp,"Taking a right!\n"); ptr = ptr->right; }
else
break;
}
else
{
if(ptr->left != NULL)
{ fprintf(fp,"Taking a left!\n"); ptr = ptr->left; }
else
break;
}
}
TREE * p = new TREE;
p->info = info;
p->left = NULL;
p->right = NULL;
p->back = ptr;
if (t == NULL) t = p;
if ( p->info < ptr->info)
{ fprintf(fp,"Attaching to right side!\n"); ptr->right = p; }
else
{ fprintf(fp,"Attaching to left side!\n"); ptr->left = p; }
return 0;
}
void build_tree(int *size, int data[], TREE *t)
{
fp = fopen("DEBUG.txt", "w");
if(!fp) return;
int ct;
int duplicate;
for (ct = 0; ct < *size; ++ct)
{
duplicate = add_node( t, data[ct]);
if(duplicate) *size--;
}
fclose(fp);
}
int main(int argc, char *argv[])
{
TREE tree;
tree.left = 0;
tree.right = 0;
tree.back = 0;
tree.info = 0;
int i;
int size = 10;
int array[ size ];
for(i = 1; i <= size; i++)
array[i-1] = i;
build_tree( &size, array, &tree);
return 0;
}