首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c++中创建两种不同的二叉树时的分割故障

在c++中创建两种不同的二叉树时的分割故障
EN

Stack Overflow用户
提问于 2019-11-29 15:14:17
回答 1查看 74关注 0票数 1
代码语言:javascript
复制
#include<iostream>
using namespace std;

struct tree
{
  int data;
  struct tree *leftchild;
  struct tree *rightchild;
};

typedef struct tree* Binary_tree;

//this function Creates a new node and assigns memory to it 
Binary_tree CreateNode(int value)
{
  Binary_tree temp;
  temp = new struct tree;
  temp->data = value;
  temp->leftchild = NULL;
  temp->rightchild = NULL;
  return temp;
}


//this function links nodes with each other
Binary_tree AddNode(Binary_tree head,int value)
{
  Binary_tree temp = head;
  if(head == NULL)
  {
    Binary_tree newNode = CreateNode(value);
    head = newNode;
  }
  else if(head->leftchild == NULL)
  {
    Binary_tree newNode = CreateNode(value);
    temp->leftchild = newNode;
  }
  else
  {
    Binary_tree newNode = CreateNode(value);
    temp->rightchild = newNode;
  }
  return head;
}


void PrintTree(Binary_tree head)
{
  if(head == NULL)
  exit;
  else
  {
    Binary_tree temp = head;
    cout<<temp->data<<endl<<temp->leftchild<<"\t\t"<<temp<<endl<<temp->rightchild<<endl;
    PrintTree(temp->leftchild);
    PrintTree(temp->rightchild);
  }

}


int main()
{
  Binary_tree head,second;
  for(int i=1;i<12;i++)
  {
    head = AddNode(head,i);
    second = AddNode(second,i+2);
  }
  PrintTree(head);
  PrintTree(second);

}

我得到的错误是分割错误。只有当我创造了两棵树,即头树和二树时,它才会出现。当我只处理头树时,它工作得很好,我不知道是什么导致了错误。

我认为分割错误是因为将这些树存储在内存中,但是树是非线性的,所以这不应该是一个问题,有人能说出问题是什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-29 16:05:25

这是您用c++重写的程序。

我使用了最佳实践,例如使用RAII保持状态,而不是手动修改指针和new/delete,安排在默认情况下初始化对象,等等。

一个功能上的区别是,我的树是不平衡的二进制搜索树。你的树在3个节点后停止生长。

您将注意到,使用这些技术,代码实际上更简单。

代码语言:javascript
复制
#include <iostream>
#include <memory>

struct tree;
using Tree = std::unique_ptr<tree>;

struct tree
{
  int data = 0;
  Tree left;
  Tree right;
};

//this function Creates a new node and assigns memory to it 
Tree CreateNode(int value)
{
  Tree retval = std::make_unique<tree>();
  retval->data = value;
  return retval;
}

//this function links nodes with each other
Tree AddNode(Tree head, int value)
{
  if(!head)
  {
    return CreateNode(value);
  }

  if (head->data <= value)
  {
    head->right = AddNode( std::move(head->right), value );
    return head;
  }

  head->left = AddNode( std::move(head->left), value );

  return head;
}


void PrintTree(Tree const& head, int indent = 0)
{
  if(!head)
    return;

  auto do_indent = [indent]{
      for (int i = 0; i < indent; ++i)
        std::cout << "\t";
  };
  do_indent();
  std::cout << head->data << std::endl;
  //do_indent();
  //std::cout << head->left.get() << "\t\t" << head.get() << std::endl;
  //do_indent();
  //std::cout << head->right.get() << std::endl;

  PrintTree(head->left, indent+1);
  PrintTree(head->right, indent+1);
}


int main()
{
  Tree head,second;
  for(int i=1;i<12;i++)
  {
    head = AddNode(std::move(head),i);
    second = AddNode(std::move(second),6+(i+2)%6);
  }
  PrintTree(head);
  PrintTree(second);
}

代码的直接问题是它使用未初始化的内存;读取未初始化的内存可能会崩溃,也可能不会崩溃,这取决于运气。它也泄露了,实际上并没有建造你想要的树。

打印这些树的一个有趣的方法是:

代码语言:javascript
复制
void PrintTree(Tree const& head, int indent = 0)
{
  auto do_indent = [indent]{
      for (int i = 0; i < indent; ++i)
        std::cout << "\t";
  };

  if(!head)
  {
    do_indent();
    std::cout << "." << std::endl;
    return;
  }

  PrintTree(head->left, indent+1);

  do_indent();
  std::cout << head->data << std::endl;

  PrintTree(head->right, indent+1);
}

我们先打印左边,然后是头,然后是右边。这看起来更像一棵树,而对于空树则是一个.

这将生成输出:

代码语言:javascript
复制
    .
1
        .
    2
            .
        3
                .
            4
                    .
                5
                        .
                    6
                            .
                        7
                                .
                            8
                                    .
                                9
                                        .
                                    10
                                            .
                                        11
                                            .
        .
    6
                .
            6
                .
        7
                    .
                7
                    .
            8
                .
9
            .
        9
            .
    10
                .
            10
                .
        11
                .
            11
                .

实例化

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59107308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档