|
-
June 26th, 2012, 05:15 AM
#1
I'm trying to build a binary tree with boost::shared_ptr
Hello,
I'm trying this to get the hang of boost::shared_prt and weak_ptr so far I have the following
Code:
#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/lambda/bind.hpp>
#include <stack>
#include <queue>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
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);
};
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;
}
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()
{
cout << "creating a tree" << endl;
root.reset();
}
Tree::~Tree()
{
root.reset();
cout << "deleting a tree" << endl;
}
void Tree::findParent(int n, int& found, boost::shared_ptr<Node>& parent)
{
boost::shared_ptr<Node> q;
q = parent;
while(!q)
{
if(q)
{
cout << "we hit a root" << endl;
return;
}
if(q->nVal < n)
{
parent = q;
q = q->left
}
}
}
class Triplet
{
public:
boost::weak_ptr<Node> ptrNode1;
};
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;
}
My questions are, is the tree class defined correctly? How do I find the parent of a given node? And how does the insert part will work? Thanks.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|