我的二叉树程序在输出时崩溃

时间:2016-01-02 12:00:48

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

我正在尝试编写二叉树代码,但它崩溃了。 它保持打印出相同的数字:左节点(较小的数字)和根节点。它以递归方式重复该程序,直到它崩溃。 我知道我的代码有一些其他的错误,但这是要解决的主要问题所以这是我的代码:

    var customerName = $('#txtName').val();

      $.ajax(
     {
      url:"http://myip:8060/Api/Test", 
      method:"POST",
      data: { custName: customerName },
      success:function(data)
      {
       console.log(data);
      }
      error:function(e)
      {
       console.log(e);
      }
    })

崩溃后它返回我:

#include <iostream>

using namespace std;

struct node {
    int value = 0;
    node* left = NULL;
    node* right = NULL;
};

node root;
void add(int x, node* curr)
{

    if (x < (*curr).value) {

        if ((*curr).left == NULL) {
            node next;
            next.value = x;
            (*curr).left = &next;
        }
        else {
            add(x, (*curr).left);
        }

        if (x > (*curr).value) {
            if ((*curr).right == NULL) {
                node next;
                next.value = x;
                (*curr).right = &next;
            }
            else {
                add(x, (*curr).right);
            }
        }
    }
}

void out(node ro)
{
    node lefta;
    node righta;
    if (ro.left != NULL) {
        lefta = *(ro.left);
        cout << lefta.value;
        out(lefta);
    }
    if (ro.right != NULL) {
        righta = *(ro.right);
        cout << " " << righta.value << endl;
        out(righta);
    }
}

int main()
{

    int n;
    cin >> n;
    int x;
    cin >> x;
    root.value = x;
    node* curr;
    curr = &root;
    for (int i = 1; i < n; i++) {
        cin >> x;
        add(x, curr);
    }

    out(root);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

如果您想在列表中插入新节点,则必须分配它。 当函数返回时,堆栈上的局部变量超出范围。 无论如何,你必须调整你的函数add的双链表如下:

void add(int x, node *curr)
{
    // while x less than curr->value step left 
    while ( curr->left != NULL && x < curr->value )
        curr = curr->left;

    // while x greater than curr->next->value step right 
    while ( curr->right != NULL && x > curr->right->value )
        curr = curr->right;

    // x is less than curr->right->value (curr->right may be NULL)
    // either x is greater curr->value or curr->left == NULL 

    node *newNode = new node; // allcat new node
    newNode->left = newNode->right = NULL; // <- this schould be done by a constructor of node
    newNode->value = x;

    if ( x < curr->value )
    {
        // curr->left == NULL => new node is new start of list
        curr->left = newNode;
        newNode->right = curr;
    }
    else if ( curr->right == NULL )
    {
        // new node is new end of list
        curr->right = newNode;
        newNode->left = curr;
    }
    else
    {
        // new node someweher in the list
        node *rightNode = curr->right;
        curr->right = newNode;
        newNode->right = rightNode;
        newNode->left = curr;
        rightNode->left = newNode;
    }
}

请注意,如果您销毁了new,则必须使用delete分配的所有节点。

如果您想从头到尾打印列表,则不需要递归函数,也不要复制节点。使用指针:

void out(const node *ro)
{
    if ( ro == NULL )
        return;

    while ( ro->left != NULL )
        ro = ro->left;

    while ( ro != NULL )
    {
        cout << ro->value;
        ro = ro->right;
    }
}

...

out(&root);