首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

问一个二叉树的 赋值函数 实现,该怎么解决

2012-02-24 
问一个二叉树的 赋值函数 实现templatetypenameTclassBinaryTreetemplatetypenameTclassBinNode{temp

问一个二叉树的 赋值函数 实现
template   <typename   T>   class   BinaryTree;
template   <typename   T>   class   BinNode   {
template   <typename   T>   friend   class   BinaryTree;
public:
        ....
private:
        T   data;
        BinNode <T>   *leftChild;
        BinNode <T>   *rightChild;
};

template   <typename   T>   class   BinaryTree   {
public:
                  BinaryTree(void)   {       //   default   constructor
        this-> root   =   NULL;
}

BinaryTree(char   endflag   )   {       //   constructor,   input   level   sequence
        T   *a   =   new   T[treeDefaultSize];
        T   data;
        int   k   =   0;
        cout   < <   "Input   char   data   to   construct   a   binaryTree( '@ '   stands   for   NULL   node).Ending   with   '# '.   "   < <   endl;
        cin   > >   data;
        while   (k   <   treeDefaultSize   &&   data   !=   endflag)   {
                                  a[k++]   =   data;
                cin   > >   data;
        }

        if   (k   <   treeDefaultSize)   {
                                this-> root   =   createBinaryTree(a,   0,   k);
              delete   []a;
              a   =   NULL;
        }
        else   {
              delete   []a;
              a   =   NULL;
              cerr   < <   "Node   numbers   exceed   maxSize "   < <   endl;
              exit(1);
          }
}

BinaryTree(T   data)   {         //   constructor
          this-> root   =   new   BinNode <T> (data);
}

                  BinaryTree(const   BinaryTree <T>   &   otherTree)   {   //copy   constructor
                            ..........
                  }

                  BinaryTree   &   operator   =   (BinaryTree <T>   &otherTree)   {         //   =


if(this   !=   &otherTree)   {
        destroy(this-> root);       //??????
        this-> root   =   copyBinaryTree(otherTree.getRoot());
}

return   *this;
}
               
                BinNode <T> *   createBinaryTree(T   *a,   int   i,   int   n)   {
if   (i   <   n)   {
BinNode <T>   *p   =   NULL;
if   (a[i]   ==   '@ ')   {
p   =   NULL;
}
else   {
p   =   new   BinNode <T> (a[i]);
p-> setLeftChild(createBinaryTree(a,   2   *i   +   1,   n));
p-> setRightChild(createBinaryTree(a,   2   *   i   +   2,   n));
}

return   p;
}
else   {

return   NULL;
}

}
          ......
private:
          void   destroy(BinNode <T>   *p)   {
                  if   (p   !=   NULL)   {
        destroy(p-> getLeftChild());
        destroy(p-> getRightChild());
        delete   p;
}
        }

        BinNode <T>   *root;
};

问题就出在语句   //??????   处,我想先删除this所指的树,然后在赋值。问题是:
        this所指的二叉树对象我无法让叶子的左右孩子为空,所以执行到那句就指针错误。如果注释掉,就不会有错误了,这样有memory   leak。
        谁给个解决办法被,谢了!

[解决办法]
给类添加默认构造函数:
template <typename T> class BinNode {
public:
BinNode(){leftChild=rightChild=NULL;}
~BinNode(){delete leftChild;delete rightChild;}
private:
T data;
BinNode <T> *leftChild;
BinNode <T> *rightChild;
};

热点排行