Question: Write the definition of the function nodeCount that returns the number of nodes in the binary tree. Add this function to the class binaryTreeType and
Write the definition of the function nodeCount that returns the number of nodes in the binary tree. Add this function to the class binaryTreeType and create a program to test this function.
main.cpp:
#include
#include "binaryTreeType.h"
#include "gtest/gtest.h"
TEST(Tree, NodeCount)
{
binaryTreeType
ASSERT_EQ(tree.nodeCount(tree.root), 0);
tree.insert(1);
tree.insert(2);
tree.insert(3);
ASSERT_EQ(tree.nodeCount(tree.root), 3);
tree.insert(4);
tree.insert(5);
ASSERT_EQ(tree.nodeCount(tree.root), 5);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
binaryTreeType.h:
#include
using namespace std;
template
struct nodeType
{
elemType info;
nodeType
nodeType
};
template
class binaryTreeType
{
public:
int nodeCount(nodeType
// Returns the number of nodes in the binary tree.
// Postcondition: Returns the number of nodes.
...
};
template
int binaryTreeType
{
if (p == NULL)
return 0;
else
return 1 + nodeCount(p->lLink) + nodeCount(p->rLink);
}
task:
nodeCount implemented
0 out of 1 checks passed. Review the results below for more details.
Checks
Unit TestIncomplete
Method nodeCount implemented
Build Status
Build Failed
Build Output
In file included from /root/sandbox0d683d16/binarySearchTree.h:5:0, from /root/sandbox0d683d16/nt-test-de836066.cpp:2: /root/sandbox0d683d16/binaryTree.h:19:5: error: expected unqualified-id before '...' token ... ^~~ In file included from /usr/include/gtest/gtest.h:1929:0, from /root/sandbox0d683d16/nt-test-de836066.cpp:1: /root/sandbox0d683d16/nt-test-de836066.cpp: In member function 'virtual void Tree_1_Test::TestBody()': /root/sandbox0d683d16/nt-test-de836066.cpp:6:22: error: 'class bSearchTreeType' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 0); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:6:3: error: template argument 1 is invalid ASSERT_EQ(treeRoot.treeNodeCount(), 0); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:6:22: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 0); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:10:22: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 3); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:10:3: error: template argument 1 is invalid ASSERT_EQ(treeRoot.treeNodeCount(), 3); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:10:22: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 3); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:15:22: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 6); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:15:3: error: template argument 1 is invalid ASSERT_EQ(treeRoot.treeNodeCount(), 6); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:15:22: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 6); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:19:21: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 7); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:19:2: error: template argument 1 is invalid ASSERT_EQ(treeRoot.treeNodeCount(), 7); ^ /root/sandbox0d683d16/nt-test-de836066.cpp:19:21: error: 'class bSearchTreeType ' has no member named 'treeNodeCount'; did you mean 'nodeCount'? ASSERT_EQ(treeRoot.treeNodeCount(), 7); ^ In file included from /root/sandbox0d683d16/nt-test-de836066.cpp:2:0: /root/sandbox0d683d16/binarySearchTree.h: In instantiation of 'void bSearchTreeType ::insert(const elemType&) [with elemType = int]': /root/sandbox0d683d16/nt-test-de836066.cpp:7:20: required from here /root/sandbox0d683d16/binarySearchTree.h:87:15: error: 'class bSearchTreeType ' has no member named 'root' if (this->root == nullptr) ~~~~~~^~~~ /root/sandbox0d683d16/binarySearchTree.h:88:15: error: 'class bSearchTreeType ' has no member named 'root' this->root = newNode; ~~~~~~^~~~ /root/sandbox0d683d16/binarySearchTree.h:91:25: error: 'class bSearchTreeType ' has no member named 'root' current = this->root; ~~~~~~^~~~ make[2]: *** [CMakeFiles/runTests.dir/nt-test-de836066.cpp.o] Error 1 make[1]: *** [CMakeFiles/runTests.dir/all] Error 2 make: *** [all] Error 2
Test Contents
bSearchTreeTypetreeRoot; TEST(Tree,1) { ASSERT_EQ(treeRoot.treeNodeCount(), 0); treeRoot.insert(1); treeRoot.insert(434); treeRoot.insert(245); ASSERT_EQ(treeRoot.treeNodeCount(), 3); treeRoot.insert(543); treeRoot.insert(5563); treeRoot.insert(324532); ASSERT_EQ(treeRoot.treeNodeCount(), 6); treeRoot.insert(3245332); ASSERT_EQ(treeRoot.treeNodeCount(), 7); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
