简体   繁体   中英

Binary Search Tree getHeight() function

I am attempting to write a function that will return the height of a BST. I'm writing my code using test-driven development but, unfortunately, it's not passing my test and I can't figure out why.

I'm passing parts 0, 1, and 2 of my test but am failing at part 3. Any help in figuring out what is wrong with my code is much appreciated. Thank you!

Here's some relevant excerpts of my code:

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;
}

Edit:

In case anyone looks at this and is wondering about the answer, I figured it out. Here is my new code.

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();
    }
}

It was just a matter of flipping leftSide++ to ++leftSide.

discard your code. try to do this,dude

int getHeight(tree *p)
{
   if (!p)
       return 0;

   int left = getHeight(p->left);
   int right = getHeight(p->right); 
   return max(left, right) +1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM