树的有关操作 (一)

//Bintree.h
#include<stdio.h>
#include<malloc.h>
typedef struct Binnode{//二叉树结点结构体
char data;
struct Binnode *lchild;
struct Binnode *rchild;
};
typedef Binnode *Bintree ;

typedef struct stack{ //二叉树结点栈
Bintree data[100];
int flag[100];
int top;
};

typedef struct queue{ //二叉树结点队列
Bintree data[30];
int front;
int rear;
};



/*******************************************/
/* 按照前序遍历建立二叉树 */
/*******************************************/

void Creat_Bintree(Bintree *root)
{
char ch;
if((ch=getchar())==' ')
{
*root=NULL;

}
else
{
*root=(Binnode*)malloc(sizeof(Binnode));
(*root)->data=ch;
Creat_Bintree(&(*root)->lchild);
Creat_Bintree(&(*root)->rchild);
}
}

/*******************************************/
/* 按照前序递归遍历二叉树 */
/*******************************************/

void Preorder1(Bintree t)
{
if(t!=NULL)
{
printf("%c",t->data);
Preorder1(t->lchild);
Preorder1(t->rchild);
}
}


/*******************************************/
/* 按照中序递归遍历二叉树 */
/*******************************************/

void Inorder1(Bintree t)
{
if(t!=NULL)
{
Inorder1(t->lchild);
printf("%c",t->data);
Inorder1(t->rchild);
}
}

/*******************************************/
/* 按照后序递归遍历二叉树 */
/*******************************************/

void Posorder1(Bintree t)
{
if(t!=NULL)
{
Posorder1(t->lchild);
Posorder1(t->rchild);
printf("%c",t->data);
}
}
/*******************************************/
/* 按照前序非递归遍历二叉树 */
/*******************************************/

void Preorder2(Bintree t)
{
Bintree pre=t;
stack s;
s.top=0;
printf("输出前序遍历序列:");
while(pre||s.top>0)
{
if(pre)
{
printf("%c",pre->data);
s.data[s.top++]=pre;
pre=pre->lchild;
}
else
{
pre=s.data[--s.top]->rchild;
}
}
printf("/n/n");
}

/*******************************************/
/* 按照中序非递归遍历二叉树 */
/*******************************************/

void Inorder2(Bintree t)
{
Bintree pre=t;
stack s;
s.top=0;
printf("输出中序遍历序列:");
while(pre||s.top>0)
{
if(pre)
{
s.data[s.top++]=pre;
pre=pre->lchild;
}
else
{
pre=s.data[--s.top];
printf("%c",pre->data);
pre=pre->rchild;
}
}
printf("/n/n");
}

/*******************************************/
/* 按照后序非递归遍历二叉树 */
/*******************************************/

void Posorder2(Bintree t)
{
stack s;
s.top=-1;
printf("输出后序遍历序列:");
while(t!=NULL||s.top!=-1)
{
while(t)
{
s.top++;
s.flag[s.top]=0;
s.data[s.top]=t;
t=t->lchild;

}
while(s.top>=0&&s.flag[s.top]==1)
{
t=s.data[s.top];
printf("%c",t->data);
s.top--;
}
if(s.top>=0)
{
t=s.data[s.top];
s.flag[s.top]=1;
t=t->rchild;
}
else
{
t=NULL;
}
}
printf("/n/n");
}


/*******************************************/
/* 按照层次遍历二叉树 */
/*******************************************/
void Levelorder(Bintree t)
{
queue q;
q.data[0]=t;
q.front=0;q.rear=1;
printf("层次遍历二叉树结果:");
while(q.front<q.rear)
{
if(q.data[q.front])
{
printf("%c",q.data[q.front]->data);
q.data[q.rear++]=q.data[q.front]->lchild;
q.data[q.rear++]=q.data[q.front]->rchild;
q.front++;
}
else
{
q.front++;
}
}
printf("/n/n");
}

#include"Bintree.h"
/*******************************************/
/* 递归法将二叉树的左右子树互换 */
/*******************************************/
void Exchange1(Bintree t)
{
Bintree temp;
if(t)
{
Exchange1(t->lchild);
Exchange1(t->rchild);
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
}
}
/*******************************************/
/* 非递归法将二叉树的左右子树互换 */
/*******************************************/
void Exchange2(Bintree t)
{
Bintree temp;
stack s;
s.top=0;
while(t||s.top)
{
if(t)
{
s.data[s.top++]=t;
temp=t->lchild;
t->lchild=t->rchild;
t->rchild=temp;
t=t->lchild;
}
else
{
t=s.data[--s.top]->rchild;
}

}
}
int main()
{
Bintree t;
Creat_Bintree(&t);
Exchange2(t);
Inorder2(t);
return 0;
}

#include"Bintree.h"
/*******************************************/
/* 递归法求叶子结点个数 */
/*******************************************/
int Leaves_Num1(Bintree t)
{
if(t)
{
if(t->lchild==NULL&&t->rchild==NULL)
{
return 1;
}
return Leaves_Num1(t->lchild)+Leaves_Num1(t->rchild);
}
return 0;
}

/*******************************************/
/* 非递归法求叶子结点个数 */
/*******************************************/
int Leaves_Num2(Bintree t)
{
int count=0;
stack s;
s.top=0;
while(t||s.top>0)
{
if(t)
{
s.data[s.top++]=t;
if(t->lchild==NULL&&t->rchild==NULL)
{

count++;
}
t=t->lchild;
}
else
{
t=s.data[--s.top]->rchild;
}
}
return count;
}


int main()
{
int count=0;
Bintree t;
Creat_Bintree(&t);
count=Leaves_Num2(t);
printf("该二叉树的叶子结点数为:%d/n",count);
return 0;
}

#include"Bintree.h"

/**********************************************/
/* 求一棵树的高度 */
/**********************************************/

int Depth(Bintree t)
{
int lh , rh ;
if( NULL == t )
{
return 0 ;
}
else
{
lh = Depth( t->lchild ) ;
rh = Depth( t->rchild ) ;
return ( lh > rh ? lh : rh ) + 1 ;
}
}

int main()
{
Bintree t ;
Creat_Bintree( &t ) ;
printf( "树的高度是%d/n" , Depth( t ) ) ;
return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值