//---------------------------------------------------------------------------
//author:putaoxiangqi
//实现树的抽象数据结构。重点在前序/中序/后序/层次遍历,共2*3+1=7种实现
//借助遍历可以递归/非递归的求数的高度,节点个数
//切记树的销毁,否则内存泄露
//----------------------------------------------------------------------------
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
typedef struct linknode {
int data;
struct linknode *left;
struct linknode * right;
}*ptree,tree;
class BITREE{
public:
BITREE(){}
~BITREE(){}
ptree create(int arr[],int n,int i);
void pre_print(ptree _tree) const ;
void pre_print2(ptree _tree) ;
void mid_print(ptree _tree) const ;
void mid_print2(ptree _tree) const ;
void postorder(ptree _tree) const;
void postorder2(ptree _tree) const ;
void level_print(ptree _tree) const;
int nodeNum(ptree _tree) const ;
ptree destroy(ptree _tree) const;
int deep(ptree _tree) ;
};
ptree BITREE::create(int arr[], int n , int i) {
if( i >= n ) {
return NULL;
}
ptree node = new tree;
node->data = arr[i];
node->left = create(arr,n,2*i+1);
node->right = create(arr,n,2*i+2);
return node;
}
void BITREE::pre_print(ptree _tree) const {
if( NULL==_tree ) {
return ;
}
cout<<_tree->data<<" ";
pre_print(_tree->left);
pre_print(_tree->right);
}
void BITREE::mid_print(ptree _tree) const {
if( NULL == _tree) {
return;
}
mid_print(_tree->left);
cout<<_tree->data<<" ";
mid_print(_tree->right);
}
void BITREE::postorder(ptree _tree) const {
if(NULL ==_tree) {
return ;
}
postorder(_tree->left);
postorder(_tree->right);
cout<<_tree->data<<" ";
}
void BITREE::pre_print2(ptree _tree) {
stack<ptree> mystack;
ptree node=_tree;
while(NULL!=node || !mystack.empty() ) {
while( NULL != node ) {
cout<<node->data<<" "; //打印
mystack.push(node); //进栈
node=node->left; //遍历左子树
}
if( !mystack.empty()) {
node=mystack.top(); //出栈
mystack.pop();
node=node->right; //遍历右子树
}
}
}
void BITREE::mid_print2(ptree _tree) const {
stack<ptree> mystack;
ptree node = _tree;
while( NULL!=node || !mystack.empty() ) {
while( NULL !=node ) {
mystack.push(node);
node=node->left;
}
if(!mystack.empty()) {
node = mystack.top();
mystack.pop();
cout<<node->data<<" ";
node=node->right;
}
}
}
void BITREE::postorder2(ptree _tree) const {
stack<ptree> mystack;
ptree node=_tree;
while(NULL!=node || !mystack.empty()) {
while(NULL!=node) {
mystack.push(node);
node=node->left;
}
ptree t=NULL;
while(!mystack.empty()) {
node =mystack.top();
if(node->right == t ) {
cout<<node->data<<" ";
mystack.pop();
t=node;
}
else {
node=node->right;
break;
}
}
}
}
void BITREE::level_print(ptree _tree) const {
queue<ptree> myqueue;
if(NULL == _tree) {
return ;
}
myqueue.push(_tree);
int sum_node_level=1;
int num=0;
while( !myqueue.empty() ) {
ptree node=myqueue.front();
myqueue.pop();
--sum_node_level;
cout<<node->data<<" ";
if(NULL != node->left ) {
myqueue.push(node->left);
num++;
}
if(NULL!= node->right ) {
myqueue.push(node->right);
num++;
}
if( 0 == sum_node_level){
cout<<""<<endl;
sum_node_level=num;
num=0;
}
}
return;
}
ptree BITREE::destroy(ptree _tree) const {
if( NULL==_tree ) {
return NULL ;
}
destroy(_tree->left);
destroy(_tree->right);
delete _tree;
_tree=NULL;
return NULL;
}
int BITREE::deep(ptree _tree) {
if(NULL==_tree) {
return 0;
}
int rh=deep(_tree->right);
int lh=deep(_tree->left);
return 1+(rh>lh?rh:lh);
}
int BITREE::nodeNum(ptree _tree) const {
if( NULL == _tree ) {
return 0;
}
return nodeNum(_tree->left)+nodeNum(_tree->right)+1;
}
#include"bitree.h"
int main() {
int arr[]={1,2,3,4,5,6,7,8};
int length = sizeof(arr)/sizeof(int);
int i=0;
BITREE * bitree = new BITREE;
ptree _tree = bitree->create(arr,length,i); //新建
bitree->pre_print(_tree); //递归前序
cout<<""<<endl;
bitree->pre_print2(_tree); //非递归前序
cout<<""<<endl;
cout<<"mid_print:"<<endl;
bitree->mid_print(_tree); //递归中序
cout<<""<<endl;
cout<<"mid_print2:"<<endl;
bitree->mid_print2(_tree); //非递归中序
cout<<""<<endl;
cout<<"postorder:"<<endl;
bitree->postorder(_tree); //递归后序
cout<<""<<endl;
cout<<"postorder2:"<<endl;
bitree->postorder(_tree); //非递归后序
cout<<""<<endl;
cout<<"deep is: "<<bitree->deep(_tree)<<endl;
cout<<"level print: "<<endl;
bitree->level_print(_tree);
cout<<"nodeNum:"<<bitree->nodeNum(_tree)<<endl; //递归求节点个数(非递归求解,可以借助非递归的遍历)
cout<<""<<endl;
_tree=bitree->destroy(_tree);
bitree->pre_print(_tree);
system("pause");
return 1;
}
层次遍历使用了列的数据结构
参考:
http://blog.csdn.net/hitwhylz/article/details/14223781
http://mingxinglai.com/cn/2014/07/traverse-binary-by-level/
http://blog.csdn.net/cxxsoft/article/details/935410