在二进制搜索树中插入节点时的堆栈溢出

时间:2019-07-06 03:21:15

标签: c++ data-structures binary-tree binary-search-tree

我是C ++编程的新手,但是我试图创建一个二进制搜索树。

程序似乎可以正常编译,但是它给了我这个错误:

 Unhandled exception at 0x009229B7 in Lab001_CS3.exe: 0xC00000FD: Stack 
 overflow (parameters: 0x00000001, 0x00AD2FBC).

当我尝试运行它时。该代码行上发生错误:

void insert(int value) {
   ...
}

我不确定自己在做什么错,而且我从来没有遇到过这个错误。

代码如下:

#include <iostream>

using namespace std;

//create a node struct
struct node {

    //member variables
    int key;
    node* left;
    node* right;

    //default constructor
    node() {
        key = 0;
        left = NULL;
        right = NULL;
        cout << "a new node is created" << endl;
    }

    //constructor so can create a node in one line
    node(int k) {
        key = k;
        left = NULL;
        right = NULL;
        cout << "a new node is created" << endl;
    }


};

class Tree {
public:
    //root node
    node root;

    //default constructor 
    Tree() {
        root.key = 0;
        root.left = NULL;
        root.right = NULL;


    }

    //constructor to create the root node
    Tree(int data) {

        //set the data to the key
        //set the right and left pointers to null
        root.key = data;
        root.left = NULL;
        root.right = NULL;
    }

    //print the root node
    void printRootNode() {
        cout << "Root Node - Key: " << root.key << endl;
    }

    //insert functions
    void insert(int value) {

        /* If the newNode's key is less than the root key, traverse left 
    */
    if (value < root.key) {

        /* if the left node is NULL */
        if (root.left == NULL) {
            root.left = new node(value);
            cout << "assigned left" << endl;
        }
        else {

            /* if the left node is important */
            insert(value);
            cout << "recurse" << endl;
        }

    }

    if (value > root.key) {

        /* if the right node is NULL */
        if (root.right == NULL) {
            root.right = new node(value);
            cout << "assigned right" << endl;
        }
        else {
            /* if the right node is important */
            insert(value);
            cout << "recurse" << endl;
        }
    }
}


};

//print inorder
void inorder(node* rt) {
//base
if (rt == NULL) {
    return;
}

inorder(rt->left);
cout << " " << rt->key << endl;
inorder(rt->right);
}

int main() {

//create a tree for a root node
Tree t(16);
t.printRootNode();

//create  newNode
node n1(20);
node n2(31);

//insert the new nodes
t.insert(20);
t.insert(31);

//keep the window from closing
system("pause");
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

在您的insert()

if (value < root.key) {

        /* if the left node is NULL */
        if (root.left == NULL) {
            root.left = new node(value);
            cout << "assigned left" << endl;
        }
        else {

            /* if the left node is important */
            insert(value);
            cout << "recurse" << endl;
        }

    }

让我们以这个向左的片段为例,如果root.left!= NULL,则代码将进入else块并永远递归调用insert(value),这会导致堆栈溢出,正确的操作是使当前节点移至root。左,然后递归调用insert(value)。

您也根本不需要节点类,树类可以做所有事情。

再次,这里不是帮助您调试的好地方,您需要自己学习如何做:-)。