954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ complete binary tree using an array. Unexpected end file

I am getting errors when trying to compile my program, can anyone help?
Errors are
error C2143: syntax error : missing ')' before 'constant'
error C2143: syntax error : missing ';' before 'constant'
fatal error C1004: unexpected end of file found
Here is my code:

//    declaration of a binary tree with pointers to left and right children//

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 9

typedef int DATA;

struct node
{
	DATA dat;
	struct node *left;
	struct node *right;
};

typedef struct node bNODE;
typedef node *BTREE;

void inorder(const BTREE root);
void preorder(const BTREE root);
void postorder(const BTREE root);
BTREE makeTree(int a[], int i, int SIZE);
BTREE new_node();
BTREE init_node(DATA dat1, BTREE par1, BTREE par2);
BTREE insert(BTREE parent, BTREE root, int item);
int count_nodes(BTREE root, int cnt);
int count_Lnodes(BTREE root, int cnt);

int *makeArray(BTREE root, int val[]);
int *inorder_traversal(int i, BTREE b_tree, int *valPtr);
int *order_array(int a[]);
void divide_array(int a[], int b[], int c[], int SIZE);
int find_center(int a[], int SIZE);

int main()
{
//	int command;

	BTREE b_tree;
	int array_of_ints[] = {5, 10, 2, 5, 7, 12, 3, 9, 8};
	int val[9] = {0};
	int *valPtr = 0;

	b_tree = makeTree)array_of_ints, 0, SIZE);
	printf("Tree printed 'in-order'\n");
	preorder (b_tree);
	putchar('\n');
	printf("Number of nodes = %d\n", count_nodes(b_tree, 0));
	printf("Number of Leaf nodes = %d\n", count_Lnodes(b_tree, 0));
	printf("Here is the array made from the tree:\n");
	valPtr = makeArray(b_tree, val);
	return 0;

}

// put an array in alphabetical order
int *order_array(int a[])
{
	int j, i, temp;
	int *b = a;

	for (i = 0; i < SIZE-1; ++i)
	for (j = i+1; j < SIZE; ++j)
	if (a[i] > a[j])
	{
		temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
return b;
}

	// put an array in alphabetical order
int *order_array(int a[])
{
	int j, i, temp;
	int *b = a;

	for (i = 0; i < SIZE-1; ++i)
	for (j = i+1; j < SIZE; ++j)
	if (a[i] > a[j])
	{
		temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	return b;
}

// send function a, the array to be divided, and b and c, the 2 array pointers
void divide_array(int a[], int b[], int c[], int SIZE)
{
	int i, j;

	for (i = 0; i < SIZE/2; ++i) b[i] = a[i];
	i++; // removing middle item, the one being added
	for (j = 0; i < SIZE; ++i, ++j) c[j] = a[i];
}

// find center of an array
int find_center(int a[], int SIZE)
{
	int i = 0, center;

	while (i < SIZE) ++i;
	center = i / 2;
	printf("%d.. %d .. %d\n", i, center, a[center]);
	return a[center];
}

// create ordered binary tree using recursion, divide array, then insert center int
BTREE makeTree(int a[], int i, int SIZE)
{
	int b[SIZE] = {0}, c[SIZE] = {0};

	if(SIZE == 0) 
		return NULL;
	if(SIZE == 1)
		init_node(a[0], NULL, NULL);
	if(i <= SIZE)
	{
		divide_array(a, b, c, SIZE);
		return init_node(find_center(a, SIZE), makeTree(b, 2 * i + 1, SIZE / 2),

		makeTree(c, 2 * i + 2, SIZE - SIZE / 2 - 1));
	}
}


// create array using inorder binary tree traversal and print
int *makeArray(BTREE root, int val[])
{
	int i = 0, *valPtr;

	valPtr = val;
	valPtr = inorder_traversal(i, root, val);
	putchar('\n');
	return valPtr;
}

// create array from binary tree
int *inorder_traversal(int i, BTREE parent, int val[])
{
	int *valPtr;
	valPtr = val;

	if (parent != NULL)
	{
		inorder_traversal(i, parent->left, val);
		val[i] = parent->dat;
		printf("%d ", val[i]);
		++i;
		inorder_traversal(i, parent->right, val);
	}
	return valPtr;
}

// inorder binary tree traversal and print
void inorder(const BTREE parent)
{
	if (parent != NULL) 
	{
		inorder(parent->left);
		printf("%d ", parent->dat);
		inorder(parent->right);
	}
}

// preorder binary tree traversal and print
void preorder(const BTREE root)
{
	if (root != NULL) 
	{
		printf("%d ", root->dat);
		preorder(root->left);
		preorder(root->right);
	}
}

// creating binary trees
BTREE new_node()
{
	return (malloc(sizeof(bNODE)));
}

BTREE init_node(dat1, BTREE par1, BTREE par2)
{
	BTREE t;
	t = new_node();
	t->dat = dat1;
	t->left = par1;
	t->right = par2;
	return t;
}


// count total number of nodes
int count_nodes(BTREE root, int cnt)
{
	if (root != NULL) 
	{
		cnt = count_nodes(root->left, cnt); /* recur left */
		cnt = count_nodes(root->right, ++cnt); /* recur right */
	}
	return cnt;
}

// count number of leaf nodes
int count_Lnodes(BTREE root, int cnt)
{
	if (root != NULL) 
	{
		cnt = count_Lnodes(root->left, cnt);
		cnt = count_Lnodes(root->right, cnt);
		if (root->left == NULL && root->right == NULL) ++cnt;
	}
	return cnt;
}
spitfire
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

The place where I am getting the errors is in bold.

I am getting errors when trying to compile my program, can anyone help? Errors are error C2143: syntax error : missing ')' before 'constant' error C2143: syntax error : missing ';' before 'constant' fatal error C1004: unexpected end of file found Here is my code: // declaration of a binary tree with pointers to left and right children//

#include #include #include

#define SIZE 9

typedef int DATA;

struct node { DATA dat; struct node *left; struct node *right; };

typedef struct node bNODE; typedef node *BTREE;

void inorder(const BTREE root); void preorder(const BTREE root); void postorder(const BTREE root); BTREE makeTree(int a[], int i, int SIZE); BTREE new_node(); BTREE init_node(DATA dat1, BTREE par1, BTREE par2); BTREE insert(BTREE parent, BTREE root, int item); int count_nodes(BTREE root, int cnt); int count_Lnodes(BTREE root, int cnt);

int *makeArray(BTREE root, int val[]); int *inorder_traversal(int i, BTREE b_tree, int *valPtr); int *order_array(int a[]); void divide_array(int a[], int b[], int c[], int SIZE); int find_center(int a[], int SIZE);

int main() { // int command;

BTREE b_tree; int array_of_ints[] = {5, 10, 2, 5, 7, 12, 3, 9, 8}; int val[9] = {0}; int *valPtr = 0;

b_tree = makeTree)array_of_ints, 0, SIZE); printf("Tree printed 'in-order'\n"); preorder (b_tree); putchar('\n'); printf("Number of nodes = %d\n", count_nodes(b_tree, 0)); printf("Number of Leaf nodes = %d\n", count_Lnodes(b_tree, 0)); printf("Here is the array made from the tree:\n"); valPtr = makeArray(b_tree, val); return 0; }

// put an array in alphabetical order int *order_array(int a[]) { int j, i, temp; int *b = a;

for (i = 0; i < SIZE-1; ++i) for (j = i+1; j < SIZE; ++j) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } return b; }

// put an array in alphabetical order int *order_array(int a[]) { int j, i, temp; int *b = a;

for (i = 0; i < SIZE-1; ++i) for (j = i+1; j < SIZE; ++j) if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } return b; }

// send function a, the array to be divided, and b and c, the 2 array pointers void divide_array(int a[], int b[], int c[], int SIZE) { int i, j;

for (i = 0; i < SIZE/2; ++i) b[i] = a[i]; i++; // removing middle item, the one being added for (j = 0; i < SIZE; ++i, ++j) c[j] = a[i]; }

// find center of an array int find_center(int a[], int SIZE) { int i = 0, center;

while (i < SIZE) ++i; center = i / 2; printf("%d.. %d .. %d\n", i, center, a[center]); return a[center]; }

// create ordered binary tree using recursion, divide array, then insert center int BTREE makeTree(int a[], int i, int SIZE) { int b[SIZE] = {0}, c[SIZE] = {0};

if(SIZE == 0) return NULL; if(SIZE == 1) init_node(a[0], NULL, NULL); if(i <= SIZE) { divide_array(a, b, c, SIZE); return init_node(find_center(a, SIZE), makeTree(b, 2 * i + 1, SIZE / 2),

makeTree(c, 2 * i + 2, SIZE - SIZE / 2 - 1)); } }

// create array using inorder binary tree traversal and print int *makeArray(BTREE root, int val[]) { int i = 0, *valPtr;

valPtr = val; valPtr = inorder_traversal(i, root, val); putchar('\n'); return valPtr; }

// create array from binary tree int *inorder_traversal(int i, BTREE parent, int val[]) { int *valPtr; valPtr = val;

if (parent != NULL) { inorder_traversal(i, parent->left, val); val[i] = parent->dat; printf("%d ", val[i]); ++i; inorder_traversal(i, parent->right, val); } return valPtr; }

// inorder binary tree traversal and print void inorder(const BTREE parent) { if (parent != NULL) { inorder(parent->left); printf("%d ", parent->dat); inorder(parent->right); } }

// preorder binary tree traversal and print void preorder(const BTREE root) { if (root != NULL) { printf("%d ", root->dat); preorder(root->left); preorder(root->right); } }

// creating binary trees BTREE new_node() { return (malloc(sizeof(bNODE))); }

BTREE init_node(dat1, BTREE par1, BTREE par2) { BTREE t; t = new_node(); t->dat = dat1; t->left = par1; t->right = par2; return t; }

// count total number of nodes int count_nodes(BTREE root, int cnt) { if (root != NULL) { cnt = count_nodes(root->left, cnt); /* recur left */ cnt = count_nodes(root->right, ++cnt); /* recur right */ } return cnt; }

// count number of leaf nodes int count_Lnodes(BTREE root, int cnt) { if (root != NULL) { cnt = count_Lnodes(root->left, cnt); cnt = count_Lnodes(root->right, cnt); if (root->left == NULL && root->right == NULL) ++cnt; } return cnt; }

spitfire
Newbie Poster
2 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

Don't use preprocessor macros unless you know how the preprocessor works. It's all to easy to forget that it's textual replacement before anything else. So this:

#define SIZE 9
BTREE makeTree(int a[], int i, int SIZE);

Becomes this:

#define SIZE 9
BTREE makeTree(int a[], int i, int 9);

Which is a syntax error. Therefore, you can't use SIZE anywhere except where you want it replaced with 9.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hi spitfire and Narue, i am doing some work similar to this and i am try to compile your code to study it, but having the erors you are having. Can someone help me out to fix it so that i can run it and study it.
Thanks

wish_C
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You