List leaves

来源:陈越《数据结构》

03-树2 List Leaves   (25分)

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer NN (\le 1010) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1N1. Then NN lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

Sample Output:

4 1 5
#include <stdio.h>  
#include<stdlib.h>
/*
	Name: List leaves
	Copyright: 
	Author: demosees
	Date: 09/04/17 20:17
	Description: list the leaves of trees  and creata a queue
*/


#define MaxTree 10  
#define Null -1  
typedef int Tree ;  
typedef char  ElementType ;
typedef struct Node *PtrToNode;
typedef int ElementType1;
typedef PtrToNode Position;
typedef struct QNode *Queue;
struct Node { /* 队列中的结点 */
    ElementType1 Data;
    PtrToNode Next;
};

 
struct QNode {
    Position Front, Rear;  /* 队列的头、尾指针 */
};

struct TreeNode  
{  
    ElementType Element ;  /*静态链表树*/ 
     Tree Left ;  
    Tree Right ;  
}TT[MaxTree];

Tree BuildeTree(struct TreeNode T[])  
{  
    /*建立静态链表树*/ 
    int i,N,check[MaxTree];  
    char cl,cr;  
    int Root;  
    scanf("%d",&N); 
	for( i=0;i<N;i++) T[i].Element=i; 
  
    if(N!=0)  
    {  
        for( i=0;i<N;i++) check[i]=0;  
        for( i=0;i<N;i++)  
        {  
            scanf(" %c %c",&cl,&cr) ;
  
            if(cl!='-')  
            {  
                T[i].Left=cl-'0' ;  
                check[T[i].Left]=1 ;  
            }  
            else T[i].Left=Null ;  
  
            if(cr!='-')  
             {  
                 T[i].Right =cr-'0' ;  
                 check[T[i].Right]=1 ;  
             }  
            else T[i].Right=Null ;  
        }  
    }  
    else  
        return  Null ;  
  
    for(i=0;i<N;i++ )  
    {  
        if(check[i]==0 )  
        break ;  
    }  
    Root=i ;  
  
    return  Root ;  
}

 
ElementType1 DeleteQ( Queue Q )
{  /*出队列*/ 
    Position FrontCell; 
    ElementType1 m;
     FrontCell = Q->Front;
        if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */
            Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */
        else                     
            Q->Front = Q->Front->Next;
        m = FrontCell->Data;
        free( FrontCell );  /* 释放被删除结点空间  */
        return m;
}
Queue  CreatQueue()
{   
   /*建立一个新的队列*/ 
    Queue Q;
    Q=(Queue)malloc(sizeof(struct QNode));
    Q->Front=NULL;
	Q->Rear=NULL;
	return Q;
}
void AddQ( Queue Q ,int m)
{
  /*入队列*/ 
	PtrToNode que;
	que=(PtrToNode)malloc(sizeof (struct Node));
	que->Data=m;
    que->Next=NULL;  
    if (Q->Front==NULL){ 
      	 Q->Front=que;
	     Q->Rear=que;
       }
	 else { 
	   Q->Rear->Next=que;
	   Q->Rear=que; 
       }
}
int IsEmpty(Queue Q)
{
   /*判断队首或队尾任一个指针是否为空即可*/
   if(Q->Front==NULL)
     return 1;
   else
    return 0;
}

void Travel(Tree Root)
{
	/*层序遍历输出叶子节点*/ 
	 int flag=1;
	 Queue Q;
	 Tree T;
	 if (Root==Null)
	   return; 
	 Q=CreatQueue();  
	 AddQ(Q,Root);
	 while(!IsEmpty(Q)){
	    T=DeleteQ(Q);
	    /*判定是否为叶子节点*/ 
	    if(TT[T].Left==Null&&TT[T].Right==Null)
	      if (flag)
	      {	     
	       printf("%d",TT[T].Element);
	       flag=0;
		  }
	    else
	    	printf(" %d",TT[T].Element);
	    
		if(TT[T].Left!=Null)
		   AddQ(Q,TT[T].Left); 
		if(TT[T].Right!=Null)
		   AddQ(Q,TT[T].Right);		   
	}
}
int main()
{
	Tree Root;
	Root=BuildeTree(TT);
	
	Travel(Root);
 return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值