I've been working on this for many hours now. Maybe a good night of rest will help clear my head and help me out.

But I'm going to post this here tonight in hope to see something tomorrow that can help me out.

The link to my homework is below...

http://omega.uta.edu/~darin/CSE1320/1320-hw10.pdf

I need to construct a binary tree and print the values out in pre-order, inorder and post-order.

Everything is working fine except that I am getting a random 0 in each of the outputs.

Can anyone see any problems I'm having?

Here's the main code...

Here's the header code.....Code:/* Geoff Graham 4/23/12 Cse 1320 Homework 10 */ #include <stdio.h> #include <stdlib.h> #include "binary-lib.h" int main(void) { struct node *root = NULL; int *array_of_numbers; int i; int Max_lines = 0; array_of_numbers = numbers(&Max_lines); printf("\nunsorted: "); for(i = 0; i < Max_lines; i++) printf(" %3d, ", array_of_numbers[i]); array_of_numbers = sort_array(array_of_numbers, Max_lines); root = add_and_balance(array_of_numbers, 0, Max_lines); free(array_of_numbers); printf("\n\npreorder: "); preorder( root ); printf("\ninorder: "); inorder( root ); printf("\npostorder: "); postorder( root ); printf("\n"); freeMemory( root ); getchar(); return 0; }

Code:struct node { int value; struct node *left; struct node *right; }; void preorder(struct node* leaf); void inorder(struct node* leaf); void postorder(struct node* leaf); void freeMemory(struct node* leaf); int *numbers(int *Max_lines); int *sort_array(int *array_of_numbers, int Max_lines); struct node* add_and_balance(int *array, int first_index, int last_index);

And here is the binary-lib.c code which has the functions. (somewhere in here is where I think the problem is. I'm thinking somewhere in my add_and_balance array)

Code:#include "binary-lib.h"#include <stdio.h> #include <stdlib.h> #include <string.h> int *numbers(int *Max_lines) { int *array_of_numbers, *temp; char buffer[50]; FILE *fp; int counter; int n; n = 5; counter = 0; array_of_numbers = (int*)malloc(n * sizeof(int)); printf("Initial array size is %d words\n", n); if ( (fp = fopen("numbers.txt", "r" )) == NULL ) { printf("Couldn't open file\n"); exit(1); } while( fgets(buffer, sizeof(buffer), fp)) { array_of_numbers[counter] = atoi(strtok(buffer, "\n")); if ( (counter+1) == n ) { n = n*2; temp = (int*)realloc(array_of_numbers, n * sizeof(int)); if(temp != NULL) array_of_numbers = temp; else { printf("unable to reallocate\n"); exit(1); } printf("Reached limit...increasing array to %d words\n", n); } counter++; } fclose(fp); *Max_lines = counter; return array_of_numbers; } int *sort_array(int *array_of_numbers, int Max_lines) { int temp; int i; int swaps = 1; while(swaps) { swaps = 0; for(i = 0; i < Max_lines; i++) { if( array_of_numbers[i] > array_of_numbers[i+1] ) { temp = array_of_numbers[i]; array_of_numbers[i] = array_of_numbers[i+1]; array_of_numbers[i+1] = temp; swaps = 1; } } } return array_of_numbers; } struct node* add_and_balance(int *array, int first_index, int last_index) { int middle_index; struct node *new_node; if (first_index <= last_index) { middle_index = first_index + (last_index - first_index)/2; new_node = (struct node*) malloc( sizeof(struct node) ); new_node->value = array[middle_index]; new_node->left = add_and_balance(array, first_index, middle_index - 1); new_node->right = add_and_balance(array, middle_index + 1, last_index); return new_node; } else return NULL; } void preorder(struct node* leaf) { if(leaf != NULL) { printf(" %3d, ", leaf->value); preorder(leaf->left); preorder(leaf->right); } } void inorder(struct node* leaf) { if(leaf != NULL) { inorder(leaf->left); printf(" %3d, ", leaf->value); inorder(leaf->right); } } void postorder(struct node* leaf) { if(leaf != NULL) { postorder(leaf->left); postorder(leaf->right); printf("%3d, ", leaf->value); } } void freeMemory(struct node* leaf) { if(leaf != NULL) { freeMemory(leaf->left); freeMemory(leaf->right); free( leaf ); } }

Thanks for any help. I'm thinking it's either a logic problem or some silly mistake I've made. Hopefully I can finish it up tomorrow.