#include <iostream>
#include <cassert>
#include <queue>
#include <stack>
using namespace std;
template <class T>
struct TreeNode
{
TreeNode(const T& value = T())
:_value(value)
,_lchild(0)
,_rchild(0)
{}
T _value;
TreeNode<T>* _lchild;
TreeNode<T>* _rchild;
};
template <class T>
class BinaryTree
{
public:
typedef TreeNode<T> Node;
BinaryTree()
:_root(NULL)
{}
BinaryTree(const T* a,size_t size,const T& invalid)
{
assert(a);
size_t index = 0;
_root = CreatTree(a,size,invalid,index);
}
BinaryTree(const BinaryTree<T>& b)
{
_root = Copy(b._root);
}
BinaryTree& operator=(const BinaryTree<T>& b)
{
if (this != &b)
{
Node* tmp = Copy(b._root);
Destroy(_root) ;
_root = tmp;
}
return *this;
}
~BinaryTree()
{
if (NULL != _root)
{
Destroy(_root);
_root = NULL;
}
}
protected:
Node* CreatTree(const T* a,size_t size,const T& invalid,size_t& index)
{
assert(a);
Node* root = NULL;
if (a[index] != invalid && index < size)
{
root = new Node(a[index]);
root->_lchild = CreatTree(a,size,invalid,++index);
root->_rchild = CreatTree(a,size,invalid,++index);
}
return root;
}
Node* Copy(Node* root)
{
Node* tmp = NULL;
if(root)
{
tmp = new Node(root->_value);
tmp->_lchild = Copy(root->_lchild);
tmp->_rchild = Copy(root->_rchild);
}
return tmp;
}
void Destroy(Node*& root)
{
if(root)
{
Destroy(root->_rchild);
Destroy(root->_lchild);
delete root;
root = NULL;
}
}
private:
Node* _root;
};
2、遍历二叉树
二叉树的遍历方式一共分为四种:先序遍历、中序遍历、后序遍历和层序遍历,而前三种遍历又分为递归遍历和非递归遍历,层序遍历则是借助队列先进先出的特性来辅助完成。
【递归遍历二叉树】
void preorder(Node* root)
{
if (root)
{
cout<<root->_value<<" ";
preorder(root->_lchild);
preorder(root->_rchild);
}
}
void inorder(Node* root)
{
if (root)
{
preorder(root->_lchild);
cout<<root->_value<<" ";
preorder(root->_rchild);
}
}
void postorder(Node* root)
{
if (root)
{
preorder(root->_lchild);
preorder(root->_rchild);
cout<<root->_value<<" ";
}
}
void levelorder(Node* root)
{
queue<Node*> q;
if (root)
{
q.push(root);
}
while(!q.empty())
{
Node* front = q.front();
q.pop();
cout<<front->_value<<" ";
if (front->_lchild)
{
q.push(front->_lchild);
}
if (front->_rchild)
{
q.push(front->_rchild);
}
}
}
二叉树的非递归遍历:
public:
void PreOrder()
{
cout<<"先序遍历:";
preorderR(_root);
cout<<endl;
}
void InOrder()
{
cout<<"中序遍历:";
inorderR(_root);
cout<<endl;
}
void PostOrder()
{
cout<<"后序遍历:";
postorderR(_root);
cout<<endl;
}
void LevelOrder()
{
cout<<"层序遍历:";
levelorder(_root);
cout<<endl;
}
算法:
void preorderR(Node* root)
{
Node* cur = root;
stack<Node*> s;
while (!s.empty() || cur)
{
while(cur)
{
cout<<cur->_value<<" ";
s.push(cur);
cur = cur->_lchild;
}
Node* top = s.top();
s.pop();
cur = top->_rchild;
}
cout<<endl;
}
void inorderR(Node* root)
{
Node* cur = root;
stack<Node*> s;
while(!s.empty() || cur)
{
while(cur)
{
s.push(cur);
cur = cur->_lchild;
}
Node* top = s.top();
cout<<top->_value<<" ";
s.pop();
cur = top->_rchild;
}
cout<<endl;
}
void postorderR(Node* root)
{
Node* cur = root;
Node* prev = NULL;
stack<Node*> s;
while(!s.empty() || cur)
{
while(cur)
{
s.push(cur);
cur = cur->_lchild;
}
Node* top = s.top();
if (top->_rchild==NULL || top->_rchild==prev)
{
cout<<top->_value<<" ";
prev = top;
s.pop();
}
else
{
cur = top->_rchild;
}
}
cout<<endl;
}
void levelorder(Node* root)
{
queue<Node*> q;
if (root)
{
q.push(root);
}
while(!q.empty())
{
Node* front = q.front();
q.pop();
cout<<front->_value<<" ";
if (front->_lchild)
{
q.push(front->_lchild);
}
if (front->_rchild)
{
q.push(front->_rchild);
}
}
}