数据结构:树(C语言)

#include "stdio.h"
#include <stdio.h> 
#include "stdlib.h"
#include "string.h"
#include <iostream>
#define NULL 0
//二叉链表的存储结构定义
typedef char DataType;
   
typedef struct BiNode
{
    DataType data;
    struct BiNode *lchild, *rchild;
} BiNode,*BiTree;
  BiTree root;

//建立二叉链表


int  CreatBiTree(BiTree &T)
{
	char c;
	scanf("%c",&c);                           /*输入结点的数据信息*/
    if (c=='#')  T = NULL;                     /*递归结束,建立一棵空树*/
    else {
         T = (BiNode *)malloc(sizeof(BiNode));    
         T->data = c;
          CreatBiTree (T->lchild);             /*递归建立左子树*/
          CreatBiTree (T->rchild);             /*递归建立右子树*/
    	}
	}

typedef BiTree ElementType;
typedef struct SNode *Stack;  
typedef struct SNode{  
    ElementType Data;  
    struct SNode *Next;  
}SNode;

//堆栈的建立  
Stack CreateStack(){  
    Stack S;  
    S=(Stack)malloc(sizeof(struct SNode));  
    S->Next=NULL;  
    return S;  
}  
//判断是否空  
int IsEmpty(Stack S){  
    return(S->Next==NULL);  
}  
//Push操作  
void Push(ElementType item,Stack S){  
    struct SNode *TmpCell;  
    TmpCell=(struct SNode *)malloc(sizeof(struct SNode));  
    TmpCell->Data=item;  
    TmpCell->Next=S->Next;  
    S->Next=TmpCell;  
}  
 
//Pop操作
ElementType Pop(Stack S){  
    struct SNode *FirstCell;  
    ElementType TopElem;  
    if(IsEmpty(S)){  
        printf("堆栈空\n");  
        return NULL;  
    }else{  
        FirstCell=S->Next;  
        S->Next=FirstCell->Next;  
        TopElem=FirstCell->Data;  
        free(FirstCell);  
        return TopElem;  
    }  
}




//求二叉树中叶子结点个数
void CountLeaf0 (BiTree T,  int& count){
   if ( T ) {
      if ((!T->lchild)&& (!T->rchild))
         count++;      // 对叶子结点计数
      CountLeaf0( T->lchild, count);  
      CountLeaf0( T->rchild, count); 
   } 
} 

//求二叉树中结点个数为一的个数 
void CountLeaf1 (BiTree T,  int& count){
   if ( T ) {
      if (( (!T->lchild)&&(T->rchild) )||( (!T->rchild)&&(T->lchild) ))
         count++;     // 对结点为1的节点计数
      CountLeaf1( T->lchild, count);  
      CountLeaf1( T->rchild, count); 
   } 
} 


//求二叉树中结点个数为二的个数 
void CountLeaf2 (BiTree T,int& count){
   if ( T ) {
      if ((T->lchild)&&(T->rchild))
         count++;     // 对结点为2的节点计数
      CountLeaf2( T->lchild, count);  
      CountLeaf2( T->rchild, count); 
   } 
} 

//求二叉树的高度 
int Depth (BiTree T ){ // 返回二叉树的深度
	int depthval,depthLeft,depthRight;
   if ( !T )  
   depthval = 0;
   else   {
     depthLeft = Depth( T->lchild );
     depthRight= Depth( T->rchild );
     depthval = 1 + (depthLeft > depthRight  ?
                               depthLeft : depthRight);
   }    return depthval;
}


//摧毁二叉树 
void DestroyBiTree(BiTree &T)
{
    if (T) return;
    else {
        DestroyBiTree(T->lchild);
        DestroyBiTree(T->rchild);
        free(T);
    }
}


  //利用栈先序遍历
 void PreOderTraversal(BiTree BT){
	 BiTree T=BT;
	 Stack S=CreateStack();
	 while(T||!IsEmpty(S)){
		 while(T){ 
			 printf("%c",T->data);
			 Push(T,S);
			 T=T->lchild;
		 }
			 T=Pop(S);
			 T=T->rchild;
	 }
 };
 
 //利用栈中序遍历
 void InOderTraversal(BiTree BT){
	 BiTree T=BT;
	 Stack S=CreateStack();
	 while(T||!IsEmpty(S)){
		 while(T){
			 Push(T,S);
			 T=T->lchild;
		 }
			 T=Pop(S);
			 printf("%c",T->data);
			 T=T->rchild;
	 }
 };
 
  //利用栈后序遍历
 void PostOderTraversal(BiTree BT){
	 BiTree T=BT;
	 BiTree TempT=NULL;//Temp记录被检查的节点的右儿子
	 Stack S=CreateStack();
	 while(T||!IsEmpty(S)){
		 while(T){
			 Push(T,S);
			 T=T->lchild;
		 }
		 T=Pop(S);
		 Push(T,S);//相当于返回了栈顶元素
		  // 当前节点的右孩子如果为空或者已经被访问,则访问当前节点    
	    if(T->rchild==NULL||T->rchild==TempT){
            printf("%c",T->data);
			TempT=T;
			Pop(S);
			T=NULL;
		}
		 // 否则访问右孩子 
		else
		    T=T->rchild;			
	 }
 }
main()
{
	printf("请输入您先建立的二叉树\n"); 
	BiTree T;
	CreatBiTree(T);
	printf("创建成功");
	printf("请输入您想输入的操作\n1 先序遍历\n2 中序遍历\n");
	printf("3 后序遍历\n4 叶子节点数\n5 结点度数为1\n6 ");
	printf("结点度数为2\n7 树的高度\n8 清空一棵树\n9 操作台\n");
	printf("\n");
	int G;
	do{
			scanf("%d",&G);			
			if(G==1){
					printf("你建立的树先序遍历为:\n");
					PreOderTraversal(T);
					printf("\n"); 
					} 
					
		else if(G==2){
					printf("你建立的树中序遍历为:\n");
					InOderTraversal(T);
					printf("\n");
					}
					
		else if(G==3){
					printf("你建立的树后序遍历为:\n");
					PostOderTraversal(T);
					printf("\n");
					}			
					
		else if(G==4){
					int l=0; 
					CountLeaf0(T,l);
					printf("你建立的树叶子节点数有%d个:\n",l);
					printf("\n");
					}
					
		else if(G==5){
					int j=0; 
					CountLeaf1(T,j);
					printf("你建立的树中结点度数为1有%d个 :\n",j);
					printf("\n");
					}
		else if(G==6){
					int w=0; 
					CountLeaf2(T,w);
					printf("你建立的树中结点度数为2有%d个 :\n",w);
					printf("\n");
					}
		else if(G==7){
					int t= Depth(T);
					printf("你建立的树的高度为%d :\n",t);
					printf("\n");
					}
		else if(G==8){
					DestroyBiTree(T);
					printf("清除成功\n");
					printf("\n"); 
					}
		else if(G==9){
					printf("请输入您想输入的操作\n1 先序遍历\n2 中序遍历\n");
					printf("3 后序遍历\n4 叶子节点数\n5 结点度数为1\n6 ");
					printf("结点度数为2\n7 树的高度\n8 清空一棵树\n9 操作台\n");
					printf("\n");
					}															
	}while(G);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值