C ++ EXC_BAD_ACCESS(二叉树)

时间:2017-11-15 22:44:54

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

我的类分配是创建二叉树,我决定使用节点向量来实现。当我尝试运行我的代码时,我不断收到进程11错误访问错误,我的IDE /调试器(CLion)总是突出显示节点的类声明。代码如下。任何人都可以告诉我为什么我一直收到这个错误?无效的内存访问在哪里发生?

//Genghis Khan
#include <iostream>
#include <vector>
using namespace std;

//Creating node class and initializing tree vector
class node
{
public:
    int key;
    double balance;
    string name;
    friend class binarytree;
    bool visited; //for use in traversal
};

class binarytree
{
public:
vector<node> tree;
int index; //index to iterate through the tree

//Constructor
binarytree()
{
    node sentinel;
    sentinel.key = 0;
    sentinel.balance = 0;
    sentinel.name = "";
    sentinel.visited = false;
    tree[0] = sentinel;
}

//Empty
bool empty()
{
    return(tree.size() == 1);
}

//set temp to root
void set()
{
    index = 1;
}

//create a new node
/*node create()
{
    node *v = new node;
    cout << "Enter name: ";
    getline(cin, v->name);
    cout << "Enter balance: ";
    cin >> v->balance;
    cout << "Enter key: ";
    cin >> v->key;
    return(*v);
}*/

//return left child for an index
int leftchild(int a)
{
    return(a*2);
}

//return right child for an index
int rightchild(int a)
{
    return(a*2+1);
}
//whether a node is a leaf
bool isleaf(int a)
{
    //if there are no nodes at tree[a]'s left and right children, a is a leaf
    try
    {
        cout << tree[leftchild(a)].key;
        cout << tree[rightchild(a)].key;
    }
    catch(const std::invalid_argument)
    {
        return true;
    }
    return false;
}

//insert
void insert(node a)
{
    while(!isleaf(index))
    {
        if(empty())
        {
            tree[1] = a; //set root to index 1
        }
        else
        {
            if(a.key < tree[index].key)
            {
                index = index * 2;
                insert(tree[index]);
            }
            else if(a.key > tree[index].key)
            {
                index = index * 2 + 1;
                insert(tree[index]);
            }
        }
    }
    tree[index] = a;
}

//visit
void visit(node a)
{
    cout << a.name << endl;
    cout << a.balance << endl;
    cout << a.key << endl;
}

//inorder
void inorder(int a) //will only pass root to this function
{
    if(!isleaf(a))
    {
        inorder(leftchild(a));
        visit(tree[a]);
        inorder(rightchild(a));
    }
}

};

int main()
{
    int n;
    cout << "How many people would you like to enter: ";
    cin >> n;

    binarytree b;
    node *v = new node;
    for(int i = 0; i < n; i++)
    {
        cout << "Enter name: ";
        getline(cin, v->name);
        cout << "Enter balance: ";
        cin >> v->balance;
        cout << "Enter key: ";
        cin >> v->key;
        b.set();
        b.insert(*v);
    }

    for(int j = 0; j < b.tree.size(); j++)
    {
        cout << b.tree[j].key << endl;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码中有多个地方,您尝试通过std::vector::operator[]访问不存在的vector元素,这些元素永远不会引发异常,但如果您尝试重新尝试,则可能会崩溃访问越界元素。首先,您可以使用std::vector::at(),这类似但会抛出异常(而非崩溃),为您提供更多信息。

您似乎缺乏对std::vector如何工作以及如何使用的基本理解。例如,在binarytree的构造函数中,您应该代替

tree[0] = sentinel;

tree.push_back(sentinel);