码迷,mamicode.com
首页 > 其他好文 > 详细

创建/遍历 二叉树

时间:2015-01-14 21:20:53      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <iostream>
  2 #include <fstream>
  3 
  4 using namespace std;
  5 
  6 
  7 //假定所创建的二叉树如下图所示
  8 /*
  9                                              A
 10                                            /    11                                           B     C
 12                                          / \    / 
 13                                         D   E  F
 14                                              \  
 15                                               G
 16 
 17 BiTree.txt:
 18 A B D # # E # G # # C  F # # #
 19 
 20 */
 21 char *preorder  = "ABDEGCG";
 22 char *inorder   = "DBEGAFC";    
 23 char *postorder = "DGEBFCA";
 24 const char INVALID = #;//INVALID类型适配 template <typename T> 
 25 
 26 
 27 template <typename T>
 28 struct BiNode 
 29 {
 30     T Value;
 31     BiNode<T> *pLeft;
 32     BiNode<T> *pRight;
 33 };
 34 
 35 
 36 template <typename T>
 37 class BiTree 
 38 {
 39     public:
 40         BiTree();
 41         ~BiTree();
 42         void PreOrder1();//递归
 43         void PreOrder2();//非递归
 44         void InOrder1();//递归
 45         void InOrder2();//非递归
 46         void PostOrder1();//递归
 47         void PostOrder2();//非递归
 48         void LevelOrder();
 49     private:
 50         BiNode<T> * root;
 51         void CreateBiTree(BiNode<T> * &root);
 52         void ReleaseBiTree(BiNode<T> * root);
 53         
 54 };
 55 
 56 
 57 
 58 template <typename T>
 59 void BiTree::CreateBiTree(BiNode<T> * & root)
 60 {
 61     T Value;
 62     cin>>Value;
 63     if(Value == INVALID)
 64     {
 65         root = NULL;
 66     }
 67     else 
 68     {
 69         root = new BiNode<T>;
 70         root->Value = Value; 
 71         CreateBiTree(root->pLeft);      
 72         CreateBiTree(root->pRight); 
 73     }
 74 }
 75 
 76 
 77 template <typename T>
 78 void BiTree::ReleaseBiTree(BiNode<T> * root)
 79 {
 80     if(root != NULL)
 81     {
 82         ReleaseBiTree(root->pLeft);
 83         ReleaseBiTree(root->pRight);
 84         delete root;
 85     }
 86 }
 87     
 88 template <typename T>
 89 BiTree::BiTree():root(NULL)
 90 {
 91     CreateBitree(root);
 92 }
 93 
 94 template <typename T>
 95 BiTree::~BiTree()
 96 {
 97     ReleaseBiTree(root);
 98 }
 99 
100 template <typename T>
101 void BiTree::PreOrder1()
102 {
103     if(root != NULL)
104     {
105         cout<<root->Value<<" ";
106         PreOrder1(root->pLeft);
107         PreOrder1(root->pRight);
108     }
109 }
110 
111 template <typename T>
112 void BiTree::InOrder1()
113 {
114     if(root != NULL)
115     {
116         InOrder1(root->pLeft);
117         cout<<root->Value<<" ";
118         InOrder1(root->pRight);
119     }
120 }        
121 
122 template <typename T>
123 void BiTree::PostOrder1()
124 {
125     if(root != NULL)
126     {
127                 PostOrder1(root->pLeft);
128                 PostOrder1(root->pRight);
129                 cout<<root->Value<<" ";
130     }
131 }
132     
133 int main()
134 {
135 
136     ifstream fin("BiTree.txt");// 已有输入文件
137     //ofstream fout("output.txt");  //输出文件
138     
139     streambuf *cinbackup;  
140     //streambuf *coutbackup; 
141     
142     cinbackup= cin.rdbuf(fin.rdbuf());  //用 rdbuf() 重新定向
143     //coutbackup= cout.rdbuf(fout.rdbuf());  //用 rdbuf() 重新定向
144     
145     BiTree<char> bitree;
146     bitree.PreOrder1();
147     bitree.InOrder1();
148     bitree.PostOrder1();
149     
150     cin.rdbuf(cinbackup);  // 取消,恢复键盘输入
151     //cout.rdbuf(coutbackup);  //取消,恢复屏幕输出
152     system("pause");
153     return 0;
154 }


VC2005编译时提示错误如下:

Error 1 error C2955: ‘BiTree‘ : use of class template requires template argument list c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 59 
Error 2 error C2244: ‘BiTree<T>::CreateBiTree‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 74 
Error 3 error C2244: ‘BiTree<T>::ReleaseBiTree‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 86 
Error 4 error C2244: ‘BiTree<T>::BiTree‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 92 
Error 5 error C2244: ‘BiTree<T>::~BiTree‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 98 
Error 6 error C2244: ‘BiTree<T>::PreOrder1‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 109 
Error 7 error C2244: ‘BiTree<T>::InOrder1‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 120 
Error 8 error C2244: ‘BiTree<T>::PostOrder1‘ : unable to match function definition to an existing declaration c:\users\dql\documents\visual studio 2005\projects\bitree\bitree\main.cpp 131 

 

 

望园友们指点错误

 

创建/遍历 二叉树

标签:

原文地址:http://www.cnblogs.com/Dillonn/p/4224797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!