From 6f1f6d5af3b50d51ea59aa56cf7ea164a14bdf3d Mon Sep 17 00:00:00 2001 From: Shine wOng <1551885@tongji.edu.cn> Date: Mon, 27 May 2019 11:10:56 +0800 Subject: [PATCH] update BinNode::size() method, and passed tests --- thu_dsa/chp5/binNode.h | 2 +- thu_dsa/chp5/testBinTree.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/thu_dsa/chp5/binNode.h b/thu_dsa/chp5/binNode.h index d6de521..9e02c22 100644 --- a/thu_dsa/chp5/binNode.h +++ b/thu_dsa/chp5/binNode.h @@ -68,7 +68,7 @@ void BinNode::goAlongLeftBranch(Stack &S){ template int BinNode::size() const { int leftSize = this->leftChild ? this->leftChild->size() : 0; - int rightSize = this - rightChild ? this->rightChild->size() : 0; + int rightSize = this -> rightChild ? this->rightChild->size() : 0; return leftSize + rightSize + 1; } diff --git a/thu_dsa/chp5/testBinTree.cpp b/thu_dsa/chp5/testBinTree.cpp index b01544a..3d50704 100644 --- a/thu_dsa/chp5/testBinTree.cpp +++ b/thu_dsa/chp5/testBinTree.cpp @@ -6,6 +6,7 @@ using std::cout; using std::endl; void test_insert(); +void test_size(); void test_preTraversal(); void test_inTraversal(); void test_postTraversal(); @@ -15,6 +16,7 @@ int main(){ cout << "Running test." << endl; test_insert(); + test_size(); test_preTraversal(); test_inTraversal(); test_postTraversal(); @@ -56,6 +58,16 @@ void test_insert(){ assert(left->height == 0 && right->height == 0); } +void test_size(){ + BinTree intTree; + BinNodePosi(int) root = intTree.insertAsRoot(8); + assert(root->size() == 1); + intTree.insertAsLC(root, 7); + assert(root->size() == 2); + intTree.insertAsRC(root, 14); + assert(root->size() == 3); +} + template struct Print{ virtual void operator()(T const &val){