二叉树的建立及遍历各种递归

递归简直太难了啊可怜可怜

 
#include<stdio.h>
#include<malloc.h> 

#define    OK       1
#define    ERROR    0
#define    TRUE     1
#define    FLASE    0
#define    OVERFLOW 2


typedef    int     Status;
#define    TElemType   char

typedef struct BiTNode{
	TElemType      data;
	struct BiTNode * lchild, * rchild;     //左右孩子指针
}BiTNode, * BiTree;

Status CreateBiTree(BiTree &T)
{//先序建立二叉树
	char ch;
	scanf("%c", &ch);
	if(ch==' ')
		T=NULL;
	else
	{
		if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
			exit(OVERFLOW);
		T->data=ch;
		CreateBiTree(T->lchild);         //构造左子树
		CreateBiTree(T->rchild);         //构造右子树
	}
	return OK;
}//CreateBiTree

Status PrintElement(TElemType e)
{
	printf("%c ", e);
	return OK;
}//相当于Visit函数

Status PreOrderTraverse(BiTree T, Status(*Visit)(TElemType e))        //先序遍历T的递归算法
{
	if(T)
	{
		if(Visit(T->data))
			if(PreOrderTraverse(T->lchild, Visit))
				if(PreOrderTraverse(T->rchild, Visit))
					return OK;
				return ERROR;
	}
	else return OK;
}//PreOrderTraverse



Status InOrderTraverse(BiTree T)  //中序递归遍历
 {  

    if(T)  
    {  
        InOrderTraverse(T->lchild);     //最左节点 
        printf("%c ",T->data);          
        InOrderTraverse(T->rchild);  
     }  
     return OK;  
} 

Status PostOrderTraverse(BiTree T)          //后续递归遍历
{
	if(T)
	{
		PostOrderTraverse(T->lchild);
		PostOrderTraverse(T->rchild);
		printf("%c ",T->data);
	}
	return OK;
}

int LeavesNum(BiTree T)     //递归法求叶子个数 
 {  
   if(T)  
    {  
         if((T->lchild==NULL) && (T->rchild==NULL))  
         {  
           return 1;  
         }  
        return (LeavesNum(T->lchild)+LeavesNum(T->rchild));  
    }  
    return 0;
} 

Status Exchange(BiTree T)  //递归法交换树的左右子树 
{  
      BiTree temp;  
      if(T)  
      {  
           Exchange(T->lchild);  
           Exchange(T->rchild);  
           temp = T->lchild;  
           T->lchild = T->rchild;  
           T->rchild = temp;  
       }
       return OK;
} 

int Depth(BiTree T)       //求树的高度
{
    int lh,rh;
    lh = rh = 0;
    BiTree p = T; 
    if(p==NULL)
    {
     return 0;
    }
    else
    {
       lh = Depth(p->lchild);
       rh = Depth(p->rchild);
       return(lh > rh ?  (lh+1):(rh+1));
    }
} 

int  Num_Of_Node(BiTree T)  //求树的结点数 
{  
   if(T==NULL)  
      return 0 ;  
  else  
       return Num_Of_Node(T->lchild)+Num_Of_Node(T->rchild)+1;  
} 

 
int main()
{
	 BiTree T;
	 printf("以先序序列建树:\n"); 
     CreateBiTree(T); 
     printf("原树的前、中、后序遍历如下:\n"); 
	 PreOrderTraverse(T, PrintElement);
	 printf("\n");
	 InOrderTraverse(T);
	 printf("\n");
	 PostOrderTraverse(T);
	 printf("\n");
     int leavesnum;
	 leavesnum = LeavesNum(T); 
	 printf("原树的叶子数为: %d\n", leavesnum);
     int depth;
     depth = Depth(T);
	 printf("原树的深度值为: %d\n", depth);
	 int num_of_node;
	 num_of_node = Num_Of_Node(T);
	 printf("原树的结点数为: %d\n", num_of_node);
	 
	       
	 Exchange(T);     //交换树的左右结点 
	 printf("交换左右结点后的前中后序遍历为:\n"); 
	 PreOrderTraverse(T, PrintElement);
	 printf("\n");
	 InOrderTraverse(T);
	 printf("\n");
	 PostOrderTraverse(T);
	 printf("\n");
	 leavesnum = LeavesNum(T); 
	 printf("交换后的叶子数为: %d\n", leavesnum);
	 depth = Depth(T);
	 printf("交换后树的深度为: %d\n", depth);
     num_of_node = Num_Of_Node(T);
	 printf("交换后的结点数为: %d\n", num_of_node);
	 system("pause");
	 return 0;
}


 

posted on 2011-11-24 21:42  java课程设计例子  阅读(251)  评论(0编辑  收藏  举报