c ++-使用BST来实现策略表错误

时间:2018-11-15 03:33:24

标签: c++ binary-search-tree

我用BST实现了一个静态表。但是我不知道查找帮助功能有什么问题。当搜索数据不在表中并且数据大于表中的数据时,exe将终止。我认为有关指针的事。

标题:

#include <iostream>
#include <cstddef>
using namespace std;

class Node{
    private:
    int key;
    Node* left;
    Node* right;
    public:
    Node(){left = nullptr;right = nullptr;}
    Node(int k,Node* l,Node* r)//构造函数
    {
        key = k;
        left = l;
        right = r;
    }
    ~Node(){};
    inline int get_key(){return key;}
    inline Node* rt(){return right;};
    inline Node* lt(){return left;};
    inline void setLeft(Node* r){left = r;}
    inline void setRight(Node* r){right = r;}
};
class BST:public Node{
    private:
        Node *root;
    public:
        BST(){root = nullptr;}//无参构造函数
        ~BST(){}//析构函数
        Node* get_root(){return this->root;}
        void insert(const int & elem);
        void create(BST&tree,const int n,const int a[]);//构造BST
        Node* inserthelp(Node* r,const int& elem);//插入数据
        int search(const int& elem);//查找
        void clear(){delete []root;}
};

cpp:

#include "static_table.h"

void BST::create(BST& tree,const int n,const int a[])
{
    for(int i = 0;i < n;i ++)
    {
        tree.insert(a[i]);
    }
}

Node* BST::inserthelp(Node* r,const int& elem)
{
    if(r == nullptr)
        return new Node(elem,nullptr,nullptr);
    if(elem < r->get_key())
        r->setLeft(inserthelp(r->lt(),elem));
    else
        r->setRight(inserthelp(r->rt(),elem));
    return r;
}

void BST::insert(const int &elem)
{
    root = inserthelp(root,elem);
}

int BST::search(const int& elem)
{
    int counts = 0;
    Node* p = root;
    while(p)
    {
        if(elem == p->get_key()) {counts++;return counts;}
        if(elem > p->get_key()) {counts++;p = p->rt();}
        if(elem < p->get_key()) {counts++;p = p->lt();}
    }
    return 0;
}

主要:

#include "static_table.h"

using namespace std;

int main()
{
    BST st;
    int m,n;
    cout<<"please input static table's size"<<endl;
    //cout<<"请输入静态表的数据个数:"<<endl;
    cin>>n;
    int a[n];
    cout<<"please input data"<<endl;
    for(int i = 0;i < n;i ++)
    {
        cin>>a[i];
    }
    st.create(st,n,a);
    cout<<"please input the number of data you wanna search"<<endl;
    cin>>m;
    int elem;
    cout<<"please input the data,end it with enter"<<endl;
    for(int i = 0;i < m;i ++)
    {
        cin>>elem;
        if(st.search(elem) != 0) cout<<"find "<<elem<<" "<<"compared counts:"<<st.search(elem)<<endl;
        else cout<<"Not Find!"<<endl;
    }
    return 0;
}

这是exe错误 enter image description here

1 个答案:

答案 0 :(得分:0)

TL; DR

if (elem < p->get_key())

应该是

else if (elem < p->get_key())

说明

search

if (elem > p->get_key())
{
    counts++;
    p = p->rt();
}
if (elem < p->get_key())
{
    counts++;
    p = p->lt();
}

如果p大于NULL,则允许NULL前进到elem节点而无需测试p->get_key()

if (elem > p->get_key()) // true
{
    counts++;
    p = p->rt(); // No right node, so p now NULL
}
if (elem < NULL->get_key()) // UB and almost certainly a crash
{
    counts++;
    p = p->lt();
}