update BinNode::size() method, and passed tests

This commit is contained in:
Shine wOng
2019-05-27 11:10:56 +08:00
parent 58f9b08757
commit 6f1f6d5af3
2 changed files with 13 additions and 1 deletions

View File

@@ -68,7 +68,7 @@ void BinNode<T>::goAlongLeftBranch(Stack<BinNodePosi(T)> &S){
template <typename T>
int BinNode<T>::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;
}

View File

@@ -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<int> 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 <typename T>
struct Print{
virtual void operator()(T const &val){