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

模板类继承的小疑点.

2012-06-13 
模板类继承的小问题..为了实现BinarySearchTree我建了两个模板类,一个为节点类(BinaryNodeType),另外一

模板类继承的小问题..
为了实现BinarySearchTree我建了两个模板类,一个为节点类(BinaryNode<Type>),另外一个(BinarySearchTree<Type>)用来实现二叉排序树(节点类被包含在二叉排序树内)的操作。现在问题来了,我想在新定义的AVL树中重用二叉排序树的代码,但是因为要对节点类的数据成员进行更新(BinaryNode<Type>派生出的AVLNode<Type>要增加一个int hight数据成员),我如何才能在新建增、由BinarySearchTree<Type>派生出的AVL_BinarySearchTree<Type>中将使用BinaryNode<Type>类型的地方全部更新为使用AVLNode<Type>类型?

以下为代码:

C/C++ code
#ifndef BINARY_H#define BINARY_Htemplate <typename Type>class BinaryNode{public:    Type data;    BinaryNode *right, *left;    BinaryNode(const Type &initElement, BinaryNode *r = 0, BinaryNode *l = 0)        : right(r), left(l), data(initElement){}};template <typename Type>class BinarySearchTree{public:    typedef BinaryNode<Type> BinaryNode;    BinarySearchTree();    //BinarySearchTree(const BinarySearchTree&);    //~BinarySearchTree();    const Type& findMin() const;    const Type& findMax() const;    bool contains(const Type&);    void insert(const Type&);    void remove(const Type&);    void outPut();    private:        BinaryNode *root;    bool contains(const Type&, BinaryNode*);    void insert(const Type&, BinaryNode*&);    void remove(const Type&, BinaryNode*&);    BinaryNode* findMin(BinaryNode* t) const;    BinaryNode* findMax(BinaryNode*) const;    void InOrder(BinaryNode*) const;    //void makeEmpty(BinaryNode *t);};//#include "BinarySearchTree.cpp"#endif


这个是二叉排序树的实现
C/C++ code
#include "BinarySearchTree.h"template <typename Type>BinarySearchTree<Type>::BinarySearchTree() : root(0){}////////////////////////////////////////////////////////////////////////template <typename Type>typename BinarySearchTree<Type>::BinaryNode* BinarySearchTree<Type>::findMin(BinaryNode* t) const{    if(t == 0)        return 0;//ERROR    if(t->left == 0)        return t;    else        return findMin(t->left);}template <typename Type>typename BinarySearchTree<Type>::BinaryNode* BinarySearchTree<Type>::findMax(BinaryNode* t) const{    if(t == 0)        return 0;//ERROR    if(t->right == 0)        return t;    else        return findMax(t->right);}template <typename Type>const Type& BinarySearchTree<Type>::findMin() const{    return findMin(root)->data;}template <typename Type>const Type& BinarySearchTree<Type>::findMax() const{    return findMax(root)->data;}////////////////////////////////////////////////////////////////////////template <typename Type>bool BinarySearchTree<Type>::contains(const Type& x){    return contains(x, root);}template <typename Type>bool BinarySearchTree<Type>::contains(const Type& x, BinaryNode* t){    if(t == 0)        return false;    else if(x < t->data)        return contains(x, t->left);    else if(x > t->data)        return contains(x, t->right);    else        return true;}////////////////////////////////////////////////////////////////////////template <typename Type>void BinarySearchTree<Type>::insert(const Type& x){    insert(x, root);}template <typename Type>void BinarySearchTree<Type>::insert(const Type& x, BinaryNode*& t){    if(t == 0)        t = new BinaryNode(x, 0, 0);    else if(x < t->data)        insert(x, t->left);    else if(x > t->data)        insert(x, t->right);    else        ;}////////////////////////////////////////////////////////////////////////template <typename Type>void BinarySearchTree<Type>::remove(const Type& x){    remove(x, root);}template <typename Type>void BinarySearchTree<Type>::remove(const Type& x, BinaryNode*& t){    if(t == 0)        return;    else if(x < t->data)        remove(x, t->left);    else if(x > t->data)        remove(x, t->right);    else if(t->left != 0 && t->right != 0)    {        t->data = findMin(t->right)->data;            remove(t->data, t->right);    }    else    {        BinaryNode *old = t;        t = (t->left != 0) ? t->left : t->right;        delete old;    }        }////////////////////////////////////////////////////////////////////////template <typename Type>void BinarySearchTree<Type>::outPut(){    InOrder(root);}template <typename Type>void BinarySearchTree<Type>::InOrder(BinaryNode* t) const{    if(t == 0)        return;    if(t->left != 0)        InOrder(t->left);    cout<<t->data<<" ";    if(t->right != 0)        InOrder(t->right);} 




这个是AVL树
C/C++ code
#ifndef AVL_H#define AVL_H #include "BinarySearchTree.h"template <typename Type>class AVLNode : public BinaryNode<Type>{public:    int hight;    AVLNode() : hight(0){}    };template <typename Type>class AVL_BinarySearchTree : public BinarySearchTree<Type>{public:    typedef AVLNode<Type> BinaryNode;};#endif




谢谢!!!

[解决办法]
考虑指针或引用呗。BinarySearchTree<Type>* 一样可以指向AVLNode<Type>
[解决办法]

template <typename T>
struct bNode{
 T* left;
 T* right;
//...方法都在这里实现

};




//tree就是根节点。
template <typename T>
struct bTree:public bNode<T>{
};


现在
AVLNode可以继承 bNode;

然后avltree 继承avlnode

热点排行