數據結構二叉樹的所有基本功能實現。(C++版)


本人剛學數據結構,對樹的基本功能網上找不到C++代碼

便自己寫了一份,貼出方便大家進行測試和學習。

大部分功能未測試,如有錯誤或者BUG,請高手們指教一下,謝謝。

 

結點聲明:

BinTreeNode.h

 1 template<typename ElemType>
 2 struct BinTreeNode
 3 {
 4     ElemType data;                     //數據元素
 5     BinTreeNode<ElemType> *leftChild;  //指向左孩子的指針
 6     BinTreeNode<ElemType> *rightChild; //指向右孩子的指針
 7     BinTreeNode<ElemType> *pre;        //指向雙親的指針
 8 
 9     //函數構造
10     BinTreeNode();
11     BinTreeNode(const ElemType &val,
12         BinTreeNode<ElemType> *lChild=NULL,
13         BinTreeNode<ElemType> *rChild=NULL);
14     BinTreeNode<ElemType> &operator =(const BinTreeNode<ElemType> &copy);
15 };
16 
17 template<typename ElemType>
18 BinTreeNode<ElemType>::BinTreeNode()
19 {
20     leftChild=rightChild=pre=NULL;
21 }
22 
23 template<typename ElemType>
24 BinTreeNode<ElemType>::BinTreeNode(const ElemType &val,
25                         BinTreeNode<ElemType> *lChild,
26                         BinTreeNode<ElemType> *rChild)
27 {
28     data=val;
29     leftChild=lChild;
30     rightChild=rChild;
31     pre=NULL;
32 }
33 
34 template<typename ElemType>
35 BinTreeNode<ElemType> &BinTreeNode<ElemType>::operator =(const BinTreeNode<ElemType> &copy)
36 {
37     data=copy.data;
38     leftChild=copy.leftChild;
39     rightChild=copy.leftChild;
40     pre=copy.pre;
41 }
BinTreeNode.h

 

類聲明:

BinaryTree.h

 1 #include"BinTreeNode.h"
 2 template<typename ElemType>
 3 class BinaryTree
 4 {
 5 protected:
 6     //數據成員
 7     BinTreeNode<ElemType> *root;
 8     //輔助函數
 9     void CreateBTreeHelp(BinTreeNode<ElemType> *&r,ElemType pre[],ElemType in[],int,int,int,int);//構造樹
10     BinTreeNode<ElemType> *CopyTreeHelp(const BinTreeNode<ElemType> *r);//復制二叉樹
11     void DestroyHelp(BinTreeNode<ElemType> *&r);//銷毀r為根的二叉樹
12     //先,中,后序遍歷
13     void PreOrderHelp (const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const;
14     void InOrderHelp  (const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const;
15     void PostOrderHelp(const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const;
16 
17     int HeightHelp(const BinTreeNode<ElemType> *r) const;//返回樹的高度
18     int NodeCountHelp(const BinTreeNode<ElemType> *r)const;//返回樹的節點個數
19 
20 public:
21     BinaryTree(){root=NULL}//無參構造
22     BinaryTree(const ElemType &e);//建立以e元素為根的二叉樹
23     BinaryTree(BinTreeNode<ElemType> *r);//建立以r為根的二叉樹
24     virtual ~BinaryTree();//有指針用虛虛構
25     BinaryTree<ElemType> &CreateBTree(ElemType pre[],ElemType in[],int n); //構造樹
26     BinTreeNode<ElemType> *GetRoot() const;//返回根
27     bool Empty()const;
28     bool GetElem(const BinTreeNode<ElemType> *cur,ElemType &e);//用e結點返回cur元素值
29     bool SetTlem(const BinTreeNode<ElemType> *cur,const ElemType &e);//e賦值給cur
30     //先,中,后序遍歷
31     void PreOrder(void (*visit) (const ElemType &))const;
32     void InOrder(void (*visit) (const ElemType &))const;
33     void PostOrder(void (*visit) (const ElemType &))const;
34     //層次遍歷
35     void LevelOrder(void (*visit) (const ElemType &))const;
36     int NodeCount()const;
37     BinTreeNode<ElemType> *LeftChild(const BinTreeNode<ElemType> *cur)const;//返回cur左孩子
38     BinTreeNode<ElemType> *RightChild(const BinTreeNode<ElemType> *cur)const;//返回cur右孩子
39     BinTreeNode<ElemType> *Parent(const BinTreeNode<ElemType> *cur)const;//返回cur雙親
40     void InsertLeftChild(BinTreeNode<ElemType> *cur,const ElemType &e);//插入左孩子
41     void InsertRightChild(BinTreeNode<ElemType> *cur,const ElemType &e);//插入右孩子
42     void DeleteLeftChild(BinTreeNode<ElemType> *cur);//刪除左子樹
43     void DeleteRightChild(BinTreeNode<ElemType> *cur);//刪除右子樹
44     int Height()const;//求二叉樹高
45     BinaryTree(const BinaryTree<ElemType> &copy);//復制構造函數
46     BinaryTree<ElemType> &operator =(const BinaryTree<ElemType> &copy);//重載賦值運算符
47 };
48 #include"CreateBTree.h"
49 #include"Destroy,copy,operator.h"
50 #include"height.h"
51 #include"NodeCount.h"
52 #include"return,set.h"
53 #include"Traversal.h"
BinaryTree.h

 

成員函數:

CreateBTree.h

 1 template<typename ElemType>
 2 void BinaryTree<ElemType>::CreateBTreeHelp(BinTreeNode<ElemType> *&r,
 3                                            ElemType pre[],ElemType in[],
 4                                            int preLeft,int preRight,int inLeft,int inRight)
 5 
 6 {
 7     if(preLeft>preRight||inLeft>inRight)
 8         r=NULL;
 9     else
10     {
11         r=new BinTreeNode<ElemType>(pre[preLeft]);//生成根結點
12         int mid=inLeft;
13         while(in[mid]!=pre[preLeft])
14             mid++;
15         CreateBTreeHelp(r->leftChild,pre,in,preLeft+1,preLeft+mid-inLeft,inLeft,mid-1);
16         CreateBTreeHelp(r->rightChild,pre,in,preLeft+mid-inLeft+1,preRight,mid+1,inRight);
17     }
18 }
19 
20 template<typename ElemType>
21 //構造樹
22 BinaryTree<ElemType>& BinaryTree<ElemType>::CreateBTree(ElemType pre[],ElemType in[],int n)
23 {
24     BinTreeNode<ElemType> *r;   //
25     CreateBTreeHelp(r,pre,in,0,n-1,0,n-1);
26     //return BinaryTree<ElemType>(r);//Error:不應該返回局部變量的地址
27     *this = BinaryTree<ElemType>(r);
28     return *this;
29 }
CreateBTree.h

Destroy,copy,operator.h

 1 //Destroy
 2 template<typename ElemType>
 3 void BinaryTree<ElemType>::DestroyHelp(BinTreeNode<ElemType> *&r)
 4 {
 5     if(r!=NULL)
 6     {
 7         DestroyHelp(r->leftChild);
 8         DestroyHelp(r->rightChild);
 9         delete r;
10         r=NULL;
11     }
12 }
13 template<typename ElemType>
14 //刪除左子樹
15 void BinaryTree<ElemType>::DeleteLeftChild(BinTreeNode<ElemType> *cur)
16 {
17     DestroyHelp(cur->leftChild);
18 }
19 template<typename ElemType>
20 //刪除右子樹
21 void BinaryTree<ElemType>::DeleteRightChild(BinTreeNode<ElemType> *cur)
22 {
23     DestroyHelp(cur->rightChild);
24 }
25 //虛構
26 template<typename ElemType>
27 BinaryTree<ElemType>::~BinaryTree()
28 {
29     DestroyHelp(root);
30 }
31 
32 //Copy
33 template<typename ElemType>
34 BinTreeNode<ElemType> *BinaryTree<ElemType>::CopyTreeHelp(const BinTreeNode<ElemType> *r)
35 {
36     BinTreeNode<ElemType> *cur;
37     if(r==NULL)   cur=NULL;
38     else
39     {
40         BinTreeNode<ElemType> *lChild=CopyTreeHelp(r->leftChild);//復制左子樹
41         BinTreeNode<ElemType> *rChild=CopyTreeHelp(r->rightChild);//復制右子樹
42         cur=new BinTreeNode<ElemType>(r->data,lChild,rChild);
43         //復制根節點
44     }
45     return cur;
46 }
47 template<typename ElemType>
48 BinaryTree<ElemType>::BinaryTree(const BinaryTree<ElemType> &copy)
49 {
50     root=CopyTreeHelp(copy.root)
51 }
52 
53 //operator =
54 template<typename ElemType>
55 BinaryTree<ElemType> &BinaryTree<ElemType>::operator=(const BinaryTree<ElemType> &copy)
56 {
57     if(&copy!=this)
58     {
59         DestroyHelp(root);
60         root=CopyTreeHelp(copy.root);
61     }
62     return *this;
63 }
Destroy,copy,operator.h

height.h

 1 template<typename ElemType>
 2 int BinaryTree<ElemType>::HeightHelp(const BinTreeNode<ElemType> *r) const
 3 {
 4     if(r==NULL)  return 0;
 5     else
 6     {
 7         int lHeight,rHeight,height;
 8         lHeight=HeightHelp(r->leftChild);
 9         rHeight=HeightHelp(r->rightChild);
10         height-1=lHeight>rHeight?lHeight:rHeight;//深度為左右子樹最大值加1;
11         return height;
12     }
13 
14 }
height.h

NodeCount.h

 1 template<class ElemType>
 2 int BinaryTree<ElemType>::NodeCountHelp(const BinTreeNode<ElemType> *r) const
 3 {
 4     int count;
 5     if (r == NULL)
 6         count=0;
 7     else
 8     {
 9         count = NodeCountHelp(r->leftChild) + NodeCountHelp(r->rightChild) + 1;
10         //左孩子加右孩子結點數再加根節點。
11     }
12     return count;
13 }
14 template<class ElemType>
15 int BinaryTree<ElemType>::NodeCount() const
16 {
17     return NodeCountHelp(root);
18 }
NodeCount.h

return,set.h

template<typename ElemType>
//返回根
BinTreeNode<ElemType> *BinaryTree<ElemType>::GetRoot() const
{
    return root;
}

template<typename ElemType>
//用e結點返回cur元素值
bool BinaryTree<ElemType>::GetElem(const BinTreeNode<ElemType> *cur, ElemType &e)
{
    if(cur)
    {
        e = cur->data;
        return true
    }
    else
        return false;
}

template<typename ElemType>
//e賦值給cur
bool BinaryTree<ElemType>::SetTlem(const BinTreeNode<ElemType> *cur, const ElemType &e)
{
    if(cur)
    {
        cur->data = e;
        return true;
    }
    else
        return false;
}

template<typename ElemType>
//返回cur左孩子
BinTreeNode<ElemType> *BinaryTree<ElemType>::LeftChild(const BinTreeNode<ElemType> *cur)const
{
    if(cur->leftChild)
        return cur->leftChild;
    else 
        return NULL;
}

template<typename ElemType>
//返回cur右孩子
BinTreeNode<ElemType> *BinaryTree<ElemType>::RightChild(const BinTreeNode<ElemType> *cur)const
{
    if(cur->RightChild)
        return cur->RightChild;
    else 
        return NULL;
}

template<typename ElemType>
//插入左孩子
void BinaryTree<ElemType>::InsertLeftChild(BinTreeNode<ElemType> *cur,const ElemType &e)//插入左孩子
{
    if(!(cur->leftChild))
        cur->leftChild = new BinTreeNode<ElemType>(e);
    else throw "左孩子已存在!插入失敗.";
}

template<typename ElemType>
//插入右孩子
void BinaryTree<ElemType>::InsertRightChild(BinTreeNode<ElemType> *cur,const ElemType &e)//插入右孩子
{
    if(!(cur->rightChild))
        cur->rightChild = new BinTreeNode<ElemType>(e);
    else throw "右孩子已存在!插入失敗.";
}

template<typename ElemType>
//返回cur的雙親
BinTreeNode<ElemType> *BinaryTree<ElemType>::Parent(const BinTreeNode<ElemType> *cur)const
{
    if(cur->pre != NULL)
        return cur->pre;
    else
        return NULL;
}

template<typename ElemType>
//建立以r為根的二叉樹
BinaryTree<ElemType>::BinaryTree(BinTreeNode<ElemType> *r)
{
    root = r;
}

template<typename ElemType>
//建立以e元素為根的二叉樹
BinaryTree<ElemType>::BinaryTree(const ElemType &e)//建立以e元素為根的二叉樹
{
    root = new BinTreeNode(e);
}

template<typename ElemType>
//判斷樹空
bool BinaryTree<ElemType>::Empty() const
{
    return root == NULL;
}
return,set.h

Traversal.h

  1 //recursion algorithm
  2 template<typename ElemType>
  3 void BinaryTree<ElemType>::PreOrderHelp(const BinTreeNode<ElemType> *r,
  4                                         void (*visit) (const ElemType &))const
  5 {
  6     if(r!=NULL)
  7     {
  8         visit(r->data);
  9         PreOrderHelp(r->leftChild,visit);
 10         PreOrderHelp(r->rightChild,visit);
 11     }
 12 }
 13 
 14 
 15 
 16 template<typename ElemType>
 17 void BinaryTree<ElemType>::InOrderHelp(const BinTreeNode<ElemType> *r,
 18                                         void (*visit) (const ElemType &))const
 19 {
 20     if(r!=NULL)
 21     {
 22         InOrderHelp(r->leftChild,visit);
 23         visit(r->data);
 24         InOrderHelp(r->rightChild,visit);
 25     }
 26 }
 27 
 28 
 29 
 30 template<typename ElemType>
 31 void BinaryTree<ElemType>::PostOrderHelp(const BinTreeNode<ElemType> *r,
 32                                         void (*visit) (const ElemType &))const
 33 {
 34     if(r!=NULL)
 35     {
 36         PostOrderHelp(r->leftChild,visit);
 37         PostOrderHelp(r->rightChild,visit);
 38         visit(r->data);
 39 }
 40 }
 41 
 42 using namespace std;
 43 template<typename ElemType>
 44 void print(const ElemType &e )
 45 {
 46     cout<<e<<" ";
 47 }
 48 #include<queue>
 49 template<typename ElemType>
 50 void BinaryTree<ElemType>::LevelOrder(void (*visit) (const ElemType &))const
 51 {    //隊列實現
 52     visit=print;
 53     queue<BinTreeNode<ElemType> *> q;
 54     BinTreeNode<ElemType> *t=GetRoot();
 55     if(t!=NULL) q.push(t);                //根非空,入隊
 56     while(!q.empty())                //隊不空
 57     {
 58         t=q.front();
 59         q.pop();                    //出隊
 60         (*visit)(t->data);
 61         if(t->leftChild)
 62             q.push(t->leftChild);  //遍歷左孩子
 63         if(t->rightChild)           
 64             q.push(t->rightChild); //遍歷右孩子
 65     }
 66 
 67 }
 68 
 69 /*
 70 //非遞歸先序遍歷
 71 #include<stack>
 72 using namespace std;
 73 template<typename ElemType>
 74 void NonRecurPreOrder(const BinaryTree<ElemType> &bt,
 75                                         void (*visit) (const ElemType &))const
 76 {
 77     BinTreeNode<ElemType> *cur=bt.GetRoot();//當前結點
 78     stack<ElemType> s;
 79     while(cur)//cur不空
 80     {
 81         (*visit)(cur->data);//訪問cur元素
 82         if(cur->leftChild)      //左孩子不空
 83         {    if(cur->rightChild) //右孩子也不空
 84                 s.push(cur);    //入棧(若右孩子空無須入棧)
 85         }
 86         else
 87         {
 88             if(cur->rightChild)  //左孩子空,右孩子不空(不入棧)
 89                 cur=cur->rightChild;
 90             else if(!s.empty())         //左右孩子都空,棧不空
 91                 {
 92                     cur=s.top();       //取出棧,退返。
 93                     s.pop();           //出棧
 94                 }
 95                 else //棧空
 96                     cur=NULL;//結束循環
 97         }
 98     }
 99 }
100 */
101 /*
102 //非遞歸中序遍歷
103 template<typename ElemType>
104 BinTreeNode<ElemType> *GoFarLeft(BinTreeNode<ElemType> *r,
105                                  stack<ElemType> &s)
106 {
107     if(r==NULL)             //結點空
108         return NULL;
109     BinTreeNode<ElemType> *cur=r;
110     while(cur->leftChild)    //往左走
111     {
112         s.push();            //左孩子進棧
113         cur=cur->leftChild;
114     }
115     return cur;
116 }
117 
118 template<typename ElemType>
119 void NonRecurInOrder(const BinaryTree<ElemType> &bt,
120                                         void (*visit) (const ElemType &))const
121 {
122     BinTreeNode<ElemType> *cur=bt.GetRoot();   //cur指向根節點
123     stack<ElemType> s;
124     cur=GoFarLeft(cur,s);                     //走到最底層
125     while(cur)
126     {
127         (*visit)(cur->data);                  
128         if(cur->rightChild)       //右孩子不空
129             cur=GoFarLeft(cur->rightChild,s);  //遍歷右孩子
130         else if(!s.empty())       //右孩子空,取棧
131         {    cur=s.top();    s.pop();}
132             else                  //棧空
133                 cur=NULL;
134     }
135 }
136 */
137 
138 
139 template<typename ElemType>
140 void BinaryTree<ElemType>::PreOrder(void (*visit) (const ElemType &))const
141 {
142     PreOrderHelp(root,visit);
143 }
144 template<typename ElemType>
145 void BinaryTree<ElemType>::InOrder(void (*visit) (const ElemType &))const
146 {
147     InOrderHelp(root,visit);
148 }
149 template<typename ElemType>
150 void BinaryTree<ElemType>::PostOrder(void (*visit) (const ElemType &))const
151 {
152     PostOrderHelp(root,visit);
153 }
Traversal.h

 

 

 

主函數:

 1 #include<iostream>
 2 #include"BinaryTree.h"
 3 using namespace std;
 4 BinaryTree<char>;
 5 int main()
 6 {
 7     char s1[1000], s2[1000];
 8     cin >> s1 >> s2;
 9     int n = strlen(s1);
10     if(n != strlen(s2))
11         cout << "ERROR\n";
12     BinTreeNode<char> *root = NULL;
13     BinaryTree<char> T(root);
14     T.CreateBTree(s1, s2, n);
15     T.LevelOrder(print<char>);
16     cout<<endl;
17     T.PreOrder(print<char>);
18     cout<<endl;
19     T.InOrder(print<char>);
20     cout<<endl;
21     T.PostOrder(print<char>);
22     cout<<endl;
23 }
main

 

運行結果如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM