简体   繁体   English

9147 分段错误:11 从中序和前序遍历构造二叉树

[英]9147 Segmentation Fault : 11 Construct Binary tree from inorder and preorder traversals

Given inorder and preorder traversal of a Binary Tree, construct the Binary Tree.给定二叉树的中序和前序遍历,构造二叉树。

While constructing a tree from inorder and preorder traversal, i think i have written it properly but why is it giving a segmentatation fault.在从中序和前序遍历构建树时,我想我已经正确编写了它,但是为什么它会给出分段错误。 Please help.请帮忙。 below is my code for the same:下面是我的代码:

#include<bits/stdc++.h>
using namespace std;

class Node {
 public :
 int data;
 Node* left;
 Node* right;

 Node(int data)
 {
    this->data = data;
    left = right = NULL;
 }
};

void Inorder(Node* root)
{
  if (root == NULL)
  {
    return;
  }

  Inorder(root->left);
  cout << root->data << " ";
  Inorder(root->right);
}

 Node* buildTreeHelper( int inorder[], int preorder[], int InS, int InE, int PreS, int PreE)
 {

  if (InS > InE)
  {
     return NULL;
  }

 int rootData = preorder[PreS];

 int lIns = InS;
 int rootIndex = -1;
 for (int i = InS ; i < InE ; i++)
  {
     if (inorder[i] == rootData)
       {
         rootIndex = i;
         break;
       }
  }
    int lIne = rootIndex - 1;
    int lPres = PreS + 1;
    int lPree = lIne - lIns + lPres;
    int rIns = rootIndex + 1;
    int rIne = InE;
    int rPres = lPree  + 1;
    int rPree = PreE;


   Node* root = new Node(rootData);
   root->left = buildTreeHelper(inorder, preorder, lIns, lIne, lPres, lPree);
   root->right = buildTreeHelper(inorder, preorder, rIns, rIne, rPres, rPree);

    return root;
}

Node* buildTree(int inorder[], int preorder[], int size)
{
    return buildTreeHelper(inorder, preorder, 0, size - 1, 0, size - 1);
}
int main()
{

    int inorder[] = {4, 2, 5, 1, 8, 6, 9, 3, 7};
    int preorder[] = {1, 2, 4, 5, 3, 6, 8, 9, 7};

   Node* root = buildTree(inorder, preorder, 9);
    Inorder(root);
    delete root;
    return 0;
}

I am unable to identify what's the error, please help me out of this.我无法确定是什么错误,请帮我解决这个问题。

It seems to me that at least one of your problems is that in the loop to find the root index in the in-order list of data, you don't actually check the last element (ie it should be i <= InE .)在我看来,至少您的一个问题是在循环中查找数据的有序列表中的根索引,您实际上并没有检查最后一个元素(即它应该是i <= InE 。)

Also, I can't say that your lPree calc is correct, because the names you have chosen are so unhelpful that I don't even want to read that line.另外,我不能说你的lPree calc 是正确的,因为你选择的名字太无用了,我什至不想读那行。

And, please don't use #include <bits/stdc++.h> .而且,请不要使用#include <bits/stdc++.h> I don't know which compiler/IDE is it that encourages use of this particular header, but I've seen it in other (beginner) people's code and in my opinion, it's no good.我不知道是哪个编译器/IDE 鼓励使用这个特定的 header,但我在其他(初学者)人的代码中看到了它,在我看来,这不好。 Please just use standard headers.请只使用标准标题。 In this case, that would be iostream (maybe with a using std::cout; thrown in.)在这种情况下,那将是iostream (可能使用using std::cout;抛出。)

By the way, at the end of the program, you are only freeing the single root node.顺便说一句,在程序结束时,您只释放了单个根节点。 That is obviously insufficient and incorrect.这显然是不够的和不正确的。 You have to traverse and free the whole tree (you could do that from the Node destructor, but I wouldn't recommend a recursive call in a destructor. You might be OK in this simple program.)您必须遍历并释放整个树(您可以从Node析构函数中执行此操作,但我不建议在析构函数中进行递归调用。在这个简单的程序中您可能没问题。)

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

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