I need to print out a binary tree from the bottom left most node to the bottom right most node, with the tree being sorted in alphabetical order.
struct Book{
/* Book details */
char title[MAX_TITLE_LENGTH+1]; /* name string */
char author[MAX_AUTHOR_LENGTH+1]; /* job string */
int year; /* year of publication */
/* pointers to left and right branches pointing down to next level in
the binary tree (for if you use a binary tree instead of an array) */
struct Book *left, *right;};
I wrote a compare function to add books to the tree in alphabetical order, but cannot figure out how to modify it to print them in alphabetical order instead.
void compare(struct Book *a, struct Book* new){
struct Book *temp; temp =(struct Book *)malloc(sizeof(struct Book));
if(strcmp(a->title, new->title)<0){
if(a->right == NULL)
a->right = new;
else{
temp = a->right;
compare(temp,new);
}
}
else if(strcmp(a->title, new->title)>0){
if(a->left == NULL)
a->left = new;
else{
temp = a->left;
compare(temp,new);
}
}
else if(strcmp(a->title, new->title) == 0){
fprintf(stderr, "\nThis title already exists\n");
}}
comparefunction? Instead you need to provide atraverselike function. - Gerhardh