源代码
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
template<class T>
class node{
public:
T element;
node<T> *leftnode,*rightnode;
node(){
leftnode=rightnode=NULL;
}
node(const T& element):element(element){
leftnode=rightnode=NULL;
}
~node(){
}
node(const T& element,node<T>* leftnode,node<T>* rightnode){
this->element=element;
this->leftnode=leftnode;
this->rightnode=rightnode;
}
};
template<class T>
class binaryTree{
public:
binaryTree(){
root=NULL;
}
binaryTree(node<T>* root){
this->root=root;
}
~binaryTree();
int height(node<T> *tree);
int size(node<T> *tree);
bool empty(){
return root==NULL;
}
void preOrder(node<T> *tree);
void inOrder(node<T> *tree);
void postOrder(node<T> *tree);
void postOrder2(node<T> *tree);
void levelOrder(node<T> *tree);
node<T>* getRoot(){
return root;
}
void visit(node<T> *tree){
cout<<tree->element<<" ";
}
private:
node<T> *root;
};
template<class T>
int Max(T a,T b){
return a>b?a:b;
}
template<class T>
int binaryTree<T>::height(node<T> *tree){
if(tree==NULL)return 0;
if(tree->leftnode==NULL&&tree->rightnode==NULL)return 1;
else return Max(height(tree->leftnode),height(tree->rightnode))+1;
}
template<class T>
int binaryTree<T>::size(node<T> *tree){
if(tree==NULL)return 0;
queue<node<T>* >q;
node<T> * currentNode;
q.push(tree);
int nodeSize = 0;
while (q.empty()==false)
{
currentNode = q.front();
q.pop();
nodeSize++;
if(currentNode->leftnode)q.push(currentNode->leftnode);
if(currentNode->rightnode)q.push(currentNode->rightnode);
}
return nodeSize;
}
template<class T>
void binaryTree<T>::preOrder(node<T> *tree){
if(tree==NULL)return ;
visit(tree);
preOrder(tree->leftnode);
preOrder(tree->rightnode);
}
template<class T>
void binaryTree<T>::inOrder(node<T> *tree){
if(tree==NULL)return ;
inOrder(tree->leftnode);
visit(tree);
inOrder(tree->rightnode);
}
template<class T>
void binaryTree<T>::postOrder(node<T> *tree){
if(tree==NULL)return ;
postOrder(tree->leftnode);
postOrder(tree->rightnode);
visit(tree);
}
template<class T>
void binaryTree<T>::postOrder2(node<T> *tree){
if(tree==NULL)return ;
stack<node<T> * > s;
node<T> *p = tree, *r = NULL;
while(!s.empty()||p){
while(p){
s.push(p);
p = p->leftnode;
}
if(!s.empty()){
p = s.top();
if (p->rightnode&&p->rightnode!=r){
p = p->rightnode;
}else{
s.pop();
visit(p);
r = p;
p = NULL;
}
}
}
}
template<class T>
void binaryTree<T>::levelOrder(node<T> *tree){
if(tree==NULL)return ;
queue<node<T>* >q;
node<T> * currentNode;
q.push(tree);
while (q.empty()==false)
{
currentNode = q.front();
q.pop();
visit(currentNode);
if(currentNode->leftnode)q.push(currentNode->leftnode);
if(currentNode->rightnode)q.push(currentNode->rightnode);
}
}
template<class T>
binaryTree<T>::~binaryTree(){
if(root!=NULL){
queue<node<T>* >q;
node<T> * currentNode;
q.push(root);
while (!q.empty()){
currentNode = q.front();
q.pop();
if(currentNode->leftnode)q.push(currentNode->leftnode);
if(currentNode->rightnode)q.push(currentNode->rightnode);
delete currentNode;
}
}
}
int preorder[]={4 ,1, 3, 2, 6, 5, 7};
int inorder[]={1, 2 ,3 ,4, 5, 6, 7};
node<int>* makeTree(int L1,int R1,int L2,int R2){
if(L1>R1)return NULL;
node<int> *bt;
int Data=preorder[L2];
bt = new node<int>(Data);
int p=L1;
while(inorder[p]!=Data)p++;
int nodecount=p-L1;
bt->leftnode=makeTree(L1,p-1,L2+1,L2+nodecount);
bt->rightnode=makeTree(p+1,R1,L2+nodecount+1,R2);
return bt;
}
stack<int> s1,s2;
void printPath(node<int> *tree){
if(tree==NULL)return;
s1.push(tree->element);
if(tree->leftnode==NULL&&tree->rightnode==NULL){
s2 = s1;
while(!s2.empty()){
if(s2.size()!=1){
cout<<s2.top()<<"->";
}else{
cout<<s2.top()<<"\n";
}
s2.pop();
}
}
printPath(tree->leftnode);
printPath(tree->rightnode);
s1.pop();
}
int main(){
int n = sizeof(preorder)/sizeof(int);
binaryTree<int> BT(makeTree(0,n-1,0,n-1));
cout<<"二叉树的高度:"<<BT.height(BT.getRoot());cout<<"\n";
cout<<"二叉树的节点个数:"<<BT.size(BT.getRoot());cout<<"\n";
cout<<"二叉树的前序遍历:";BT.preOrder(BT.getRoot());cout<<"\n";
cout<<"二叉树的中序遍历:";BT.inOrder(BT.getRoot());cout<<"\n";
cout<<"二叉树的后序遍历:";BT.postOrder(BT.getRoot());cout<<"\n";
cout<<"二叉树的后序遍历(非递归):";BT.postOrder2(BT.getRoot());cout<<"\n";
cout<<"二叉树的层次遍历:";BT.levelOrder(BT.getRoot());cout<<"\n";
cout<<"-----输出叶子节点到根节点的路径\n";
printPath(BT.getRoot());
}
输出结果
二叉树的高度:4
二叉树的节点个数:7
二叉树的前序遍历:4 1 3 2 6 5 7
二叉树的中序遍历:1 2 3 4 5 6 7
二叉树的后序遍历:2 3 1 5 7 6 4
二叉树的后序遍历(非递归):2 3 1 5 7 6 4
二叉树的层次遍历:4 1 6 3 5 7 2
-----输出叶子节点到根节点的路径
2->3->1->4
5->6->4
7->6->4
注
若利用中序与后序遍历创造一棵树
关键代码
bt->leftnode=makeTree(L1,p-1,L2,L2+nodecount-1);
bt->rightnode=makeTree(p+1,R1,L2+nodecount,R2-1);