(2011.08.05)二叉树结点类的声明

来源:互联网 发布:java 线程挂起 编辑:程序博客网 时间:2024/05/25 23:56

  这个二叉树结点类的声明是继承了一个二叉树的结点的抽象数据类型的,然后,其中,可以看到,二叉树结点类的声明,主要还是有七个函数,跟前面的BinNode几乎是一样的,其中私有数据成员有三个,一个是该结点的值,然后另外两个是这个二叉树的左结点和右结点的指针。公有函数有七个,其中有一个是判断是否为最后结点,其它六个是左右结点和数据的读取与写入。
  在这个函数,它的效率不算很高,因为结构性开销比较大,n(2p+d)是它的总空间的话,2p/(2p+d)就是它的开销了。

// Binary tree node abstract class 二叉树结点的ADTtemplate <class Elem> class BinNode{public:// Return the node's element        virtual Elem& val() = 0;// Set the node's elementvirtual void setVal(const Elem&) = 0;// return the node's left childvirtual BinNode* left() const = 0;// Set the node's left childvirtual void setLeft(BinNode*) = 0;// Return the node's right childvirtual BinNode* right() const = 0;// Set the node's right childvirtual void setRight(BinNode*) = 0;// Return true if the node is a leafvirtual bool isLeaf() = 0;};// Binary tree node class 二叉树结点类声明template <class Elem>class BinNodePtr : public BinNode<Elem>{private:Elem it;// The node's valueBinNodePtr* lc;// Pointer to left childBinNodePtr* rc;// Pointer to right childpublic:// Two constructors -- with and without initial vaulesBinNodePtr(){ lc = rc = NULL; }BinNodePtr(Elem e, BinNode* l = NULL, BinNOde* r = NULL){ it = e; lc = l; rc = r;}~BinNodePtr(){}Elem& val (){ return it; }void setVal(const Elem& e){ it = e; }inline BinNode<Elem>* left() const{ return lc;}void setLeft(BinNode<Elem>* b){ lc = (BinNodePtr*)b;}inline BinNode<Elem>* right() const{ return rc;}void setRight(BinNode<Elem>*b){ rc = (BinNodePtr*)b;}bool isLeaf(){ return (lc == NULL) && (rc == NULL);}};