#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);
}
}
}