//感谢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);
}