samedi 29 octobre 2016

Why won't my test statement check a *& struct argument accurately?

I've been trying to map out a binary search tree in C++, but I'm having some difficulty with my remove method. If it worked, it would basically use an inorder traversal to search the tree for a node with the value passed into the method, recursively calling itself so long as it's actually on a node that exists - if it's not, then it promptly returns itself and allows the method that's one "step up" in the recursion to check the area it's in, just like a normal inorder traversal. The problem is, my if statement checking to see if the current node exists seems to just not work, always returning true and causing it to infinitely iterate down the left branch that has a reasonable endpoint. Here's the code I'm using:

template<class T>
void binSTree<T>::remove(Node<T>*& nowOn, const T& input)
{
    if(nowOn) //This is the part that breaks. My cout statement has proved that by repeating itself an infinite number of times.
    {
        cout << "So we know it's entering this.";
        if(nowOn->left)
            remove(nowOn->left, input);
        if(nowOn->data == input)
        {
            if(!(nowOn->left) && !(nowOn->right))
            {
                delete nowOn;
            }
            if(nowOn->left && !(nowOn->right))
            {
                if(!(pred(nowOn)->left->data == input))
                    pred(nowOn)->left = nowOn->left;
                else if(!(pred(nowOn)->right->data == input))
                    pred(nowOn)->right = nowOn->left;

                delete nowOn;
        }
        if(nowOn->right && !(nowOn->left))
        {
            if(!(pred(nowOn)->left->data == input))
                pred(nowOn)->left = nowOn->right;
            else if(!(pred(nowOn)->right->data == input))
                pred(nowOn)->right = nowOn->right;

            delete nowOn;
        }
        if(nowOn->left && nowOn->right)
        {
            if(!(pred(nowOn)->left->data == input))
                pred(nowOn)->left = nowOn->right;
            else if(!(pred(nowOn)->right->data == input))
                pred(nowOn)->right = nowOn->right;

            delete nowOn;
        }
    }
    if(nowOn->right)
        remove(nowOn->right, input);
}
else
{
    return;
}
return;
}

The pred method is a simple, non-recursive stack that runs through the entire tree to find something where either the left or right nodes are the input node. That's been tested and works just fine. As for the node, here's the code for it:

template < class T > class binTree;  // forward declaration
template < class T > class binSTree; // forward declaration

template < class T > class Node {
friend class binTree < T >;          // binTree is friend
friend class binSTree < T >;         // binSTree is friend
public:
    // default constructor
    Node ( const T& x = T ( ), Node < T >* l = 0, Node < T >* r = 0 ) :
        data ( x ), left ( l ), right ( r ) { }
private:
    T data;                         // data component
    Node < T > *left, *right;       // left and right links
};

binTree is a different binary tree that can't delete, but has its own insert, height, size, and inorder traversal methods. binSTree, the class with the remove method, is a derived class of binTree. I'm reasonably certain that the error comes from me trying to check a Node<T>*&, the *& part specifically, with an if statement, but I can't figure out how to do anything about it. Anyone have any ideas?

Aucun commentaire:

Enregistrer un commentaire