Binary Trees in C

Hey guys

I am trying to build a binary Tree of integers from a file in C for college, and my program returns error exit code 255 on windows

my code is as follows

// this is a program for building a binary tree and 
// visiting its nodes using preOrder travsersal

 #include < stdio.h>
#include < string.h>


// define a structure for the data
typedef struct{
	int num;
}NodeData;

// define the treenode
typedef struct treeNode{
	NodeData data;
	struct treeNode *left,*right;
}TreeNode,*TreeNodePtr;

// define the BinaryTree 
typedef struct{
	TreeNodePtr root;
}BinaryTree;

main(){
	TreeNodePtr buildTree(FILE *);
	void preOrder(TreeNodePtr);


    // open the file
FILE *in=fopen("integers.txt","r");
if(in!=NULL){

// declare the binarytree
BinaryTree bt;

// build the tree
bt.root=buildTree(in);

printf("\n the preOrder traversal is:");
preOrder(bt.root);
} else printf("Cannot Open the file");
} // end of the main
TreeNodePtr buildTree(FILE *in){
	int num;
	fscanf (in, "%d", num);
	printf ("number %d \n", num);
	TreeNodePtr p = (TreeNodePtr) malloc(sizeof(TreeNode));
	p -> data.num;

    printf("root %d \n", p);

p->left=buildTree(in);

printf("left %d \n", p->left->data.num);
p->right=buildTree(in);
printf("right %d \n",p->right->data.num);

return p;
} //end of build tree

void visit(TreeNodePtr node){
	printf("%d", node->data.num);
}

void preOrder(TreeNodePtr node){
	if(node !=NULL){
		visit(node);
		preOrder(node->left);
		preOrder(node->right);
	}
}

On a side note how do i format code so that it looks somewhat decent on the forums?

Check your post, i edited it, it will show you how to do code blocks.

What are you using for editing your code? You have several syntax errors/warnings in it.

1 Like

the Pelles C ide

And i hate it with passion.

Why.. ?

Why not (at least) atom + gcc?

Your code is missing things your ide doesn't seem to be picking up, or your compiler? (what compiler are you using?) (it shouldn't be compiling)

You're using malloc you should include stdlib.h to use it, your compiler should complain and fail to compile if you don't.

your main() is missing a type

Your fscanf should reference the memory address

fscanf(in, "%d", &num);

not

fscanf(in, "%d", num);

Among other things. My C is very rusty.

Ill check out atom + gcc, and thanks for the help :)

1 Like

Atom + look for the gcc plugins, theres a post on the forum about using it in windows.

1 Like

I have no idea how your code is compiling...

@Eden has found most of your issues. Your main program doesn't return and doesn't have a type.
According to the C11 standard these are the only valid main definitions:

int main(void);
int main(int argc, char* argv[]);

Just a friendly word of advice. You should try to download and install and IDE of some kind for you programming class. Using print statements for debugging purposes is absolutely awful (no matter what your dumb professors might say). There are plenty of good free IDEs out there that are cross platform, but if you are on windows then nothing beats visual studio. The 2017 version was just released, and the community edition of it is free to use for academic use (assuming you also have a Microsoft account of some kind to login with as it is required with that version). Part of learning to programming is learning to use the tools (especially in native development land).