简体   繁体   中英

Removal of Leaves from Binary Search Tree Issues

I've been working on this god-forsaken assignment for almost a week past it's due date. It's barely worth points anymore, I'm more interested in getting it correct for my own sanity. Anyway, the assignment is to fill a binary search tree, and then run through it deleting all of the leaves. The driver program is supplied for us (and thus, I am unable to post it). I'm almost positive that my issues lie in the private versions of search() and remove() -- though there may be issues with leaf() (I've gotten some seg faults there). I've tried countless variations of this thing, but perhaps some fresh eyes can help. I should probably also mention that this is an inherited template class; also, the Node class consists of a left and right pointer, and a data value. Thanks in advance!

#include "binTree.h"

#ifndef prog6_binSTree_h
#define prog6_binSTree_h

template < class T >
class binSTree : public binTree < T > {
public:
    void insert ( const T& );
    bool search ( const T& ) const;
    bool remove ( const T& );
private:
    void insert ( Node < T >*&, const T& );
    bool search ( Node < T >*, const T& ) const;
    void remove ( Node < T >*&, const T& );
    bool leaf ( Node < T >* node ) const;
};

#endif


template <class T>
void binSTree<T>::insert ( const T& x )
{
    insert (this->root, x);
}



template <class T>
bool binSTree<T>::search ( const T& x ) const
{
    return (search(this->root, x));
}

template <class T>
bool binSTree<T>::remove ( const T& x )
{
    if (search (x))
    {
        remove (this->root, x);
        return true;
    }

    else
        return false;
}

template <class T>
void binSTree<T>::insert ( Node < T >*& node, const T& x)
{
    if(node == nullptr)
        node = new Node<T>(x);

    else if (x < node->data)
        insert (node->left, x);

    else
        insert (node->right, x);
}

template <class T>
bool binSTree<T>::search ( Node < T >* node, const T& val) const
{
    if (node != nullptr)
    {
        if (leaf(node) && node->data == val)
            return true;
        else
            return false;
    }

    else if (node == nullptr)
        return false;

    else if (val < node->data)
         return search(node->left, val);

    else
        return search(node->right, val);

}

template <class T>
void binSTree<T>::remove ( Node < T >*& node, const T& x)
{
    if(node->data == x && leaf(node))
    {
        delete node;
        node = NULL;
    }

    else if (x < node->data)
        remove (node->left, x);

    else
        remove (node->right, x);
}

template <class T>
bool binSTree<T>::leaf ( Node < T >* node ) const
{
    if (node != nullptr)
    {
        if (node->left == nullptr && node->right == nullptr)
            return true;
        else
            return false;
    }
    else
    {
        return false;
    } 
}

Your search() has some problems. The lines beginning with

else if (val < node->data)

will never run, because what precedes them amounts to if (x) { ... } else if (!x) { ... } , and one of those two tests will definitely succeed, so none of the cases remaining will ever fire. So it will never actually recurse!

Also I'm not sure why you check leaf(x) here before returning true . This means that search() will return false if val is in the tree as an internal node , which is almost certainly not what you want.

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