Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4081104
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:01:03+00:00 2026-05-20T18:01:03+00:00

#include <iostream> using namespace std; #define YES 1 #define NO 0 class tree {

  • 0
#include <iostream>
using namespace std;
#define YES 1
#define NO 0

class tree
{
    private:

    public:
        struct leaf
        {
            int data;
            leaf *l;
            leaf *r;
        };
        struct leaf *p;
        tree();
        ~tree();
        void destruct(leaf *q);
        tree(tree& a);
        void add(int n);
        void transverse();
        void in(leaf *q);
        void pre(leaf *q);
        void post(leaf *q);
        leaf*  createBST(int *preOrder, int* inOrder, int len);     
};      
tree::tree()
{
    p=NULL;
}
tree::~tree()
{
    destruct(p);
}
void tree::destruct(leaf *q)
{

}

void tree::transverse()
{
    int c;
    cout<<"\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
    cin>>c;
    switch(c)
    {
        case 1:
            in(p);
            break;

        case 2:
            pre(p);
            break;

        case 3:
            post(p);
            break;
    }
}
void tree::in(leaf *q)
{
    if(q!=NULL)
    {
        in(q->l);
        cout<<"\t"<<q->data<<endl;
        in(q->r);
    }

}
void tree::pre(leaf *q)
{
    if(q!=NULL)
    {
        cout<<"\t"<<q->data<<endl;
        pre(q->l);
        pre(q->r);
    }

}
void tree::post(leaf *q)
{
    if(q!=NULL)
    {
        post(q->l);
        post(q->r);
        cout<<"\t"<<q->data<<endl;
    }

}



tree::leaf* tree::createBST(int *preOrder, int* inOrder, int len)
{
    int i;
    tree::leaf *bst = new tree::leaf;
//  tree bst;
//  if(len < 0)
//      {//bst = NULL;
//      return bst;}

    bst->data = *preOrder;
    for(i = 0; i < len; i++)
        if(*(inOrder + i) == *preOrder)
        break;
    if(i>=0)    
        bst->l = createBST(preOrder + 1, inOrder, i);
    if((len-i-1) >=0)
        bst->r = createBST(preOrder + i +1, inOrder + i + 1, len-i-1);
    return bst;

}

int main()
{


    tree bst;
    int pre_data[] = {20,8,4,12,10,14,22};
    int in_data[] = {4,8,10,12,14,20,22};
    bst.p = bst.createBST(pre_data, in_data, 7);
    bst.transverse();

    return 0;
}

The main problem is in function

  tree::leaf* tree::createBST(int *preOrder, int* inOrder, int len)

Note: I’ve posted two questions about this. Because I modified my code a lot I started a fresh post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:01:04+00:00Added an answer on May 20, 2026 at 6:01 pm

    I believe the error is that you have no terminating condition for your recursion. Consider the case where len == 0, you will execute this line of code:

    bst->l = createBST(preOrder + 1, inOrder, i);
    

    This will pass in a length of 0 and the same thing will happen again. This is infinite recursion, and will cause a segmentation fault.

    I think your problem is solved by adding this to beginning of createBST:

    if(len == 0)
        return NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; #define YES 1 #define NO 0 class tree {
#include<iostream> using namespace std; struct sample { int data[3][2]; }; struct sample* function() {
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; // define the general compare template template <class T> int
#ifndef MIGRATINGUSER_H #define MIGRATINGUSER_H #include <iostream> using namespace std; class MigratingUser { public: void
I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
#include iostream #include string using namespace std; #define AA(bb) \ string(::##bb); int main (int
I have this code: #include <iostream> using namespace std; class FooA { public: virtual
#include <iostream> using namespace std; template <typename T> class test { T y; public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.