初探二叉树之动态创建,前.中,后序遍历的递归及非递归实现,层次遍历,树状输出,叶节点及节点总数的统计

//感谢CSDN共享博文!(兵主作品)
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

typedef struct Node  {
 char  data;
  struct Node *LChild;
  struct Node *RChild;
}BiNode ,*BiTree;
BiTree stack[MAX];
int first=-1;

BiTree CreatBiTree( BiTree *p ){//递归创建二叉树时涉及内存分配,使用二级指针作为形参
 char ch;
 char buf[256];
 fgets(buf,sizeof(buf),stdin);//stdin,标准输入流
 fflush(stdin);//刷新缓存
 sscanf(buf,"%c",&ch); 
 if(ch!='#'){
  *p=(BiTree )malloc(sizeof(BiNode)); 
  (*p)->data=ch; 
  printf("->LChild:\n");
  CreatBiTree(&((*p)->LChild));
  printf("->RChild:\n");
  CreatBiTree(&((*p)->RChild));
 }
 else
  *p=NULL; 
}

void show(BiTree p,int layer){//实为逆中序(RNL)遍历
int i;
if(p==NULL){
return ;
}
show(p->RChild,layer+1);
for(i=0;i<layer;i++){
printf(" ");
}
printf("%c\n",p->data);
show(p->LChild,layer+1);

}
void push(BiTree p){
stack[++first]=p;
}
BiTree pop(){
return stack[first--];
}
/*
void PreOrderTraverse(BiTree p){//NLR
 if(p){
  printf("%5c",p->data);
  PreOrderTraverse(p->LChild);
  PreOrderTraverse(p->RChild);  
 }
}*/

 void PreOrderTraverse(BiTree p){//非递归
while(p||first!=-1){
while(p){
printf("%c",p->data);
push(p);
p=p->LChild;
}
if(first!=-1){
   p=pop();
p=p->RChild;
}
}
 }

 /*
void InOrderTraverse(BiTree p){//LNR
 if(p){
  InOrderTraverse(p->LChild);
  printf("%5c",p->data);
  InOrderTraverse(p->RChild);
 }
 
}*///非递归
 
void InOrderTraverse(BiTree p){  
 while(p||first!=-1){
  while(p){
   push(p);
   p=p->LChild;
  }
  if(first!=-1){
   p=pop();
   printf("%5c",p->data);
   p=p->RChild;
  }
 }
}
/*
void PostOrderTraverse(BiTree p){//LRN 
 if(p){
  PostOrderTraverse(p->LChild);
  PostOrderTraverse(p->RChild);
  printf("%5c",p->data);
 }
}*///非递归
 void PostOrderTraverse(BiTree p){
BiTree last;//若last==NULL,当右子树为空,会出错
push(p);
while(first!=-1){
p=pop();
if(last==p->LChild||last==p->RChild){
printf("%5c",p->data);
last=p;
}
else if(p->LChild||p->RChild){//p非叶节点
push(p);
if(p->RChild)
push(p->RChild);
if(p->LChild)
push(p->LChild);
}
else{
printf("%5c",p->data);
last=p;
}
}
 }

typedef struct queue{//利用队列,头出尾进完成层次遍历
BiTree data[MAX];
int front;
int rear;
}queue;

void enter(queue *q,BiTree p){
if(q->rear==MAX){
printf("the queue is full\n");
}
else{
q->data[q->rear]=p;
q->rear++;
}
}
BiTree del(queue *q){
if(q->front==q->rear){
return NULL;
}
else{
q->front++;//使头指针向下,准备送出节点
return q->data[q->front-1];
}
}

int  LevelOrderTraverse(BiTree p){
queue q;
q.front=q.rear=0;
if(!p){
printf("the tree is empty\n");
return 0;
}
enter(&q,p);
while(q.front!=q.rear){
p=del(&q);
printf("%5c",p->data);
if(p->LChild){
enter(&q,p->LChild);
}
if(p->RChild){
enter(&q,p->RChild);
}
}
}
int CountNode(BiTree p){
if(p){
return(CountNode(p->LChild)+CountNode(p->RChild)+1);
}
return 0;
}
int CountLeafNode(BiTree p){
if(p==NULL)
return 0;
if(p->LChild==NULL&&p->RChild==NULL)
return 1;
return CountLeafNode(p->LChild)+CountLeafNode(p->RChild);
}


main(){
 int c,layer=1;
 BiTree root=NULL; 
 printf("creat:(input '#' will exit)\n");

 CreatBiTree(&root); 
 show(root,layer);

 printf("PreOrderTraverse:\n");
 PreOrderTraverse(root);  
 printf("\nInOrderTraverse:\n");
 InOrderTraverse(root);
 printf("\nPostOrderTraverse:\n");
 PostOrderTraverse(root);
 printf("\nLeafOrderTraverse:");
 LevelOrderTraverse(root); 

 c=CountNode(root);
 printf("\nthe node number is:%5d\n",c);
 c=CountLeafNode(root);
 printf("the LeafNode number is:%5d\n",c);  
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值