[数据结构]二叉树之二叉链表的类模板实现

该类模板实现了一个二叉树的模板类,采用二叉链表实现。

定义二叉树节点类,采用二叉链表实现。

/////////////////////////
#include 
#include 
#include 
#include 
using namespace std;


template
struct BinTreeNode  //二叉树节点类的定义,使用二叉链表
{
    T data;
    BinTreeNode *leftChild, *rightChild;
    BinTreeNode():leftChild(NULL),rightChild(NULL){}
    BinTreeNode(T x,BinTreeNode *l=NULL,BinTreeNode *r=NULL):data(x),leftChild(l),rightChild(r){}
};

二叉树的模板类实现如下:可进行相应的功能扩展。

接口部分:

template
class BinaryTree//二叉树的模板类
{
public:
    BinaryTree():root(NULL){}
    BinaryTree(char x):root(NULL),RefValue(x){}
    BinaryTree(const BinaryTree& rhs){root=copy(rhs.root);}//copy构造函数
    BinaryTree& operator=(const BinaryTree& rhs);//copy 赋值运算符;析构+copy构造函数  
    ~BinaryTree(){destroy(root);}//析构函数

    bool isEmpty()const{return root!=NULL?false:true;}
    BinTreeNode* leftChild(BinTreeNode* current)const{return current!=NULL?current->leftChild:NULL;}
    BinTreeNode* rightChild(BinTreeNode* current)const{return current!=NULL?current->rightChild:NULL;}
    BinTreeNode* parent(BinTreeNode* current)const{return (root==NULL || current==root)?NULL:parent(root,current);}//寻找其父节点
    BinTreeNode* getRoot()const{return root;}

    void inOrder(void (*visit)(BinTreeNode *p)){inOrder(root,visit);}//中序递归遍历
    void preOrder(void (*visit)(BinTreeNode *p)){preOrder(root,visit);}//前序递归
    void postOrder(void (*visit)(BinTreeNode *p)){postOrder(root,visit);}//后序递归
    void levelOrder(void (*visit)(BinTreeNode *p));//使用队列的层次遍历

    int size()const {return size(root);}//使用后序递归遍历求节点个数
    int height()const {return height(root);}//使用后序递归遍历求二叉树的高度



protected:
    BinTreeNode *root;
    char RefValue;//数据输入停止标志

    void destroy(BinTreeNode* subTree);//递归删除二叉树节点,后序遍历删除
    BinTreeNode* copy(const BinTreeNode *orignode);//copy构造;前序

    BinTreeNode* parent(BinTreeNode* subTree,BinTreeNode* current)const;//返回父节点
    void traverse(BinTreeNode* subTree,ostream& out)const;//按前序方式遍历输出每个节点的值
    void createBinTree(istream& in,BinTreeNode* & subTree);//采用广义表表示的二叉树创建方法

    void inOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p));//中序遍历
    void preOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p));//前序遍历
    void postOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p));//后序遍历

    int size(BinTreeNode *subTree)const;//使用后序递归遍历求节点个数
    int height(BinTreeNode *subTree)const;//使用后序递归遍历求二叉树的高度
    

    friend ostream& operator<< (ostream& out,const BinaryTree& rhs);//add  前序输出二叉树
    friend istream& operator>> (istream& in, BinaryTree& rhs);      //add  采用广义表表示方式创建二叉树

};

相应成员函数的具体实现:


template
void BinaryTree::destroy(BinTreeNode* subTree)
{
    if(subTree!=NULL){
        destroy(subTree->leftChild);
        destroy(subTree->rightChild);
        delete subTree;
    }
}

template
BinTreeNode* BinaryTree::parent(BinTreeNode* subTree,BinTreeNode* current)const
{
    if(subTree==NULL) return NULL;
    if(subTree->leftChild==current || subTree->rightChild==current) return subTree;
    
    BinTreeNode* p;
    if((p=parent(subTree->leftChild,current))!=NULL)
        return p
    else 
        return parent(subTree->rightChild,current);
}

template
void BinaryTree::traverse(BinTreeNode* subTree,ostream& out)const
{
    if(subTree!=NULL){
        out<data<<" ";
        traverse(subTree->leftChild,cout);
        traverse(subTree->rightChild,out);
    }
}

template
void BinaryTree::createBinTree(istream& in,BinTreeNode* & subTree)
{
    stack* > s;
    subTree=NULL;
    BinTreeNode *p,*t;
    unsigned int k;
    T ch;
    in>>ch;//虽然是模板类,但是目前只支持字符型,不然会报错
    while(ch!=RefValue){
        switch(ch){
        case '(': s.push(p);k=1;break;
        case ')': s.pop();break;
        case ',': k=2;break;
        default:
            p=new BinTreeNode(ch);
            if(subTree==NULL)
                subTree=p;
            else if(k==1)
                {t=s.top();t->leftChild=p;}
            else
                {t=s.top();t->rightChild=p;}
        }
        in>>ch;
    }
}

template
ostream& operator<<(ostream& out,const BinaryTree& rhs)
{
    rhs.traverse(rhs.root,out);
    out<
istream& operator>>(istream& in, BinaryTree& rhs)
{
    rhs.createBinTree(in,rhs.root);
    return in;
}

template
void BinaryTree::inOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p))
{
    if(subTree!=NULL){
        inOrder(subTree->leftChild,visit);
        visit(subTree);
        inOrder(subTree->rightChild,visit);
    }
}

template
void BinaryTree::preOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p))
{
    if(subTree!=NULL){
        visit(subTree);
        inOrder(subTree->leftChild,visit);
        inOrder(subTree->rightChild,visit);
    }
}

template
void BinaryTree::postOrder(BinTreeNode *subTree,void (*visit)(BinTreeNode *p))
{
    if(subTree!=NULL){
        inOrder(subTree->leftChild,visit);
        inOrder(subTree->rightChild,visit);
        visit(subTree);
    }
}

template
int BinaryTree::size(BinTreeNode *subTree)const
{
    if(subTree==NULL)  return 0;
    else
        return 1+size(subTree->leftChild)+size(subTree->rightChild);
}

template
int BinaryTree::height(BinTreeNode *subTree)const
{
    if(subTree==NULL) return 0;
    else{
        int i=height(subTree->leftChild);
        int j=height(subTree->rightChild);
        return (i>j)?i+1:j+1;
    }
}

template
BinTreeNode* BinaryTree::copy(const BinTreeNode *orignode)
{
    if(orignode==NULL) return NULL;
    BinTreeNode *temp=new BinTreeNode;
    temp->data=orignode->data;
    temp->leftChild=copy(orignode->leftChild);
    temp->rightChild=copy(orignode->rightChild);
    return temp;
}

template
BinaryTree& BinaryTree::operator=(const BinaryTree& rhs)
{
    this->destroy(this->root);
    this->root=copy(rhs.root);
    return *this;
}

template
void BinaryTree::levelOrder(void (*visit)(BinTreeNode *p))
{
    deque* > dq; 
    BinTreeNode *p=root;
    dq.push_back(p);
    while(!dq.empty()){
        p=dq.front();
        visit(p);
        dq.pop_front();

        if(p->leftChild!=NULL) dq.push_back(p->leftChild);
        if(p->rightChild!=NULL) dq.push_back(p->rightChild);
    }
}


测试函数:

int main(int argc, char* argv[])
{

    BinaryTree b('#');
    cin>>b;
    cout< a('#');
    //cin>>a;
    //cout< a=b;
    //cout<

测试结果:


a(b(c,d),e(f,g))#
a b c d e f g


请按任意键继续. . .



你可能感兴趣的:(数据结构)