先把代码贴了,有时间再写思路。。
二叉树定义:
binaryTree.h
1 #ifndef BINARYTREE_H 2 #define BINARYTREE_H 3 #include <iostream> 4 #include "LinkedQueue.h" 5 6 template<class T> 7 class BinaryTree; 8 9 10 int count; 11 12 template<class T> 13 class BinaryTreeNode 14 { 15 friend class BinaryTree < T > ; 16 friend void Visit(BinaryTreeNode<T> *); 17 friend void InOrder(BinaryTreeNode<T> *); 18 friend void PreOrder(BinaryTreeNode<T> *); 19 friend void PostOrder(BinaryTreeNode<T> *); 20 friend void LevelOrder(BinaryTreeNode<T> *); 21 public: 22 //构造函数 23 BinaryTreeNode(){ LeftChild = RightChild = 0; } 24 BinaryTreeNode(const T& e){ data = e; LeftChild = RightChild = 0; } 25 BinaryTreeNode(const T& e, BinaryTreeNode *l, BinaryTreeNode *r) 26 { 27 data = e; 28 LeftChild = l; 29 RightChild = r; 30 } 31 32 BinaryTreeNode<T>* GetLeft()const{ return LeftChild; } 33 BinaryTreeNode<T>* GetRight()const{ return RightChild; } 34 T GetData()const{ return data; } 35 private: 36 T data; 37 BinaryTreeNode<T> *LeftChild;//左子树 38 BinaryTreeNode<T> *RightChild;//右子树 39 }; 40 41 template<class T> 42 void Visit(BinaryTreeNode<T> *t) 43 { 44 std::cout << t->data << " "; 45 } 46 47 template<class T> 48 void PreOrder(BinaryTreeNode<T> *t) 49 { 50 if (t) 51 { 52 Visit(t); 53 PreOrder(t->LeftChild); 54 PreOrder(t->RightChild); 55 } 56 } 57 58 template<class T> 59 void InOrder(BinaryTreeNode<T> *t) 60 { 61 if (t) 62 { 63 InOrder(t->LeftChild); 64 Visit(t); 65 InOrder(t->RightChild); 66 } 67 } 68 69 template<class T> 70 void PostOrder(BinaryTreeNode<T> *t) 71 { 72 if (t) 73 { 74 PostOrder(t->LeftChild); 75 PostOrder(t->RightChild); 76 Visit(t); 77 } 78 } 79 80 template<class T> 81 void LevelOrder(BinaryTreeNode<T> *t) 82 { 83 LinkedQueue<BinaryTreeNode<T>*> Q; 84 while (t) 85 { 86 Visit(t); 87 88 if (t->LeftChild) 89 { 90 Q.Add(t->LeftChild); 91 } 92 if (t->RightChild) 93 { 94 Q.Add(t->RightChild); 95 } 96 97 if (Q.IsEmpty()) 98 { 99 return; 100 } 101 Q.Delete(t); 102 103 } 104 } 105 106 107 template<class T> 108 class BinaryTree 109 { 110 public: 111 BinaryTree(){ 112 root = NULL; 113 } 114 115 //复制构造函数 116 BinaryTree(const BinaryTree<T> &t); 117 ~BinaryTree(){ };//Delete(); }; 118 bool IsEmpty() const{ return root == NULL }; 119 bool Root(T& x)const; 120 void MakeTree(const T& element, BinaryTree<T>& left, BinaryTree<T>& right); 121 void BreakTree(T& element, BinaryTree<T> &left, BinaryTree<T> &right); 122 void FreeTree(){ Delete(); }; 123 void PreOrder(void(*Visit)(BinaryTreeNode<T> *u))const 124 { 125 PreOrder(Visit, root); 126 } 127 void InOrder(void(*Visit)(BinaryTreeNode<T> *u))const 128 { 129 InOrder(Visit, root); 130 } 131 void PostOrder(void(*Visit)(BinaryTreeNode<T> *u))const 132 { 133 PostOrder(Visit, root); 134 } 135 136 BinaryTreeNode<T>* CopyTree(const BinaryTreeNode<T>* t);//复制树 137 138 void PrintTree()const; 139 void LevelOrder(void(*Visit)(BinaryTreeNode<T> *u))const; 140 void PreOutput()const; 141 void InOutput()const; 142 void PostOutput()const; 143 void LevelOutput()const; 144 void Delete(); 145 int Height()const 146 { 147 return Height(root); 148 } 149 150 int Size(); 151 152 BinaryTreeNode<T>* GetRoot()const{ return root; } 153 private: 154 BinaryTreeNode<T> *root; 155 void PreOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const; 156 void InOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const; 157 void PostOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const; 158 int Height(BinaryTreeNode<T> *t)const; 159 160 161 static void output(BinaryTreeNode<T> *t) 162 { 163 std::cout << t->data << ' '; 164 } 165 166 static void freeNode(BinaryTreeNode<T> *t) 167 { 168 delete t; 169 t = NULL; 170 } 171 172 static void ctsize(BinaryTreeNode<T> *t) 173 { 174 ++count; 175 } 176 177 }; 178 179 template<class T> 180 void BinaryTree<T>::PrintTree()const 181 { 182 183 } 184 185 template<class T> 186 BinaryTreeNode<T>* BinaryTree<T>::CopyTree(const BinaryTreeNode<T>* t) 187 { 188 BinaryTreeNode<T>* Node = NULL; 189 if (t) 190 { 191 BinaryTreeNode<T>* lchild = CopyTree(t->LeftChild); 192 BinaryTreeNode<T>* rchild = CopyTree(t->RightChild); 193 Node = new BinaryTreeNode<T>(t->data, lchild, rchild); 194 } 195 196 return Node; 197 } 198 199 template<class T> 200 BinaryTree<T>::BinaryTree(const BinaryTree<T> &t) 201 { 202 root = CopyTree(t.root); 203 } 204 205 206 207 template<class T> 208 int BinaryTree<T>::Size() 209 { 210 count = 0; 211 PreOrder(ctsize); 212 return count; 213 } 214 215 template<class T> 216 int BinaryTree<T>::Height(BinaryTreeNode<T> *t)const 217 { 218 if (!t) 219 { 220 return 0; 221 } 222 223 int ll = Height(t->LeftChild); 224 int lr = Height(t->RightChild); 225 226 if (ll > lr) 227 { 228 return ++ll; 229 } 230 else 231 return ++lr; 232 } 233 234 template<class T> 235 bool BinaryTree<T>::Root(T& x) const 236 { 237 if (root) 238 { 239 x = root->data; 240 return true; 241 } 242 return false; 243 } 244 245 template<class T> 246 void BinaryTree<T>::MakeTree(const T& element, BinaryTree<T>& left, BinaryTree<T>& right) 247 { 248 if (&left == &right) 249 { 250 BinaryTree<T> newTree(right); 251 if (&left != this) 252 { 253 root = new BinaryTreeNode<T>(element, left.root, newTree.root); 254 } 255 else 256 { 257 BinaryTree<T> newTree2(left); 258 root = new BinaryTreeNode<T>(element, newTree2.root, newTree.root); 259 } 260 } 261 else if (&left == this) 262 { 263 BinaryTree<T> newTree(left); 264 root = new BinaryTreeNode<T>(element, newTree.root, right.root); 265 } 266 else if (this == &right) 267 { 268 BinaryTree<T> newTree(right); 269 root = new BinaryTreeNode<T>(element, left.root, newTree.root); 270 } 271 else 272 root = new BinaryTreeNode<T>(element, left.root, right.root); 273 274 left.root = right.root = NULL; 275 } 276 277 template<class T> 278 void BinaryTree<T>::BreakTree(T& element, BinaryTree<T> &left, BinaryTree<T> &right) 279 { 280 if (root == NULL) 281 { 282 std::cerr << "树为空,不能拆分" << std::endl; 283 return; 284 } 285 286 BinaryTree<T> newTree = *this; 287 element = newTree.root->data; 288 left.root = newTree.root->LeftChild; 289 right.root = newTree.root->RightChild; 290 291 delete root; 292 root = NULL; 293 } 294 295 template<class T> 296 void BinaryTree<T>::PreOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const 297 { 298 if (t) 299 { 300 Visit(t); 301 cout << endl; 302 cout<<"leftChild is: " 303 PreOrder(Visit, t->LeftChild); 304 PreOrder(Visit, t->RightChild); 305 } 306 } 307 308 template<class T> 309 void BinaryTree<T>::InOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const 310 { 311 if (t) 312 { 313 InOrder(Visit, t->LeftChild); 314 Visit(t); 315 InOrder(Visit, t->RightChild); 316 } 317 } 318 319 template<class T> 320 void BinaryTree<T>::PostOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)const 321 { 322 if (t) 323 { 324 PostOrder(Visit, t->LeftChild); 325 PostOrder(Visit, t->RightChild); 326 Visit(t); 327 } 328 } 329 330 template<class T> 331 void BinaryTree<T>::LevelOrder(void(*Visit)(BinaryTreeNode<T> *u))const 332 { 333 LinkedQueue<BinaryTreeNode<T>* > Q; 334 BinaryTreeNode<T> *t = root; 335 while (t) 336 { 337 cout << "root is "; 338 Visit(t); 339 cout << endl; 340 341 if (t->LeftChild) 342 { 343 Q.Add(t->LeftChild); 344 cout << "Leftchild of " << t->data << " is:"; 345 Visit(t->LeftChild); 346 cout << endl; 347 } 348 else 349 { 350 cout << t->data << " have no leftchild" << endl; 351 } 352 if (t->RightChild) 353 { 354 Q.Add(t->RightChild); 355 cout << "Rightchild of " << t->data << " is:"; 356 Visit(t->RightChild); 357 cout << endl; 358 } 359 360 else 361 { 362 cout << t->data << " have no Rightchild" << endl; 363 } 364 365 if (Q.IsEmpty()) 366 { 367 return; 368 } 369 Q.Delete(t); 370 } 371 } 372 373 template<class T> 374 void BinaryTree<T>::PreOutput()const 375 { 376 PreOrder(output, root); 377 } 378 379 template<class T> 380 void BinaryTree<T>::InOutput()const 381 { 382 InOrder(output, root); 383 } 384 385 template<class T> 386 void BinaryTree<T>::PostOutput()const 387 { 388 PostOrder(output, root); 389 } 390 391 template<class T> 392 void BinaryTree<T>::LevelOutput()const 393 { 394 LevelOrder(output); 395 } 396 397 template<class T> 398 void BinaryTree<T>::Delete() 399 { 400 PostOrder(freeNode, root); 401 root = NULL; 402 } 403 #endif