/***************** WZ ASUST 2016
二叉樹 的結(jié)點計算
配圖 棧實現(xiàn)非遞歸 棧內(nèi)元素變化圖
******************/
#include
#include
#include
#include
using namespace std;
typedef struct BinNode{
char data;
bool flag; //后序非遞歸中的標(biāo)記
struct BinNode *lchild,*rchild;
}BinNode,*BinTree;
//按先序創(chuàng)建樹
// char ss[]="12##3##";
char ss[]="124##x##3y##5##";
//char ss[]="124###3#5##";
/*************************
* 1
* 2 3
* 4 5
*************************/
int i=0;
void creatTree(BinTree &T)
{
char c;
// cin>>c;
c=ss[i++];
if(c=='#') T=NULL;
else{ // T=(BinTree)malloc(sizeof(BinNode));
T=new BinNode; T->data=c;
creatTree(T->lchild);
creatTree(T->rchild);
}
}
void visit(BinTree T)
{
if(T->data!='#') cout<data<<" ";
}
void preOrder(BinTree T) //前序遞歸
{
if(T)
{
visit(T);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void inOrder(BinTree T) //中序遞歸
{
if(T)
{
inOrder(T->lchild);
visit(T);
inOrder(T->rchild);
}
}
void postOrder(BinTree T) //后序遞歸
{
if(T)
{
postOrder(T->lchild);
postOrder(T->rchild);
visit(T);
}
}
void preOrderUnrec(BinTree T) //非遞歸前序遍歷
{
BinTree p; p=T;
stack s;
while(p || !s.empty())
{
while(p)
{
visit(p);
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->rchild;
}
}
}
void inOrderUnrec(BinTree T) ////非遞歸中序遍歷
{
BinTree p; p=T;
stack s;
while(p || !s.empty())
{
while(p)
{
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
visit(p);
s.pop();
p=p->rchild;
}
}
}
void postOrderUnrec1(BinTree T) //非遞歸后序遍歷 這個思路更好理解一些,下面有解釋
{
BinTree p,cur; p=T;
stack s; s.push(p);
BinTree pre=NULL;
while(!s.empty())
{
cur=s.top();
if((cur->lchild==NULL && cur->rchild==NULL)||((pre!=NULL)&&(pre==cur->lchild || pre==cur->rchild)))
{
visit(cur);
s.pop();
pre=cur;
}
else
{
if(cur->rchild)
s.push(cur->rchild);
if(cur->lchild)
s.push(cur->lchild);
}
}
}
void postOrderUnrec2(BinTree T) //非遞歸后序遍歷
{
BinTree cur,p; p=T; p->flag=false;
stack s; s.push(p);
while(!s.empty())
{
cur=s.top();
if(cur->flag==true)
{
visit(cur);
s.pop();
}
else
{
if(cur->rchild){
cur->rchild->flag=false;
s.push(cur->rchild);
}
if(cur->lchild){
cur->lchild->flag=false; // 左右節(jié)點為NULL時,開始要出棧
s.push(cur->lchild);
}
cur->flag=true; //左右子節(jié)點入棧后,父節(jié)點標(biāo)記為TRUE
}
}
}
void postOrderUnrec3(BinTree T)
{
BinTree cur,p; p=T;
queue q; q.push(p);
while(!q.empty())
{
cur=q.front(); visit(cur); q.pop();
if(cur->lchild!=NULL) q.push(cur->lchild);
if(cur->rchild!=NULL) q.push(cur->rchild);
}
}
void postOrderUnrec4(BinTree T) //非遞歸后序遍歷 不宜用帶計數(shù)器實現(xiàn)
{
BinTree cur,p; p=T;
BinTree q[10];
int f=0;int r=1;
while(flchild!=NULL)q[r++]=p->lchild;
if(p->rchild!=NULL)q[r++]=p->rchild;
if(r%2==1)f++;//cout<data=c; }
BinTree cur=T;
queue q; q.push(cur);
while(!q.empty())
{
cur=q.front(); q.pop();
if(c=='#') cur->lchild=NULL;
else { cur->lchild=new BinNode; T->data=c; q.push(cur->lchild);}
if(c=='#') cur->rchild=NULL;
else { cur->rchild=new BinNode; T->data=c; q.push(cur->rchild);}
}
}
void print_as_tree(BinTree T,int lay)
{
if(T)
{
print_as_tree(T->rchild,lay+1);
for(int j=0;jdata<lchild,lay+1);
}
else return;
}
size_t count_on_klay(BinTree root,size_t k)
{
//假設(shè)參數(shù)合法;
if(NULL==root) return 0;
if(k==1) return 1;
return count_on_klay(root->lchild,k-1)+count_on_klay(root->rchild,k-1);
}
size_t count_to_klay(BinTree root,size_t k)
{
//假設(shè)參數(shù)合法;
size_t count=0;
for(int i=1;i<=k;i++)
count+=count_on_klay(root,i);
return count;
}
int main()
{
BinTree T;
creatTree(T);
preOrder(T);cout<
當(dāng)前名稱:小代碼二叉樹非遞歸與結(jié)點計算
網(wǎng)頁地址:http://weahome.cn/article/igcodj.html