C ++创建二进制搜索树:EXC_BAD_ACCESS错误。坏算法或编码错误?

时间:2017-11-14 05:29:53

标签: c++ binary-search-tree exc-bad-access

问题:我一直收到exc_bad_access(进程代码11)错误。这是由于错误的算法还是仅仅是编码错误?任何人都可以帮我修复它吗?

我的类分配是创建一个二叉搜索树,其节点可以存储名称,余额和密钥。使用密钥组织和搜索节点。这个树应该支持插入,按顺序遍历和基于密钥的搜索(我还没有构建这个功能)。我还包括其他几个功能,以方便构建这些功能。如果重要的话,我在OSX High Sierra上使用CLion。另外,我在第一次提示输入节点信息时得到错误,错误似乎与输入本身无关。

//Genghis Khan
#include <iostream>
#include <vector>
using namespace std;
class node
{
public:
    int key;
    string name;
    double balance;
    node *leftptr;
    node *rightptr;
    friend class tree;
};
class tree
{
public:
    node *root, *temp, *v;

    //Constructor
    tree()
    {
        root = NULL;
        temp = root;
    }

    bool empty()
    {
        return(root == NULL);
    }


    bool isleaf(node *x)
    {
        return((x->leftptr == NULL) && (x->rightptr == NULL));
    }  

    void inorder(node *temp)
    {
        if(~isleaf(temp)) 
        {
            inorder(temp->leftptr);
            cout << "Name: " << temp->name << " " << "Balance: " << 
    temp->balance << " " << "Key: " << temp->key;
            inorder(temp->rightptr);
        }
    }

    node* createnode()
    {
        v = new node;
        cout << "Enter name (string): " << endl;
        getline(cin, v->name); 
        cout << "Enter key (integer): " << endl;
        cin >> v->key;
        cout << "Enter balance (double): " << endl;
        cin >> v->balance;
        return(v);
    }

    void set()
    {
        temp = root;
    }

    void insert(node *v)
    {
        while(~isleaf(temp)) 
        {
            if((v->key < temp->key))
            {
                temp = temp->leftptr;
                insert(v);
            }
            else if(v->key > temp->key)
            {
                temp = temp->rightptr;
                insert(v);
            }
        }
    temp->key = v->key;
    temp->balance = v->balance;
    temp->name = v->name;
    }

};

int main()
{
    int n;
    cout << "Enter number of people: ";
    cin >> n;

    //Creating instance of tree, inserting all data into tree
    tree b;
    for(int i = 0; i < n; i++)
    {
        b.set();
        node *a = b.createnode();
        b.insert(a);
    }

    //inorder part
    b.set();
    b.inorder(b.temp);

}

函数是(伪代码):

1. function isleaf(x): return(x's left pointer and x's right pointer are both NULL)

2. function set(): set temp to root //temp will be reset every time an insertion, traversal, or search occurs

3. function createnode(): 

   v is  a new node

   get all the fields for v 

   return v

4. function insert(v)

   while(not isleaf(temp)):  
   -if(v's key < temp's key)
  temp = temp's left pointer (to the lower value child node)    
  insert(node *v)

   -if(v's key > temp's key)  
  temp = temp's right pointer (to the higher value child node)   
  insert(node *v)  
  end while  
  duplicate v's data to temp, now that temp is a leaf

5. function inorder(temp):  
   if(not isleaf(temp):  
   inorder(temp's left pointer)  
   output all info in temp node  
   inorder(temp's right pointer)

主算法

表示要输入的节点数:
 1.设定  2. node * a = createnode
 3.插入(a)

更新

错误似乎来自'if((v-&gt; key&lt; temp-&gt; key))'行。

1 个答案:

答案 0 :(得分:0)

EXC_BAD_ACCESS只表示您正在尝试访问无效内存。通过快速简介,您的函数isleaf不会检查x是否为null。

可能有其他错误,您可以自行调试并找到它。