数据结构 互换二叉树中所有结点的左右子树 C

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

#include <iostream>#define NULL 0using namespace std;template<class T>struct BTNode{ T data; BTNode<T> *lChild, *rChild; BTNode(); BTNode(const T &val, BTNode<T> *Childl = NULL, BTNode<T> *Childr = NULL) {  data = val;  lChild = Childl;  rChild = Childr; } BTNode<T>* CopyTree() {  BTNode<T> *l, *r, *n;  if(&data == NULL)  {   return NULL;  }  l = lChild->CopyTree();  r = rChild->CopyTree();  n = new BTNode<T>(data, l, r);  return n; }};template<class T>BTNode<T>::BTNode(){ lChild = rChild = NULL;}template<class T>class BinaryTree{public: BTNode<T> *root; BinaryTree(); ~BinaryTree(); void Pre_Order()void In_Order()void Post_Order()void DestroyTree(); BTNode<T>* MakeTree(const T &element, BTNode<T> *l, BTNode<T> *r) {  root = new BTNode<T> (element, l, r);  if(root == NULL)  {   cout << "Failure for applying storage address, system will close the process." << endl;   exit(1);  }  return root; }privatevoid PreOrder(BTNode<T> *r)void InOrder(BTNode<T> *r)void PostOrder(BTNode<T> *r)void Destroy(BTNode<T> *&r);};template<class T>BinaryTree<T>::BinaryTree(){ root = NULL;}template<class T>BinaryTree<T>::~BinaryTree(){ DestroyTree();}template<class T>void BinaryTree<T>::Pre_Order(){ PreOrder(root);}template<class T>void BinaryTree<T>::In_Order(){ InOrder(root);}template<class T>void BinaryTree<T>::Post_Order(){ PostOrder(root);}template<class T>void BinaryTree<T>::DestroyTree(){ Destroy(root);}template<class T>void BinaryTree<T>::PreOrder(BTNode<T> *r){ if(r != NULL) {  cout << r->data << ' ';  PreOrder(r->lChild);  PreOrder(r->rChild); }}template<class T>void BinaryTree<T>::InOrder(BTNode<T> *r){ if(r != NULL) {  InOrder(r->lChild);  cout << r->data << ' ';  InOrder(r->rChild); }}template<class T>void BinaryTree<T>::PostOrder(BTNode<T> *r){ if(r != NULL) {  PostOrder(r->lChild);  PostOrder(r->rChild);  cout << r->data << ' '; }}template<class T>void BinaryTree<T>::Destroy(BTNode<T> *&r){ if(r != NULL) {  Destroy(r->lChild);  Destroy(r->rChild);  delete r;  r = NULL; }}template<class T>void Swap(BTNode<T> *r) // Swap left, right subtree of all nodes.{ BTNode<T> *p; if(r) {  p = r->lChild;  r->lChild = r->rChild;  r->rChild = p; // Swap left, right child.  Swap(r->lChild); // Swap left, right subtree of all the nodes in left subtree.  Swap(r->rChild); // Swap left, right subtree of all the nodes in right subtree. }}void main(){ BTNode<char> *b, *c, *d, *e, *f, *g; BinaryTree<char> a; b = a.MakeTree('F', NULL, NULL); c = a.MakeTree('E', NULL, NULL); d = a.MakeTree('D', NULL, NULL); e = a.MakeTree('C', b, NULL); f = a.MakeTree('B', d, c); g = a.MakeTree('A', f, e); cout << "Pre Order: "; a.Pre_Order(); cout << endlcout << "In Order: "; a.In_Order(); cout << endlcout << "Post Order: "; a.Post_Order(); cout << endlcout << endl << "------Swap left, right subtree of all nodes------" << endl << endl; Swap(a.root); cout << "Pre Order: "; a.Pre_Order(); cout << endlcout << "In Order: "; a.In_Order(); cout << endlcout << "Post Order: "; a.Post_Order(); cout << endl << endl;}// Output:/*Pre Order: A B D E C FIn Order: D B E A F CPost Order: D E B F C A------Swap left, right subtree of all nodes------Pre Order: A C F B E DIn Order: C F A E B DPost Order: F C E D B A*/
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/yffhhffv/article/details/83820398