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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 2
Reputation: spitfire is an unknown quantity at this point 
Solved Threads: 0
spitfire spitfire is offline Offline
Newbie Poster

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

 
0
  #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:

  1. // declaration of a binary tree with pointers to left and right children//
  2.  
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define SIZE 9
  8.  
  9. typedef int DATA;
  10.  
  11. struct node
  12. {
  13. DATA dat;
  14. struct node *left;
  15. struct node *right;
  16. };
  17.  
  18. typedef struct node bNODE;
  19. typedef node *BTREE;
  20.  
  21. void inorder(const BTREE root);
  22. void preorder(const BTREE root);
  23. void postorder(const BTREE root);
  24. BTREE makeTree(int a[], int i, int SIZE);
  25. BTREE new_node();
  26. BTREE init_node(DATA dat1, BTREE par1, BTREE par2);
  27. BTREE insert(BTREE parent, BTREE root, int item);
  28. int count_nodes(BTREE root, int cnt);
  29. int count_Lnodes(BTREE root, int cnt);
  30.  
  31. int *makeArray(BTREE root, int val[]);
  32. int *inorder_traversal(int i, BTREE b_tree, int *valPtr);
  33. int *order_array(int a[]);
  34. void divide_array(int a[], int b[], int c[], int SIZE);
  35. int find_center(int a[], int SIZE);
  36.  
  37. int main()
  38. {
  39. // int command;
  40.  
  41. BTREE b_tree;
  42. int array_of_ints[] = {5, 10, 2, 5, 7, 12, 3, 9, 8};
  43. int val[9] = {0};
  44. int *valPtr = 0;
  45.  
  46. b_tree = makeTree)array_of_ints, 0, SIZE);
  47. printf("Tree printed 'in-order'\n");
  48. preorder (b_tree);
  49. putchar('\n');
  50. printf("Number of nodes = %d\n", count_nodes(b_tree, 0));
  51. printf("Number of Leaf nodes = %d\n", count_Lnodes(b_tree, 0));
  52. printf("Here is the array made from the tree:\n");
  53. valPtr = makeArray(b_tree, val);
  54. return 0;
  55.  
  56. }
  57.  
  58. // put an array in alphabetical order
  59. int *order_array(int a[])
  60. {
  61. int j, i, temp;
  62. int *b = a;
  63.  
  64. for (i = 0; i < SIZE-1; ++i)
  65. for (j = i+1; j < SIZE; ++j)
  66. if (a[i] > a[j])
  67. {
  68. temp = a[i];
  69. a[i] = a[j];
  70. a[j] = temp;
  71. }
  72. return b;
  73. }
  74.  
  75. // put an array in alphabetical order
  76. int *order_array(int a[])
  77. {
  78. int j, i, temp;
  79. int *b = a;
  80.  
  81. for (i = 0; i < SIZE-1; ++i)
  82. for (j = i+1; j < SIZE; ++j)
  83. if (a[i] > a[j])
  84. {
  85. temp = a[i];
  86. a[i] = a[j];
  87. a[j] = temp;
  88. }
  89. return b;
  90. }
  91.  
  92. // send function a, the array to be divided, and b and c, the 2 array pointers
  93. void divide_array(int a[], int b[], int c[], int SIZE)
  94. {
  95. int i, j;
  96.  
  97. for (i = 0; i < SIZE/2; ++i) b[i] = a[i];
  98. i++; // removing middle item, the one being added
  99. for (j = 0; i < SIZE; ++i, ++j) c[j] = a[i];
  100. }
  101.  
  102. // find center of an array
  103. int find_center(int a[], int SIZE)
  104. {
  105. int i = 0, center;
  106.  
  107. while (i < SIZE) ++i;
  108. center = i / 2;
  109. printf("%d.. %d .. %d\n", i, center, a[center]);
  110. return a[center];
  111. }
  112.  
  113. // create ordered binary tree using recursion, divide array, then insert center int
  114. BTREE makeTree(int a[], int i, int SIZE)
  115. {
  116. int b[SIZE] = {0}, c[SIZE] = {0};
  117.  
  118. if(SIZE == 0)
  119. return NULL;
  120. if(SIZE == 1)
  121. init_node(a[0], NULL, NULL);
  122. if(i <= SIZE)
  123. {
  124. divide_array(a, b, c, SIZE);
  125. return init_node(find_center(a, SIZE), makeTree(b, 2 * i + 1, SIZE / 2),
  126.  
  127. makeTree(c, 2 * i + 2, SIZE - SIZE / 2 - 1));
  128. }
  129. }
  130.  
  131.  
  132. // create array using inorder binary tree traversal and print
  133. int *makeArray(BTREE root, int val[])
  134. {
  135. int i = 0, *valPtr;
  136.  
  137. valPtr = val;
  138. valPtr = inorder_traversal(i, root, val);
  139. putchar('\n');
  140. return valPtr;
  141. }
  142.  
  143. // create array from binary tree
  144. int *inorder_traversal(int i, BTREE parent, int val[])
  145. {
  146. int *valPtr;
  147. valPtr = val;
  148.  
  149. if (parent != NULL)
  150. {
  151. inorder_traversal(i, parent->left, val);
  152. val[i] = parent->dat;
  153. printf("%d ", val[i]);
  154. ++i;
  155. inorder_traversal(i, parent->right, val);
  156. }
  157. return valPtr;
  158. }
  159.  
  160. // inorder binary tree traversal and print
  161. void inorder(const BTREE parent)
  162. {
  163. if (parent != NULL)
  164. {
  165. inorder(parent->left);
  166. printf("%d ", parent->dat);
  167. inorder(parent->right);
  168. }
  169. }
  170.  
  171. // preorder binary tree traversal and print
  172. void preorder(const BTREE root)
  173. {
  174. if (root != NULL)
  175. {
  176. printf("%d ", root->dat);
  177. preorder(root->left);
  178. preorder(root->right);
  179. }
  180. }
  181.  
  182. // creating binary trees
  183. BTREE new_node()
  184. {
  185. return (malloc(sizeof(bNODE)));
  186. }
  187.  
  188. BTREE init_node(dat1, BTREE par1, BTREE par2)
  189. {
  190. BTREE t;
  191. t = new_node();
  192. t->dat = dat1;
  193. t->left = par1;
  194. t->right = par2;
  195. return t;
  196. }
  197.  
  198.  
  199. // count total number of nodes
  200. int count_nodes(BTREE root, int cnt)
  201. {
  202. if (root != NULL)
  203. {
  204. cnt = count_nodes(root->left, cnt); /* recur left */
  205. cnt = count_nodes(root->right, ++cnt); /* recur right */
  206. }
  207. return cnt;
  208. }
  209.  
  210. // count number of leaf nodes
  211. int count_Lnodes(BTREE root, int cnt)
  212. {
  213. if (root != NULL)
  214. {
  215. cnt = count_Lnodes(root->left, cnt);
  216. cnt = count_Lnodes(root->right, cnt);
  217. if (root->left == NULL && root->right == NULL) ++cnt;
  218. }
  219. return cnt;
  220. }
Last edited by alc6379; Feb 16th, 2005 at 12:37 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: spitfire is an unknown quantity at this point 
Solved Threads: 0
spitfire spitfire is offline Offline
Newbie Poster

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

 
0
  #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[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;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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:
  1. #define SIZE 9
  2. BTREE makeTree(int a[], int i, int SIZE);
Becomes this:
  1. #define SIZE 9
  2. 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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC