0

ほとんどのコードは、Weiss の「C++ でのデータ構造とアルゴリズム分析」からのものです。CodeBlock(gnu,gcc) でコードを入力した後、コンパイラはエラーを報告しませんでしたが、インスタンスを作成していくつかの関数をテストしようとしました。違う。BST() または insert() に何か問題があるようです。これは、プログラムが実行後すぐにスタックしたためです。誰かがこの問題を解決する方法を見つけるのを手伝ってくれますか? まことにありがとうございます!!!

#include <iostream>
using namespace std;
struct TreeNode
{
    int element;
    TreeNode*left,*right;
    TreeNode(const int&e,TreeNode*le=NULL,TreeNode*rt=NULL):element(e),left(le),right(rt){};
};

class BST
{
public:
    BST(){root=NULL;};
    ~BST(){ makeEmpty(root); };
// use public member function to call private member functions.
    void insert(const int & x){ insert(x, root); }
    void remove(const int & x){ remove(x, root); }
    TreeNode*findMin() const{ return findMin(root); }
    bool contain(const int & x) const{ return contain(x,root); }
    void printNodes() const{ printNodes(root); }

private:
    TreeNode*root;

    void makeEmpty(TreeNode*&);
    TreeNode* findMin(TreeNode*) const;
    void insert(const int &, TreeNode*) const;
    void remove(const int &, TreeNode*) const;
    bool contain(const int &, TreeNode*) const;
    void printNodes(TreeNode*) const;
};

void BST::makeEmpty(TreeNode*&t)
{
    if(t!=NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}
TreeNode* BST::findMin(TreeNode*t) const
{
    if(t->left==NULL)    return t;
    return findMin(t->left);
}
void BST::insert(const int & x, TreeNode* t) const
{
    if(t==NULL) t=new TreeNode(x,NULL,NULL);
    else if(x < t->element)    insert(x,t->left);
    else if(x > t->element)    insert(x,t->right);
    else;   /// duplicate, do nothing
}
void BST::remove(const int & x, TreeNode* t) const
{
    if(t==NULL) return;
    if(t->element > x)    remove(x, t->left);
    else if(t->element < x)  remove(x, t->right);
    else if(t->left!=NULL && t->right!=NULL)
    {
        t->element=findMin(t->right)->element;
        remove(t->element,t->right);
    }
    else
    {
        TreeNode*oldNode=t;
        t=(t->left==NULL)?t->right:t->left;
        delete oldNode;
    }
}
bool BST::contain(const int & x, TreeNode*t) const
{
    if(t==NULL)    return false;
    else if(x<t->element)    return contain(x,t->left);
    else if(x>t->element)    return contain(x,t->right);
    else    return true;
}
void BST::printNodes(TreeNode*t) const
{
    if(t==NULL) return;
    cout<<t->element<<" ";
    printNodes(t->left);
    printNodes(t->right);
    cout<<endl;
};

クラスBSTをテストするために私が書いたコードは次のとおりです。

int main()
{
    BST BinarySearchTree;
    int element,node;
    for(int i=0; i<5; i++)
    {
        cin>>element;
        BinarySearchTree.insert(element);
    }
    BinarySearchTree.printNodes();

    cout<<BinarySearchTree.findMin()->element<<endl;

    cin>>node;
    if(BinarySearchTree.contain(node)){ cout<<"item "<<node<<" is in BST."<<endl; }

    BinarySearchTree.remove(node);
    BinarySearchTree.printNodes();
    return 0;
}
4

4 に答える 4

4

クラスBSTにはメンバー変数が 1 つしかありません。 TreeNode*root. (それで問題ありません)

insertおよびremove関数はおそらくを変更するため、 に関連するものBSTを変更する必要があります。root

const(そして、コンパイラは、これらの関数が同じ理由であってはならないことをすぐに指摘します。)

于 2013-04-25T06:00:35.943 に答える
2
void BST::makeEmpty(TreeNode*&t)
{
    if(t==NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}

このコードは間違っています。t が NULL の場合、t->left および t->right を実行します。これは NULL ポインターの逆参照と呼ばれ、プログラムがすぐにクラッシュする可能性があります。

これは

void BST::makeEmpty(TreeNode*&t)
{
    if (t!=NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}
于 2013-04-25T07:34:02.890 に答える
0

ソリューションをクロスチェックしたい場合は、ここで見つけることができますhttps://siddharthjain094.wordpress.com/2014/07/13/binary-search-tree-implementation-in-ccpp/

于 2014-07-20T14:29:11.417 に答える