我越来越:
Error 1 error C2059: syntax error : 'type' c:\users\user\documents\visual studio 2010\projects\ex4\q4\tree.h 40 1 Q4
Error 2 error C2059: syntax error : 'type' c:\users\user\documents\visual studio 2010\projects\ex4\q4\tree.c 174 1 Q4
max()
位于 tree.c 的底部
这是树.h:
#ifndef __TREE_H
#define __TREE_H
/***** Type definitions *****/
typedef struct TreeNode{
int data;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
typedef struct tree{
TreeNode* root;
} Tree;
/****** Prototypes *****/
/* Testing trees */
Tree testTreeCase1();
Tree testTreeCase2();
Tree testTreeCase3();
Tree testTreeCase4();
Tree testTreeCase5();
/* This function creates a new node */
TreeNode* createNewTreeNode(int data, TreeNode *left, TreeNode *right);
/* This function deallocates a given tree */
void freeTree(Tree *tr);
void freeTreeHelper(TreeNode *root);
/* This function returns the number of nodes of a given tree */
int numNodes(Tree *tr);
int numNodesHelper(TreeNode *root);
/* This function returns the sum of all of the nodes data of a given tree */
int sumNodes(Tree *tr);
int sumNodesHelper(TreeNode *root);
int height(Tree *tr);
int heightHelper(TreeNode *root);
int max(int a, int b);
#endif
这是tree.c:
#include <stdlib.h>
#include <stdio.h>
#include "tree.h"
/* This function creates a new node */
TreeNode* createNewTreeNode(int data, TreeNode *left, TreeNode *right)
{
TreeNode *res;
res = (TreeNode*)malloc(sizeof(TreeNode));
res->data = data;
res->left = left;
res->right = right;
return res;
}
/* Shell function for numNodes() */
int numNodes(Tree *tr)
{
return numNodesHelper(tr->root);
}
/* This function returns the number of nodes of a given tree's root node */
int numNodesHelper(TreeNode *root)
{
int numNodesLeft, numNodesRight;
if (root == NULL)
return 0;
else
{
numNodesLeft = numNodesHelper(root->left);
numNodesRight = numNodesHelper(root->right);
return numNodesLeft + numNodesRight + 1;
}
}
/* Shell function for sumNodes() */
int sumNodes(Tree *tr)
{
return sumNodesHelper(tr->root);
}
/* This function returns the sum of all of the nodes data of a given tree's root node */
int sumNodesHelper(TreeNode *root)
{
int sumNodesLeft, sumNodesRight;
if (root == NULL)
return 0;
else
{
sumNodesLeft = sumNodesHelper(root->left);
sumNodesRight = sumNodesHelper(root->right);
return sumNodesLeft + sumNodesRight + root->data;
}
}
/* Shell function for freeTree() */
void freeTree(Tree *tr)
{
freeTreeHelper(tr->root);
tr->root = NULL;
}
/* This function deallocates a given tree's root node */
void freeTreeHelper(TreeNode *root)
{
if (root == NULL)
return;
else if (root->left == NULL && root->right == NULL)
free(root);
else
{
freeTreeHelper(root->right);
freeTreeHelper(root->left);
free(root);
}
}
/* Shell function for heightHelper() */
int height (Tree *tr)
{
return heightHelper(tr->root);
}
/* This function returns the height of a given tree's root node */
int heightHelper(TreeNode *root)
{
int heightLeft, heightRight;
if (root == NULL) // case empty tree
return -1;
else if (root->left == NULL && root->right == NULL)
return 0;
else if (root->left == NULL)
return 1 + heightHelper(root->right);
else if (root->right == NULL)
return 1+ heightHelper(root->left);
else
{
heightLeft = heightHelper(root->left);
heightRight = heightHelper(root->right);
return 1 + (heightLeft>heightRight?heightLeft:heightRight);
}
}
// Checking trees according to special cases
/* Case 1: Empty tree */
Tree testTreeCase1()
{
Tree resTree;
resTree.root = NULL;
return resTree;
}
/* Case 2: Only root node */
Tree testTreeCase2()
{
Tree resTree;
TreeNode *root;
root = createNewTreeNode(1, NULL, NULL);
resTree.root = root;
return resTree;
}
/* Case 3: Only right sub-tree exists */
Tree testTreeCase3()
{
Tree resTree;
TreeNode *root, *l1_r, *l2_rr, *l2_rl;
l2_rl = createNewTreeNode(4, NULL, NULL);
l2_rr = createNewTreeNode(3, NULL, NULL);
l1_r = createNewTreeNode(2, l2_rl, l2_rr);
root = createNewTreeNode(1, NULL, l1_r);
resTree.root = root;
return resTree;
}
/* Case 4: Only left sub-tree exists */
Tree testTreeCase4()
{
Tree resTree;
TreeNode *root, *l1_l, *l2_lr, *l2_ll;
l2_ll = createNewTreeNode(4, NULL, NULL);
l2_lr = createNewTreeNode(3, NULL, NULL);
l1_l = createNewTreeNode(2, l2_ll, l2_lr);
root = createNewTreeNode(1, l1_l, NULL);
resTree.root = root;
return resTree;
}
/* Case 5: Both left and right sub-trees exists */
Tree testTreeCase5()
{
Tree resTree;
TreeNode *root, *l1_r, *l1_l, *l2_lr, *l2_ll, *l2_rr, *l2_rl;
l2_ll = createNewTreeNode(7, NULL, NULL);
l2_lr = createNewTreeNode(6, NULL, NULL);
l2_rl = createNewTreeNode(5, NULL, NULL);
l2_rr = createNewTreeNode(4, NULL, NULL);
l1_l = createNewTreeNode(2, l2_ll, l2_lr);
l1_r = createNewTreeNode(3, l2_rl, l2_rr);
root = createNewTreeNode(1, l1_l, l1_r);
resTree.root = root;
return resTree;
}
int max(int a, int b)
{
return a>b ? a:b;
}