User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,944 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,891 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 9959 | Replies: 2
Reply
Join Date: Feb 2005
Posts: 2
Reputation: spitfire is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spitfire spitfire is offline Offline
Newbie Poster

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

  #1  
Feb 14th, 2005
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);
	return a;
}

// 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;
}
Last edited by alc6379 : Feb 15th, 2005 at 11:37 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2005
Posts: 2
Reputation: spitfire is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
spitfire spitfire is offline Offline
Newbie Poster

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

  #2  
Feb 14th, 2005
The place where I am getting the errors is in bold.

Originally Posted by spitfire
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);
return a;
}

// 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;
}
Reply With Quote  
Join Date: Sep 2004
Posts: 6,019
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

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

  #3  
Feb 14th, 2005
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.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC