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

  • SEARCH
  • Home
  • 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 8543075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:13:02+00:00 2026-06-11T12:13:02+00:00

I am trying to figure out how to create a tree in C++. I

  • 0

I am trying to figure out how to create a tree in C++. I have tried debugging this for hours and I thought it was time I got another set of eyes on it. My question is if my treeNodeClass looks correct. Right now I am getting a stack explosion because I’m double removing items from my node. The tree itself will be parsing a simple xml file. Here is my code.


#include "treeNodeClass.h"

TreeNodeClass::TreeNodeClass()
{
  cout << "TREENODECLASS::TREENODECLASS()" << endl;
  attributes.clear();
  children.clear();
  data = "";
  height = 0;
  parent = NULL;
  tag = "";
  cout << "EXIT TREENODECLASS::TREENODECLASS()" << endl;
}

TreeNodeClass::TreeNodeClass(const TreeNodeClass& other)
{
    cout << "TREENODECLASS::TREENODECLASS(const other)" << endl;
    parent = NULL; 
    CopyIntoMe(other); 
    cout << "EXIT TREENODECLASS::TREENODECLASS(const other)" << endl;
}

TreeNodeClass::~TreeNodeClass()
{
      cout << "TREENODECLASS::~TREENODECLASS()" << endl; 
      if(parent)
        delete parent;
      parent = NULL; 
      children.clear(); 
      attributes.clear();
      cout << "EXIT TREENODECLASS::~TREENODECLASS()" << endl;
}

void TreeNodeClass::CreateAttrib(string root, string val)
{
  string attrib = root + "=" + val;
  attributes.push_back(attrib);
}

void TreeNodeClass::CreateTag(string data, string name)
{
  tag = name;
  this->data = data;
}

list<string> TreeNodeClass::ReturnAttrib()
{
  return this->attributes; 
}

string TreeNodeClass::ReturnTag(string tag)
{
  string retTag = "";
  if(this->tag == tag)
    retTag = this->tag;
  return retTag;
}

void TreeNodeClass::AddChildren(TreeNodeClass* c)
{
if(c != NULL)
  children.push_back(c);
}

TreeNodeClass& TreeNodeClass::operator=(const TreeNodeClass& other)
{
cout << "TREENODECLASS& TREENODECLASS OPERATOR = " << endl;
if(&other != this)
{
  if(parent)
    delete parent;

  parent = NULL; 
  attributes.clear(); 
  children.clear(); 
  CopyIntoMe(other); 
}
return *this;
}

void TreeNodeClass::CopyIntoMe(const TreeNodeClass& other)
{
  cout << "Copy into me" << endl;
  tag = other.tag; 
  data = other.data; 
  attributes = other.attributes; 
  children = other.children; 
  parent = new TreeNodeClass; 
  parent = other.parent; 
  height = other.height; 
}


void TreeNodeClass::AddParent(TreeNodeClass* p)
{ 
  if(p)
  {
    parent = new TreeNodeClass;
    parent = p; 
  }
}

std::vector< TreeNodeClass* > TreeNodeClass::ReturnChildren()
{
  return children; 
}


ostream& operator<<(ostream& out, const TreeNodeClass& treeNode)
{
out << "NODE: " << treeNode.tag << " " << treeNode.data << endl;
out << "CHILDREN: " << treeNode.children.size() << endl;
out << "HEIGHT: " << treeNode.height << endl;
out << "Attributes: "; 
for(list<string>::const_iterator iter = treeNode.attributes.begin(); iter != treeNode.attributes.end(); iter++)
{
  out << *iter << " ";  
}
out << endl;
}

void TreeNodeClass::SetHeight(int h)
{
  height = h;
}

/*void function(TreeNodeClass* node)
{
  cout << node << " " << *node << endl; 

}

TreeNodeClass* function2(TreeNodeClass* node)
{

  return node; 
}

int main()
{
  cout << "STARTING PROGRAM" << endl;
  cout << "CREATING A TREE NODE CLASS " << endl;
  TreeNodeClass* newNode;
  TreeNodeClass* tempNode; 

  list<string> attribs; 

  newNode = new TreeNodeClass; 
  tempNode = new TreeNodeClass; 

  newNode->SetHeight(10); 

  cout << *tempNode << " " <<  *newNode << endl;
  tempNode->SetHeight(20); 

  cout << *tempNode << "\n " <<  *newNode << endl;

  cout << "Setting equal " << endl;
  *tempNode = *newNode; 
  cout << *tempNode << " " <<  *newNode << endl;

  tempNode->SetHeight(40); 
  cout << *tempNode << " " <<  *newNode << endl;

  tempNode->AddChildren(newNode); 
  newNode->AddParent(tempNode); 
  cout << *tempNode << "\n " <<  *newNode << endl;

  return 0;
}
*/

And I’m trying to use this code on a simple state machine. I basically set up a vector of lists to return the states. This is what I believe is giving me a majority of my errors. I’ve been staring at this as well for a while, but I’m kind of lost.The machine will create the tree (supposedly). When the state machine finishes (state 10) it returns and the calling function will just make another call to yylex(). Thanks for the help so far!

TreeNodeClass* ProcessTree(TokenT token, vector <list <stateAssoc> >& vecTree, int depth)
   {
int state = 1; //Assume this is a new record.
bool noState = false;
bool update = true;
int dex = 0;
string root, value, data, tag;
TreeNodeClass* treeNode;

treeNode = new TreeNodeClass; //Assume a new node per state machine visit.


while(state != 10)
{
  switch(state)
  {
case 1: dex = 1;
    break;

case 2: state = 3;
    noState = true;
    root = yylval;
    break;

case 3: state = 4;
    noState = true;
    break;

case 4: dex = 3;
    value = yylval;
    //cout << value << endl;
    treeNode->CreateAttrib(root, value);
    break;

case 5: dex = 2;
    data = yylval;
    //cout << 5 << " " << yylval  << " " << token << endl;

    //If its data store as data; if tag its a start tag.
    break;

case 6: dex = 4;
    //cout << token << " " << yylval << endl;
    break;

case 7: state = 9;
    noState = true;
    tag = yylval;
    //cout << tag << token << endl;
    if(data != "" and data != "authors")
      treeNode->CreateTag(data, tag);
    break;

case 8: {
      TreeNodeClass* childNode = new TreeNodeClass;
      childNode = ProcessTree(token, vecTree, depth+1);

      cout << "BEFORE PARENT" << endl;
      childNode->AddParent(treeNode);
      childNode->SetHeight(depth); 
      treeNode->AddChildren(childNode);
      delete childNode;
      childNode = NULL;
    }
    token = TokenT(yylex()); //Get a new token to process.
    dex = 5;
    break;

case 9: state = 10;
    noState = true;
    update = false;
    break;

case 10: noState = true;
    update = false;
    break;

default: cout << "Error " << endl;
    cout << state << endl;
    cin.get();
    break;

  }

  if(!noState)
state = FindMatch(vecTree[dex], token);
  else
noState = false;

  if(update)
token = TokenT(yylex());
  else
update = true;
}
return treeNode;

}

  • 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-06-11T12:13:04+00:00Added an answer on June 11, 2026 at 12:13 pm

    1.A children shouldn`t delete a parent:

    TreeNodeClass::~TreeNodeClass()
    {
          cout << "TREENODECLASS::~TREENODECLASS()" << endl; 
          /* Delete next 2 lines
          if(parent)
            delete parent;
          */
          parent = NULL; 
          children.clear(); 
          attributes.clear();
          cout << "EXIT TREENODECLASS::~TREENODECLASS()" << endl;
    }
    

    2.Containers will not delete a pointer — you should take it in mind always. Easy way to delete, for example:

     for (vector<TreeNodeClass*>::iterator child = children.begin(); child != children.end(); ++child)
         delete *child;
    

    But best way — not use native pointers and use some smart pointers or shared pointers.

    3.Main function do not delete pointers tempNode and newNode.

    4.If you’ll use native pointers, you should recursively create and copy each children. In other way you’ll catch memory leak.

    5.Example of method CopyIntoMe:

    void TreeNodeClass::CopyIntoMe(const TreeNodeClass& other)
    {
      cout << "Copy into me" << endl;
      tag = other.tag; 
      data = other.data; 
      attributes = other.attributes; 
    
      // delete each pointer to Nodes
      foreach (vector<TreeNodeClass*>::iterator child = children.begin(); child != children.end(); ++child)
        delete *child;
      children.clear();
    
      // Copy recursively each child
      foreach (vector<TreeNodeClass*>::iterator child = other.children.begin(); child != other.children.end(); ++child) {
        TreeNodeClass* new_child = new TreeNodeClass;
        new_child->CopyIntoMe(*child);
        children.push_back(new_child);
      }
    
      parent = other.parent; 
      height = other.height; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out how to create a http CRAN-repository. I've tried to
I am trying to figure out how to create 3 divs and have them
I have a view that will look like this: I'm trying to figure out
I was trying to figure out how to create this style used in Google
I'm still trying to figure out how to create a bomb simulation. I have
I have been trying to figure out how to create a treeview which is
I have a problem trying to figure out how to create a query... Let's
I am trying to figure out how to create a temporary table that is
I am trying to figure out how to create an image gallery like the
i am trying to figure out how to create a javascript alert box asking

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.