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 8156289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:54:21+00:00 2026-06-06T16:54:21+00:00

I have created this simple program to learn shared_ptr using namespace std; #define Yes

  • 0

I have created this simple program to learn shared_ptr

using namespace std;
 #define Yes 1
 #define No 0

class Node
{
public:
boost::shared_ptr<Node> left;
boost::shared_ptr<Node> rigth;
int nVal;
Node();
Node(int);
~Node();
int getVal(void);
void setVal(int);

 };

Methods

Node::Node()
{
cout << "creating node empty" << endl;
nVal = 0;
left.reset();
rigth.reset();

}

Node::~Node() 
{
cout << "entering destructor"  << nVal << endl;
}

Node::Node(int n)
{
cout << "creating node with value" << n << endl;
nVal = n;
left.reset();
rigth.reset();
}

int Node::getVal(void)
{
cout << "returning value" << endl;
return this->nVal;
}

void Node::setVal(int n)
{
cout << "setting value" << endl;
nVal = n;
}

Tree

  class Tree 
   {
  public:
 boost::shared_ptr<Node> root;
  Tree();
  ~Tree();
  void findParent(int n, int &found, boost::shared_ptr<Node> &parent);
  void add(int n);
  void post(boost::weak_ptr<Node> q);
  void del(int n);

  };

Tree::Tree()
{
root.reset();
}

 Tree::~Tree()
{
cout << "deleting tree" << endl;
}

Tree find parent

void Tree::findParent(int n, int& found, boost::shared_ptr<Node> &parent)
{
   boost::shared_ptr<Node> q;
  found = No;

 cout << "looking parent of" << n << endl;
 if(parent.use_count() == 0)
 {
  cout << "not found" << endl;
  return;
  }

q=parent;

 while (!q.use_count())
{
  if( q->nVal == n)
  {
      cout << "found" << endl;
      found = Yes;
      return;

  }

  if (q->rigth->nVal)
  {
      cout << "looking to the rigth" << endl;
      parent = q;
      q = q->left;
  }
  else
  {
      cout << "looking to the left" << endl;
      parent = q;
      q = q->rigth;
  }
 }
}

Tree add

void Tree::add(int n)
 {
 int found;

boost::shared_ptr<Node> parent;
findParent(n, found,parent);
if(found == Yes)
{
    cout << "no such node exist" << endl;
}

else
{

    boost::shared_ptr<Node> t(new Node(n));  
    t->rigth.reset();
    t->left.reset();

    if (parent.get()== 0)
    {
        parent = t;
    }
    else
    {
        parent->nVal > n ? parent->left = t : parent->rigth = t;
    }
}
}

Main:

int THREADS_HOW_MANY = 0;

int main()
{
Tree bt;
bt.add(10);
bt.add(4);
bt.add(12);
bt.add(2);
bt.add(8);
bt.add(15);
bt.add(15);



return 0;
}

Now the q is, why is is not working, why is giving this output:

  empty constructor only the root is reset
  looking parent of10
  not found
  creating node with value10
  entering destructor10
  looking parent of4
  not found
  creating node with value4
  entering destructor4
  looking parent of12
  not found
  creating node with value12
  entering destructor12
 looking parent of2
 not found
  creating node with value2
 entering destructor2

Fixed it now is diff it seem that the nodes are just being created and deleted not added to a tree why??

  • 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-06T16:54:22+00:00Added an answer on June 6, 2026 at 4:54 pm

    This is your code:

    // from Tree::add
    boost::shared_ptr t(new Node(n));
    boost::shared_ptr parent(new Node(n));
    findParent(n, found,parent);
    
    // from tree::findParent
    boost::shared_ptr q(new Node(n));
    

    So, the program creates node for every value 3 times, as requested. shared_ptr ensures, that every node is properly destructed, so Node destructor is called 3 times as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created this simple program in Eclipse: import java.util.*; import java.io.*; public class prob1
I have created my web socket server and client using this simple tutorial here
Could someone help me on this, I have created simple web services using axis2
I'm building simple phonebook. Thus a have created a class Person: public class Person
I have created this program that takes two inputs and prints them out (simple,
I am trying to run a simple RPC program which I have created using
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created a simple program in C++ with Code::Blocks. If I run it
I have created a simple class to filter out data from a data stream.
I have created an applet program using Eclipse IDE. Now im creating .html file

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.