• 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;
    }
    


  • 相关阅读:
    2277 爱吃皮蛋的小明
    zoj2314 经典 无源汇有上下界最大流 并输出可行流
    [置顶] css3 befor after 简单使用 制作时尚焦点图相框
    [置顶] 程序员的奋斗史(二十八)——寒门再难出贵子?
    TaintDroid:智能手机监控实时隐私信息流跟踪系统(四)
    Activity切换效果(overridePendingTransition)
    Activity生命周期,状态保存恢复(经典)
    大二实习使用的技术汇总(上)
    Struts2配置RESULT中TYPE的参数说明
    关于程序动态库链接和运行时搜索路径设置的个人理解
  • 原文地址:https://www.cnblogs.com/jacksin/p/8830224.html
Copyright © 2020-2023  润新知