如何使用boost :: shared_ptr构造二叉树

时间:2013-01-02 01:17:05

标签: c++ boost binary-tree shared-ptr

我正在尝试使用boost共享指针来构造二叉树并将节点存储在STL映射中。我采用了一个常见的二叉树示例,并尝试将指针转换为shared_ptr,如下所示。我可能让它变得比需要的更复杂,因为我认为使用模板会是一个好主意。

#include <iostream>
#include <string>
#include <map>
using namespace std;

#include "boost/smart_ptr.hpp"

//typedef boost::shared_ptr<node<int> >  nodePtr;

template <typename T>
class node
{
public:

    T nodeData;
    boost::shared_ptr<node<T> > *left, *right;

    // default constructor. data not initialized
    node() {}

     // initialize the data members
    node (const T& item, 
          boost::shared_ptr<node<T> > lptr = NULL, 
          boost::shared_ptr<node<T> > rptr = NULL) :
          nodeValue(item), left(lptr), right(rptr) {}
};

template <typename T>
void inorderOutput(boost::shared_ptr<node<T> > t)
{
   // the traversal terminates on a empty subtree
   if (t != NULL)
   {
      inorderOutput(t->left);       // descend left
      cout << t->nodeData << ", ";  // output the node
      inorderOutput(t->right);      // descend right
   }
}

int main()
{
    // a few nodes to assemble into a tree
    boost::shared_ptr<node<char> > d( new node<char>('D') );
    boost::shared_ptr<node<char> > e( new node<char>('E') );
    boost::shared_ptr<node<char> > f( new node<char>('F') );
    boost::shared_ptr<node<char> > g( new node<char>('G') );

    boost::shared_ptr<node<char> > b( new node<char>('B',d, e) );
    boost::shared_ptr<node<char> > c( new node<char>('C',f, g) );
    boost::shared_ptr<node<char> > a( new node<char>('A',b, c) );

    // now store the node pointers for later reference
    map<string, boost::shared_ptr<node<char> > > nodeMap;
    nodeMap["D"] = d;
    nodeMap["A"] = a;
    // etc.

    // inorder traversal of nodes  
    cout << "Inorder:      " ;
    inorderOutput(a);
    cout << endl;

    // Test to see if a node has children
    if (( nodeMap["A"]->left != NULL ) || (nodeMap["A"]->right != NULL ) ) 
    {
        cout << "Node A has children!" << endl;
    }

    return 0;
}

我在Visual Studio Express 2010中构建并出现以下错误:

1>  SharedPtr.vcxproj -> c:\users\john\documents\visual studio 2010\Projects\SharedPtr\Debug\SharedPtr1.exe
2>c:\users\john\documents\visual studio 2010\projects\sharedptr\sharedptr2\sharedptr2.cpp(27): error C2440: 'default argument' : cannot convert from 'int' to 'boost::shared_ptr<T>'
2>          with
2>          [
2>              T=node<char>
2>          ]
2>          No constructor could take the source type, or constructor overload resolution was ambiguous
2>c:\users\john\documents\visual studio 2010\projects\sharedptr\sharedptr2\sharedptr2.cpp(49): fatal error C1903: unable to recover from previous error(s); stopping compilation

显然,节点构造函数未正确定义。我一直在旋转我的车轮一段时间,试图找出我做错了什么(可能有几件事!)。另外,假设我可以编译,是节点子项的工作测试吗?以前的实验让我有理由相信这不容易。最后,我在列表顶部注释了typdef,因为我也没有正确使用它。任何人都可以建议一种清理此代码的方法吗?

约翰

2 个答案:

答案 0 :(得分:3)

更改

 node (const T& item, 
      boost::shared_ptr<node<T> > lptr = NULL, 
      boost::shared_ptr<node<T> > rptr = NULL) :
      nodeValue(item), left(lptr), right(rptr) {}
 };

 node (const T& item, 
      const boost::shared_ptr<node<T> >& lptr = boost::shared_ptr<node<T> >(), 
      const boost::shared_ptr<node<T> >& rptr = boost::shared_ptr<node<T> >()) :
      nodeValue(item), left(lptr), right(rptr) {}
 };

并指出,将'left'和'right'设为非指针boost_shared_ptr。

答案 1 :(得分:2)

在大多数实现中,

NULL让我#define d为0,看起来猜测boost::shared_ptr不知道如何处理它。

更改

boost::shared_ptr<node<T> > *left, *right;

boost::shared_ptr<node<T> > left, right;

node (const T& item, 
      boost::shared_ptr<node<T> > lptr = NULL, 
      boost::shared_ptr<node<T> > rptr = NULL) :

node (const T& item, 
      boost::shared_ptr<node<T> > lptr = boost::shared_ptr<node<T> >(), 
      boost::shared_ptr<node<T> > rptr = boost::shared_ptr<node<T> >()) :