#pragma once #include<iostream> #include<stdlib.h> #include<assert.h> #include<stack> #include<queue> using namespace std; template<class T> struct BinTreeNode { T _data; BinTreeNode<T>* _left; BinTreeNode<T>* _right; BinTreeNode(const T& x) :_data(x) , _left(NULL) , _right(NULL) {} }; template<class T> class BinTree { typedef BinTreeNode<T> Node; public: BinTree() :_Root(NULL) {} BinTree(const T*arr, size_t n, const T& invalid) { int index = 0; _Root = CreatTree(arr, 10, '#', index); } BinTree& operator=(const BinTree& t)//传统写法 { if (this != &t) { _Destroy(_Root); _Root = _Copy(t._Root); } return *this; } //BinTree& operator=(BinTree<T> t)//现代写法 //{ // swap(_Root, t._Root); // return *this; //} BinTree(const BinTree & t) { _Root = _Copy(t._Root); } ~BinTree() { _Destory(_Root); } Node* CreatTree(const T*arr, size_t n, const T& invalid, int& index) { Node* Root = NULL; if ((index < n) && (arr[index] != invalid)) { Root = new Node(arr[index]); Root->_left = CreatTree(arr, n, '#', ++index); //index++错误 index++返回的是临时变量,出了作用域便销毁,因此不能传给引用 Root->_right = CreatTree(arr, n, '#', ++index); } return Root; } Node* _Copy(Node* Root) { Node* tmp = NULL; if (Root == NULL) { return NULL; } else { tmp = new Node(Root->_data); tmp->_left = _Copy(Root->_left); tmp->_right = _Copy(Root->_right); } return tmp; } void PreOrder() { _PreOrder(_Root); cout << endl; } void InOrder() { _InOrder(_Root); cout << endl; } void PostOrder() { _PostOrder(_Root); cout << endl; } void LevelOrder() { queue<Node*> q1; if (_Root == NULL) { return ; } else { q1.push(_Root); while (!q1.empty()) { Node* front = q1.front(); cout << front->_data << " "; if (front->_left) { q1.push(front->_left); } if (front->_right) { q1.push(front->_right); } q1.pop(); } } cout << endl; } void PreOrderNon() { stack<Node*>s1; Node* cur = _Root; while (cur || !s1.empty()) { while (cur) { cout << cur->_data << " "; s1.push(cur); cur = cur->_left; } Node* top= s1.top(); s1.pop(); cur = top->_right; } cout << endl; } void InOrderNon() { stack<Node* >s1; Node* cur = _Root; while (cur || !s1.empty()) { while (cur) { s1.push(cur); cur = cur->_left; } Node * top = s1.top(); cout << top->_data << " "; s1.pop(); cur = top->_right; } cout << endl; } void PostOrderNon()//后序遍历非递归,很重要 { stack<Node *>s1; Node* cur = _Root; Node* prev= NULL; while ((cur) || (!s1.empty())) { while (cur) { s1.push(cur); cur = cur->_left; } Node* top = s1.top(); if( (top->_right == NULL) ||( top->_right == prev)) { cout << top->_data << " "; prev = top;//用来记录上次访问的元素,用来判断此时访问的元素,右子数访问了吗,如果访问了就可以pop.否则会造成死循环 s1.pop(); } else { cur = top->_right; } } cout << endl; } size_t Size() { return _Size(_Root); } size_t _Size(Node* Root) { if (Root == NULL) { return 0; } else if ((Root->_left == NULL) && (Root->_right == NULL)) { return 1; } else { return 1 + _Size(Root->_left) + _Size(Root->_right); } } size_t Depth() { return _Depth(_Root); } size_t _Depth(Node* Root) { if (Root == NULL) { return 0; } else if (Root->_left == NULL&&Root->_right == 0) { return 1; } else { int leftheight = _Depth(Root->_left); int rightheight = _Depth(Root->_right); return (leftheight > rightheight) ? (leftheight + 1) : (rightheight + 1); } } size_t LeafSize() { return _LeafSize(_Root); } size_t _LeafSize(Node* Root) { if (Root == NULL) { return 0; } else if ((Root->_left == NULL) && (Root->_right == NULL)) { return 1; } else { return _LeafSize(Root->_left) + _LeafSize(Root->_right); } } size_t GetKLevel(int k) { return _GetKLevel(_Root, k); } size_t _GetKLevel(Node* Root, size_t k) { assert(k > 0); if (Root == NULL) { return 0; } if (k == 1) { return 1; } else { return (_GetKLevel(Root->_left, k - 1) + _GetKLevel(Root->_right, k - 1)); } } void _PreOrder(Node* Root) { if (Root == NULL) return ; else { cout<< Root->_data << " "; _PreOrder(Root->_left); _PreOrder(Root->_right); } } void _InOrder(Node* Root) { if (Root == NULL) return; else { _InOrder(Root->_left); cout << Root->_data << " "; _InOrder(Root->_right); } } void _PostOrder(Node* Root) { if (Root == NULL) { return; } else { _PostOrder(Root->_left); _PostOrder(Root->_right); cout << Root->_data << " "; } } void _Destory(Node* Root) { if (Root) { _Destory(Root->_left); _Destory(Root->_right); delete Root; Root = NULL; } } private: Node* _Root; }; void test() { int arr[10] = { 1, 2, 3, '#', '#', 4, '#', '#', 5, 6 }; BinTree<int> t1(arr, 10, '#'); BinTree<int> t2=t1; BinTree<int> t3; t3 = t1; t3.PreOrder(); t2.PreOrder(); t1.PreOrder(); t1.PreOrderNon(); t1.InOrder(); t1.InOrderNon(); t1.PostOrder(); t1.PostOrderNon(); cout<<t1.Size()<<endl; cout << t1.Depth() << endl; cout << t1.LeafSize() << endl; cout << t1.GetKLevel(3) << endl; t1.LevelOrder(); } int main() { test(); system("pause"); return 0; }
实在是不想把这周计划的事情推到下周,所以就赶快在这周的尾巴上结束本周任务。新的一周马上开始,来日可期。