1. 程式人生 > >資料結構 - 互換二叉樹中所有結點的左右子樹(C++)

資料結構 - 互換二叉樹中所有結點的左右子樹(C++)

 

#include <iostream>

#define NULL 0

using 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; } private: void 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 << endl; cout << "In Order: "; a.In_Order(); cout << endl; cout << "Post Order: "; a.Post_Order(); cout << endl; cout << endl << "------Swap left, right subtree of all nodes------" << endl << endl; Swap(a.root); cout << "Pre Order: "; a.Pre_Order(); cout << endl; cout << "In Order: "; a.In_Order(); cout << endl; cout << "Post Order: "; a.Post_Order(); cout << endl << endl; } // Output: /* Pre Order: A B D E C F In Order: D B E A F C Post Order: D E B F C A ------Swap left, right subtree of all nodes------ Pre Order: A C F B E D In Order: C F A E B D Post Order: F C E D B A */

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net