簡體   English   中英

c ++-使用BST來實現策略表錯誤

[英]c++-Use BST to implement stactic table error

我用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錯誤,請在此處輸入圖片描述

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

如果elem大於p->get_key()允許將p推進到NULL節點而無需測試NULL

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM