我正在尝试编写一个返回BST高度的函数。我使用测试驱动开发编写代码,但不幸的是,它没有通过我的测试,我无法弄清楚原因。
我正在通过测试的第0,1和2部分,但在第3部分失败了。我非常感谢任何帮助我找出我的代码有什么问题。谢谢!
这里是我的代码的一些相关摘录:
BST.cpp
unsigned BST::Node::getHeight() const{
unsigned leftSide = 0;
if(myLeft != NULL){
myLeft->getHeight();
}
unsigned rightSide = 0;
if(myRight != NULL){
myRight->getHeight();
}
if(leftSide > rightSide){
return leftSide++;
} else{
return rightSide++;
}
}
unsigned BST::getHeight() const{
if(myNumItems == 0){
return 0;
} else if(myNumItems == 1){
return 1;
} else if(myNumItems == 2){
return 2;
} else {
return myRoot->getHeight();
}
}
BST.h
class BST {
public:
BST();
virtual ~BST();
unsigned getHeight() const;
private:
struct Node {
Node(const Item& it);
virtual ~Node();
void insert(const Item& it);
unsigned getHeight() const;
Item myItem;
Node* myLeft;
Node* myRight;
};
Node* myRoot;
unsigned myNumItems;
friend class BST_Tester;
};
BST_Tester.cpp
void BST_Tester::testGetHeight() {
cout << "Testing getHeight()..." << flush;
BST bst;
// empty
assert( bst.getHeight() == 0 );
cout << " 0 " << flush;
// balanced
bst.insert(44);
assert( bst.getHeight() == 1 );
cout << " 1 " << flush;
bst.insert(22);
assert( bst.getHeight() == 2 );
cout << " 2 " << flush;
bst.insert(66);
assert( bst.getHeight() == 2 );
cout << " 3 " << flush;
bst.insert(77);
assert( bst.getHeight() == 3 );
cout << " 4a " << flush;
bst.insert(55);
assert( bst.getHeight() == 3 );
cout << " 4b " << flush;
bst.insert(33);
assert( bst.getHeight() == 3 );
cout << " 4c " << flush;
bst.insert(11);
assert( bst.getHeight() == 3 );
cout << " 4d " << flush;
bst.insert(88);
assert( bst.getHeight() == 4 );
cout << " 4e " << flush;
// chained ascending
BST bst2a;
bst2a.insert(11);
bst2a.insert(22);
bst2a.insert(33);
bst2a.insert(44);
bst2a.insert(55);
bst2a.insert(66);
bst2a.insert(77);
assert( bst2a.getHeight() == 7 );
cout << " 5a " << flush;
// chained descending
BST bst2b;
bst2b.insert(77);
bst2b.insert(66);
bst2b.insert(55);
bst2b.insert(44);
bst2b.insert(33);
bst2b.insert(22);
bst2b.insert(11);
assert( bst2b.getHeight() == 7 );
cout << " 5b " << flush;
// four 4-node permutations
BST bst3;
bst3.insert(44);
assert( bst3.getHeight() == 1 );
cout << " 6a " << flush;
bst3.insert(22);
assert( bst3.getHeight() == 2 );
cout << " 6b " << flush;
bst3.insert(33);
assert( bst3.getHeight() == 3 );
cout << " 6c " << flush;
bst3.insert(55);
assert( bst3.getHeight() == 3 );
cout << " 6d " << flush;
BST bst4;
bst4.insert(44);
assert( bst4.getHeight() == 1 );
cout << " 7a " << flush;
bst4.insert(33);
assert( bst4.getHeight() == 2 );
cout << " 7b " << flush;
bst4.insert(22);
assert( bst4.getHeight() == 3 );
cout << " 7c " << flush;
bst4.insert(55);
assert( bst4.getHeight() == 3 );
cout << " 7d " << flush;
BST bst5;
bst5.insert(44);
assert( bst5.getHeight() == 1 );
cout << " 8a " << flush;
bst5.insert(33);
assert( bst5.getHeight() == 2 );
cout << " 8b " << flush;
bst5.insert(55);
assert( bst5.getHeight() == 2 );
cout << " 8c " << flush;
bst5.insert(66);
assert( bst5.getHeight() == 3 );
cout << " 8d " << flush;
BST bst6;
bst6.insert(44);
assert( bst6.getHeight() == 1 );
cout << " 9a " << flush;
bst6.insert(33);
assert( bst6.getHeight() == 2 );
cout << " 9b " << flush;
bst6.insert(66);
assert( bst6.getHeight() == 2 );
cout << " 9c " << flush;
bst6.insert(55);
assert( bst6.getHeight() == 3 );
cout << " 9d " << flush;
cout << " Passed!" << endl;
}
修改
如果有人看到这个并且想知道答案,我就明白了。这是我的新代码。
unsigned BST::Node::getHeight() const{
unsigned leftSide = 0;
if(myLeft != NULL){
leftSide = myLeft->getHeight();
}
unsigned rightSide = 0;
if(myRight != NULL){
rightSide = myRight->getHeight();
}
if(leftSide > rightSide){
return ++leftSide;
} else{
return ++rightSide;
}
}
unsigned BST::getHeight() const{
if(myNumItems == 0){
return 0;
} else {
return myRoot->getHeight();
}
}
这只是将leftSide ++翻转到++ leftSide。
答案 0 :(得分:0)
丢弃您的代码。 试着这样做,老兄
int getHeight(tree *p)
{
if (!p)
return 0;
int left = getHeight(p->left);
int right = getHeight(p->right);
return max(left, right) +1;
}