[資料結構]二叉樹之二叉鏈表的類模板實現,二叉樹二叉

來源:互聯網
上載者:User

[資料結構]二叉樹之二叉鏈表的類模板實現,二叉樹二叉

該類模板實現了一個二叉樹的模板類,採用二叉鏈表實現。

定義二叉樹節點類,採用二叉鏈表實現。

/////////////////////////#include <iostream>#include <cstdlib>#include <stack>#include <deque>using namespace std;template<class T>struct BinTreeNode  //二叉樹節點類的定義,使用二叉鏈表{    T data;    BinTreeNode<T> *leftChild, *rightChild;    BinTreeNode():leftChild(NULL),rightChild(NULL){}    BinTreeNode(T x,BinTreeNode<T> *l=NULL,BinTreeNode<T> *r=NULL):data(x),leftChild(l),rightChild(r){}};

二叉樹的模板類實現如下:可進行相應的功能擴充。

介面部分:

template<class T>class BinaryTree//二叉樹的模板類{public:    BinaryTree():root(NULL){}    BinaryTree(char x):root(NULL),RefValue(x){}    BinaryTree(const BinaryTree<T>& rhs){root=copy(rhs.root);}//copy建構函式    BinaryTree<T>& operator=(const BinaryTree<T>& rhs);//copy 賦值運算子;析構+copy建構函式      ~BinaryTree(){destroy(root);}//解構函式    bool isEmpty()const{return root!=NULL?false:true;}    BinTreeNode<T>* leftChild(BinTreeNode<T>* current)const{return current!=NULL?current->leftChild:NULL;}    BinTreeNode<T>* rightChild(BinTreeNode<T>* current)const{return current!=NULL?current->rightChild:NULL;}    BinTreeNode<T>* parent(BinTreeNode<T>* current)const{return (root==NULL || current==root)?NULL:parent(root,current);}//尋找其父節點    BinTreeNode<T>* getRoot()const{return root;}    void inOrder(void (*visit)(BinTreeNode<T> *p)){inOrder(root,visit);}//中序遞迴遍曆    void preOrder(void (*visit)(BinTreeNode<T> *p)){preOrder(root,visit);}//前序遞迴    void postOrder(void (*visit)(BinTreeNode<T> *p)){postOrder(root,visit);}//後序遞迴    void levelOrder(void (*visit)(BinTreeNode<T> *p));//使用隊列的層次遍曆    int size()const {return size(root);}//使用後序遞迴遍曆求節點個數    int height()const {return height(root);}//使用後序遞迴遍曆求二叉樹的高度protected:    BinTreeNode<T> *root;    char RefValue;//資料輸入停止標誌    void destroy(BinTreeNode<T>* subTree);//遞迴刪除二叉樹節點,後序遍曆刪除    BinTreeNode<T>* copy(const BinTreeNode<T> *orignode);//copy構造;前序    BinTreeNode<T>* parent(BinTreeNode<T>* subTree,BinTreeNode<T>* current)const;//返回父節點    void traverse(BinTreeNode<T>* subTree,ostream& out)const;//按前序方式遍曆輸出每個節點的值    void createBinTree(istream& in,BinTreeNode<T>* & subTree);//採用廣義表表示的二叉樹建立方法    void inOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p));//中序遍曆    void preOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p));//前序走訪    void postOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p));//後序遍曆    int size(BinTreeNode<T> *subTree)const;//使用後序遞迴遍曆求節點個數    int height(BinTreeNode<T> *subTree)const;//使用後序遞迴遍曆求二叉樹的高度        friend ostream& operator<< <T>(ostream& out,const BinaryTree<T>& rhs);//add <T> 前序輸出二叉樹    friend istream& operator>> <T>(istream& in, BinaryTree<T>& rhs);      //add <T> 採用廣義表表示方式建立二叉樹};

相應成員函數的具體實現:


template<class T>void BinaryTree<T>::destroy(BinTreeNode<T>* subTree){    if(subTree!=NULL){        destroy(subTree->leftChild);        destroy(subTree->rightChild);        delete subTree;    }}template<class T>BinTreeNode<T>* BinaryTree<T>::parent(BinTreeNode<T>* subTree,BinTreeNode<T>* current)const{    if(subTree==NULL) return NULL;    if(subTree->leftChild==current || subTree->rightChild==current) return subTree;        BinTreeNode<T>* p;    if((p=parent(subTree->leftChild,current))!=NULL)        return p    else         return parent(subTree->rightChild,current);}template<class T>void BinaryTree<T>::traverse(BinTreeNode<T>* subTree,ostream& out)const{    if(subTree!=NULL){        out<<subTree->data<<" ";        traverse(subTree->leftChild,cout);        traverse(subTree->rightChild,out);    }}template<class T>void BinaryTree<T>::createBinTree(istream& in,BinTreeNode<T>* & subTree){    stack<BinTreeNode<T>* > s;    subTree=NULL;    BinTreeNode<T> *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<T>(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<class T>ostream& operator<<(ostream& out,const BinaryTree<T>& rhs){    rhs.traverse(rhs.root,out);    out<<endl;    return out;}template<class T>istream& operator>>(istream& in, BinaryTree<T>& rhs){    rhs.createBinTree(in,rhs.root);    return in;}template<class T>void BinaryTree<T>::inOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p)){    if(subTree!=NULL){        inOrder(subTree->leftChild,visit);        visit(subTree);        inOrder(subTree->rightChild,visit);    }}template<class T>void BinaryTree<T>::preOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p)){    if(subTree!=NULL){        visit(subTree);        inOrder(subTree->leftChild,visit);        inOrder(subTree->rightChild,visit);    }}template<class T>void BinaryTree<T>::postOrder(BinTreeNode<T> *subTree,void (*visit)(BinTreeNode<T> *p)){    if(subTree!=NULL){        inOrder(subTree->leftChild,visit);        inOrder(subTree->rightChild,visit);        visit(subTree);    }}template<class T>int BinaryTree<T>::size(BinTreeNode<T> *subTree)const{    if(subTree==NULL)  return 0;    else        return 1+size(subTree->leftChild)+size(subTree->rightChild);}template<class T>int BinaryTree<T>::height(BinTreeNode<T> *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<class T>BinTreeNode<T>* BinaryTree<T>::copy(const BinTreeNode<T> *orignode){    if(orignode==NULL) return NULL;    BinTreeNode<T> *temp=new BinTreeNode<T>;    temp->data=orignode->data;    temp->leftChild=copy(orignode->leftChild);    temp->rightChild=copy(orignode->rightChild);    return temp;}template<class T>BinaryTree<T>& BinaryTree<T>::operator=(const BinaryTree<T>& rhs){    this->destroy(this->root);    this->root=copy(rhs.root);    return *this;}template<class T>void BinaryTree<T>::levelOrder(void (*visit)(BinTreeNode<T> *p)){    deque<BinTreeNode<T>* > dq;     BinTreeNode<T> *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<char> b('#');    cin>>b;    cout<<b<<endl;    //b.levelOrder(NULL);    //BinaryTree<char> a('#');    //cin>>a;    //cout<<a<<endl;    // b=a;    //cout<<b<<endl;    //BinaryTree<char> a=b;    //cout<<a<<endl;    //cout<<b.size()<<endl;    //cout<<b.isEmpty()<<endl;    //cout<<b.height()<<endl;    system("pause");    return 0;}

測試結果:


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


請按任意鍵繼續. . .



著作權聲明:本文為【借你一秒】原創文章,轉載請標明出處。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.