C ++中的二叉树段错误

时间:2016-01-18 03:01:07

标签: c++ binary-tree

我正在尝试编写二叉树搜索/插入/打印(BFS)函数。 它编译得很好,但是当我尝试插入新节点时,我不断收到段错误。因此,我无法测试打印功能。任何建议,小伙子? 顺便说一下,我在单独的Binarytree.cpp文件中对函数进行了编码。

#ifndef BINARYTREE_H
#define BINARYTREE_H

#include <iostream>
#include <vector>

using namespace std;

typedef struct Node{
    int key;
    struct Node* leftNode;
    struct Node* rightNode;
    //C++에서는 struct의 constructor 가능
    Node(int _key){key = _key;};
} Node;

class BinaryTree
{
    private:
        static Node* rootNode;
    public:
        static Node* search(int searchkey){
            Node* curNode = rootNode;
            while (curNode){
                if (searchkey == curNode->key){
                    break;
                } else if (searchkey < curNode->key){
                    curNode = curNode->leftNode;
                } else {
                    curNode = curNode->rightNode;
                }
            } 
            return curNode;
        }

        static bool insert(int insertkey){
            Node* curNode = search(insertkey);
            if (insertkey == search(insertkey)->key){
                cout << "Key already exits" << endl;
                return false;
            }
            if (!rootNode){
                rootNode = new Node(insertkey);
                return true;
            } else {
                Node* newNode = new Node(insertkey);
                newNode = search(insertkey);
                return true;
            }
        }

        static void print(){
            //Node* rootNode = getRoot();
            vector<Node*> v;

            if (rootNode == NULL){
                cout << "Binary tree is empty." << endl;
                return;
            } else {
                v.push_back(rootNode);
                cout << rootNode->key << endl;
            }
            while (!v.empty()){
                Node* temp = v.front();
                v.erase(v.begin());

                if (temp->leftNode){
                    v.push_back(temp->leftNode);
                    cout << temp->leftNode->key << endl;
                }

                if (temp->rightNode){
                    v.push_back(temp->rightNode);
                    cout << temp->rightNode->key << endl;
                }
            } 
        }

};

Node* BinaryTree::rootNode = NULL;

#endif

1 个答案:

答案 0 :(得分:1)

此处insert

Node* curNode = search(insertkey);

第一次致电insert时,curNode将为NULL。在以下情况下:

if (insertkey == search(insertkey)->key){
        cout << "Key already exits" << endl;
        return false;
}

你试图通过NULL来推测search(insertkey)->key指针。这会导致seg故障。

以下是我机器上gdb中的backtrace输出:

Program received signal SIGSEGV, Segmentation fault.
0x00000000004009a3 in BinaryTree::insert (insertkey=1) at binarytree.h:38
38              if (insertkey == search(insertkey)->key){
(gdb) backtrace
#0  0x00000000004009a3 in BinaryTree::insert (insertkey=1) at binarytree.h:38

您可以通过快速方式解决此问题,检查search的回复是NULL是否为public void threePlus(View view){ if(view == findViewById(R.id.team_a_score)){ scoreTeamA += 3; displayForTeamA(scoreTeamA);} else{ scoreTeamB += 3; displayForTeamB(scoreTeamB); } } ,然后转到其他情况。