#include<iostream>
#include<queue>
#include<stack>
using namespace std;
template<class T>
struct BinaryTreeNode
{
BinaryTreeNode(T value = 0)
:_value(value)
,_LeftChild(NULL)
, _RightChild(NULL)
{}
T _value;
BinaryTreeNode<T>* _LeftChild;
BinaryTreeNode<T>* _RightChild;
};
template<class T>
class BinaryTree
{
public:
BinaryTree()
:_root(NULL)
{}
BinaryTree(T* str)
{
_CreateTree(_root, str);
}
BinaryTree( BinaryTree<T>& bt)
{
_root = _CopyBinaryTree(bt._root);
}
BinaryTree& operator=(BinaryTree<T>& bt)
{
/*if (this != &bt) { _Destory(_root); _root = _CopyBinaryTree(bt._root); }*/
swap(_root, bt._root);
return *this;
}
~BinaryTree()
{
_Destory(_root);
}
void preOrder()
{
cout << "树的先序遍历:";
_preOrder(_root);
cout << endl;
}
void InOrder()
{
cout << "树的中序遍历:";
_InOreder(_root);
cout << endl;
}
void PostOrder()
{
cout << "树的后序遍历:";
_PostOrder(_root);
cout << endl;
}
void LevelOrder()
{
cout << "树的层序遍历:";
_LevelOrder(_root);
cout << endl;
}
int Size()
{
cout << "此树的节点数:";
return _Size(_root);
}
int Depth()
{
cout << "树的深度:";
return _Depth(_root);
}
void preOrder_NonRec() //非递归
{
cout << "树的先序遍历:";
_preOrder_NonRec(_root);
cout << endl;
}
void InOrder_NonRec()
{
cout << "树的中序遍历:";
_InOrder_NonRec(_root);
cout << endl;
}
void PostOrder_NonRec()
{
cout << "树的后序遍历:";
_PostOrder_NonRec(_root);
cout << endl;
}
protected:
void _Destory(BinaryTreeNode<T>*& root)
{
if (root)
{
_Destory(root->_LeftChild);
_Destory(root->_RightChild);
delete root;
root = NULL;
}
}
BinaryTreeNode<T>*& _CopyBinaryTree(BinaryTreeNode<T>*& root)
{
BinaryTreeNode<T>* CopyRoot = new BinaryTreeNode<T>(root->_value);
if (root->_LeftChild)
{
CopyRoot->_LeftChild = _CopyBinaryTree(root->_LeftChild);
}
if (root->_RightChild)
{
CopyRoot->_RightChild = _CopyBinaryTree(root->_RightChild);
}
return CopyRoot;
}
void _CreateTree(BinaryTreeNode<T>*& root, T*& str) //创建节点
{
if (*str != '#' && *str != '\0')
{
root = new BinaryTreeNode<T>(*str);
_CreateTree(root->_LeftChild, ++str);
_CreateTree(root->_RightChild, ++str);
}
}
void _preOrder(BinaryTreeNode<T>*& root) //先序遍历 跟,左,右
{
if (root)
{
cout << root->_value << " ";
_preOrder(root->_LeftChild);
_preOrder(root->_RightChild);
}
}
void _InOreder(BinaryTreeNode<T>*& root) //中序遍历 左,跟,右
{
if (root)
{
_InOreder(root->_LeftChild);
cout << root->_value << " ";
_InOreder(root->_RightChild);
}
}
void _PostOrder(BinaryTreeNode<T>*& root) //后序遍历 左,右,跟
{
if (root)
{
_PostOrder(root->_LeftChild);
_PostOrder(root->_RightChild);
cout << root->_value<<" ";
}
}
void _LevelOrder(BinaryTreeNode<T>*& root)
{
queue<BinaryTreeNode<T>*> q; //利用队列先进先出的特点
if (root)
{
q.push(root);
}
//队列中的数据为1 2 5 3 4 6
while (!q.empty())
{
BinaryTreeNode<T>* front = q.front();
cout << front->_value << " ";
q.pop();
if (front->_LeftChild != NULL)
{
q.push(front->_LeftChild); //插入左孩子
}
if (front->_RightChild != NULL)
{
q.push(front->_RightChild); //插入右孩子
}
}
}
int _Size(BinaryTreeNode<T>*& root)
{
if (root == NULL) //空树
{
return 0;
}
if (root->_LeftChild == NULL && root->_RightChild == NULL) //叶子节点
{
return 1;
}
return 1 + _Size(root->_LeftChild) + _Size(root->_RightChild);
}
int _Depth(BinaryTreeNode<T>*& root)
{
if (root == NULL)
{
return 0;
}
size_t LeftDepth = _Depth(root->_LeftChild);
size_t RightDepth = _Depth(root->_RightChild);
return 1 + (LeftDepth > RightDepth ? LeftDepth : RightDepth);
}
void _preOrder_NonRec(BinaryTreeNode<T>*& root)
{
stack<BinaryTreeNode<T>*> s;
if (root)
{
s.push(root);
}
while (!s.empty())
{
BinaryTreeNode<T>* top = s.top();
cout << top->_value << " ";
s.pop();
if (top->_RightChild)
{
s.push(top->_RightChild);
}
if (top->_LeftChild)
{
s.push(top->_LeftChild);
}
}
}
void _InOrder_NonRec(BinaryTreeNode<T>*& root)
{
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* cur = root;
while (cur || !s.empty())
{
while (cur) //将所有左节点存入栈中
{
s.push(cur);
cur = cur->_LeftChild;
}
if(!s.empty())
{
BinaryTreeNode<T>* top = s.top();
cout << top->_value<<" ";
s.pop();
cur = top->_RightChild;
}
}
}
void _PostOrder_NonRec(BinaryTreeNode<T>*& root)
{
stack<BinaryTreeNode<T>*> s;
BinaryTreeNode<T>* cur = root;
while (cur || !s.empty())
{
while (cur)
{
s.push(cur);
cur = cur->_LeftChild;
}
if (!s.empty())
{
BinaryTreeNode<T>* top = s.top();
cout << top->_value << " ";
s.pop();
if (!s.empty())
{
BinaryTreeNode<T>* prev = s.top();
//当prev->_RightChild = top时说明右节点已经输出
if (prev->_RightChild != top)
{
cur = prev->_RightChild; //插右子树
}
}
}
}
}
private:
BinaryTreeNode<T>* _root;
};
main.cpp
#include"BinaryTree.h"
void Test()
{
char* str = "123##4##56";
BinaryTree<char> bt(str);
bt.preOrder();
bt.InOrder();
bt.PostOrder();
bt.LevelOrder();
int size = bt.Size();
cout << size << endl;
int depth = bt.Depth();
cout << depth << endl;
bt.preOrder_NonRec();
bt.InOrder_NonRec();
bt.PostOrder_NonRec();
}
void Test1()
{
char* str = "123##4##56";
BinaryTree<char> bt(str);
bt.preOrder_NonRec();
BinaryTree<char> bt1(bt);
bt1.preOrder_NonRec();
bt1 = bt;
bt1.preOrder_NonRec();
}
int main()
{
Test1();
getchar();
return 0;
}