二叉树节点计数有问题
Having issues with Binary Tree node counting
在 public 中调用节点计数函数时,我得到了不兼容的声明,私下里我的 returns 与该函数不匹配,我无法在测试中调用 driver.
float, int, void, bool(愚蠢我知道)。
菲律宾共产党
void BinaryTree::count(TreeNode* root) {
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
return count(root->left) + count(root->right) + 1;
}
Header(我想我需要做这个 public 所以我可以在测试中调用 driver 但我只能在 CPP 中从 Private 声明。)
void count(TreeNode *);
Driver
cout << "Total Number of Nodes " << endl;
tree.count();
cout << endl;
在 Driver 测试中,CPP tree.count 是不可访问的,这是可以理解的,因为它是从私有调用的,但是作为 public 调用,声明是不兼容的。
void BinaryTree::count(TreeNode* root) {
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
return count(root->left) + count(root->right) + 1;
}
那个操作return一个int(至少一个数字),它必须有一个签名returning一个数字,例如
int BinaryTree::count(TreeNode* root)
你的定义也白白复杂了,可以
int BinaryTree::count(TreeNode* root) {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
并且因为它不修改实例,所以使其成为 const
int BinaryTree::count(TreeNode* root) const {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
有
tree.count();
没有 arg 并明显写入计数,您需要其他操作,例如
void BinaryTree::count() const {
cout << count(..the tree node..);
}
那个操作一定是public,可能前面那个是私有的
无论如何最好不要写计数而是 return 它,让调用者做它想做的事。
所以最后是这样的:
// I use a _struct because fields accessible by BinaryTree
// but may be a class and BinaryTree is a friend class etc
struct TreeNode {
// ...
TreeNode * left;
TreeNode * right;
// ...
};
class BinaryTree {
public:
// ...
int count() const { return count(tree); }
// ...
private:
// ...
int count(TreeNode *) const;
// ...
TreeNode * tree;
// ...
};
int BinaryTree::count(TreeNode* root) const {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
还有某个地方cout << "Total Number of Nodes " << tree.count() << endl;
在 public 中调用节点计数函数时,我得到了不兼容的声明,私下里我的 returns 与该函数不匹配,我无法在测试中调用 driver.
float, int, void, bool(愚蠢我知道)。
菲律宾共产党
void BinaryTree::count(TreeNode* root) {
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
return count(root->left) + count(root->right) + 1;
}
Header(我想我需要做这个 public 所以我可以在测试中调用 driver 但我只能在 CPP 中从 Private 声明。)
void count(TreeNode *);
Driver
cout << "Total Number of Nodes " << endl;
tree.count();
cout << endl;
在 Driver 测试中,CPP tree.count 是不可访问的,这是可以理解的,因为它是从私有调用的,但是作为 public 调用,声明是不兼容的。
void BinaryTree::count(TreeNode* root) { if(root == NULL) return 0; else if(root->left == NULL && root->right == NULL) return 1; else return count(root->left) + count(root->right) + 1; }
那个操作return一个int(至少一个数字),它必须有一个签名returning一个数字,例如
int BinaryTree::count(TreeNode* root)
你的定义也白白复杂了,可以
int BinaryTree::count(TreeNode* root) {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
并且因为它不修改实例,所以使其成为 const
int BinaryTree::count(TreeNode* root) const {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
有
tree.count();
没有 arg 并明显写入计数,您需要其他操作,例如
void BinaryTree::count() const {
cout << count(..the tree node..);
}
那个操作一定是public,可能前面那个是私有的
无论如何最好不要写计数而是 return 它,让调用者做它想做的事。
所以最后是这样的:
// I use a _struct because fields accessible by BinaryTree
// but may be a class and BinaryTree is a friend class etc
struct TreeNode {
// ...
TreeNode * left;
TreeNode * right;
// ...
};
class BinaryTree {
public:
// ...
int count() const { return count(tree); }
// ...
private:
// ...
int count(TreeNode *) const;
// ...
TreeNode * tree;
// ...
};
int BinaryTree::count(TreeNode* root) const {
return (root == NULL)
? 0
: count(root->left) + count(root->right) + 1;
}
还有某个地方cout << "Total Number of Nodes " << tree.count() << endl;