繁体   English   中英

二叉树的示例实现不适用于大量值

[英]Sample Implementation of Binary Trees doesn't work for large number of values

我最近尝试实现一个二叉树,它似乎适用于少于1000个值,但是在那之后最终它给了我堆栈溢出错误

#include<iostream>
#include<math.h>

using namespace std;
struct Node{
    long long int val;
    Node *left;
    Node *right;
};
struct BinaryTree{
    Node *head  = (Node*) malloc(sizeof(Node));
    bool headSet = false;
    Node* findLast(Node *asgn,int val){
        if (val > asgn->val){
            if (asgn->right != NULL)
                asgn  = findLast(asgn->right, val);
            else
                return asgn;
        }
        else{
            if (asgn->left != NULL)
                asgn = findLast(asgn->left, val);
            else
                return asgn;
        }
        return asgn;
    }
    void insert(long long int vals){
        if (headSet){
            Node *asgn = (Node*) malloc(sizeof(Node));
            asgn = findLast(head,vals);
            Node *fix = (Node*)malloc(sizeof(Node));
            fix->val = vals;
            fix->left = NULL;
            fix->right = NULL;
            if (vals > asgn->val){
                asgn->right = fix;
                asgn->left = NULL;
            }
            else{
                asgn->right = NULL;
                asgn->left = fix;
            }
        }
        else{
            head->val = vals;
            head->right = NULL;
            head->left = NULL;
            headSet = true;
        }
    }
};
int main(){
    BinaryTree a;
    for (long long int i = 0; i < 100;i++)
    a.insert(i);

    return 0;
}

例如:-如果我改变

for (long long int i = 0; i < 100;i++)
    a.insert(i);

for (long long int i = 0; i < 10000;i++)
    a.insert(i);

它给了我错误。 我似乎无法理解为什么发生这种情况,堆栈在哪里溢出?

您的堆栈溢出来自您的findLast方法,一旦二叉树变得太大,递归就变得太多,并在某一时刻使调用堆栈溢出。 您应该通过将有关搜索的信息以某种结构存储并动态分配,从而将其转换为非递归方法,以使您的堆栈不会填满。

PS在C ++中使用new而不是mallocdelete来清除分配的内存,您当前正在泄漏内存。

迭代版本:

Node* findLast(Node *asgn,int val){
  while (1)
  {
    if (val > asgn->val) {
      if (asgn->right != NULL)
        asgn = asgn->right;
      else
        return asgn;
    }
    else {
      if (asgn->left != NULL)
        asgn = asgn->left;
      else
        return asgn;
    }
  }
}

很简单,不是吗?

以及insert的更正版本:

void insert(long long int vals){
   if (headSet){
       // Node *asgn = (Node*) malloc(sizeof(Node));   // removed
       Node *asgn = findLast(head,vals);               // line changed slighty
       Node *fix = (Node*)malloc(sizeof(Node));
       fix->val = vals;
       fix->left = NULL;
       fix->right = NULL;

       if (vals > asgn->val){
           asgn->right = fix;
            //asgn->left = NULL;     // removed
       }
       else{
           //asgn->right = NULL;     // removed
           asgn->left = fix;
        }
    }
    else{
        head->val = vals;
        head->right = NULL;
        head->left = NULL;
        headSet = true;
    }
}

这样不是答案。 我只想建议对代码进行更改,以作为对迈克尔·沃尔茨(Michael Walz)更正的原始代码的补充。 我的建议是忽略headSet成员并将head成员初始化为NULL ,并在第一次插入时分配head ,如下所示:

struct BinaryTree{
    Node *head  = (Node *)NULL;
    Node* findLast(Node *asgn,int val){ // Courtesy of Michael Walz
        while (1){
            if (val > asgn->val){
               if (asgn->right != NULL)
                   asgn = asgn->right;
               else
                   return asgn;
            }
            else{
                if (asgn->left != NULL)
                    asgn = asgn->left;
                else
                    return asgn;
            }
        }
    }
    void insert(long long int vals){
        Node *fix = (Node*)malloc(sizeof(Node));
        fix->val = vals;
        fix->left = NULL;
        fix->right = NULL;
        if (head != NULL){
            Node *asgn = findLast(head,vals);
            if (vals > asgn->val){
                asgn->right = fix;
            }
            else{
                asgn->left = fix;
            }
        }
        else{
            head = fix;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM