03-树2 List Leaves (25分)(数据结构)(c语言实现)

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 N (≤10) which is the total number of nodes in the tree – and hence the nodes are numbered from 0 to N−1. Then N 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>
#define Max 10
#define Null -1
typedef struct Node *PtrNode;
typedef struct PNode *Queue;
struct Node
{
	int data;
	PtrNode next;
};
struct PNode
{
	PtrNode front,rear;
};
struct TreeNode
{
	char element;
	int left;
	int right;
}T[Max];

主函数窗口:

int main()
{
    int root;
    root=CreatTree();
    Travel(root);
    return 0;
}

接着是创造树:

int CreatTree()
{
	int N;
	int i,root;
	int check[Max];
	char c1,c2;
	scanf("%d",&N);
	for(i=0;i<N;i++)
		T[i].element=i;
	if(N)
	{
		for(i=0;i<N;i++)
			check[i]=0;
		for(i=0;i<N;i++)
		{
			scanf(" %c %c",&c1,&c2);
			if(c1!='-')
			{
				T[i].left=c1-'0';
				check[T[i].left]=1;
			}
			else 
			{
				T[i].left=Null;
			}
			if(c2!='-')
			{
				T[i].right=c2-'0';
				check[T[i].right]=1;
			}
			else 
			{
				T[i].right=Null;
			}
			
		}

	}
	else
		return Null;
	for(i=0;i<N;i++)
	{
		if(!check[i])
			break;
	}
	root=i;
	return root;
}

关于队列的一系列操作:

int DeleteQ(Queue Q)//出队列
{
	PtrNode frontcell;
	int 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 CreatQ()//创造队列
{
	Queue Q;
	Q=(Queue)malloc(sizeof(struct PNode));
	Q->front=Q->rear=NULL;
	return Q;
}
void AddQ(Queue Q,int item)//入队
{
	PtrNode Node;
	Node=(PtrNode)malloc(sizeof(struct Node));
	Node->data=item;
	Node->next=NULL;
	if(Q->front==NULL)
	{
		Q->front=Node;
		Q->rear=Node;
	}
	else
	{
		Q->rear->next=Node;
		Q->rear=Node;
	}

}
int IsEmpty(Queue Q)//判断队列是否为空
{
	if(Q->front==NULL)
		return 1;
	else return 0;
}

循环查找叶子结点:

void Travel(int root)
{
    int m;
	int flag=1;
	if(root==Null)
		return;
	Queue Q;
	Q=CreatQ();
	AddQ(Q,root);
	while(!IsEmpty(Q))
	{
        m=DeleteQ(Q);
		if(T[m].left==Null&&T[m].right==Null)
		{
			if(flag)
			{
				printf("%d",T[m].element);
                flag=0;
			}
			else
				printf(" %d",T[m].element);
		}
		 if(T[m].left!=Null)
		{
			AddQ(Q,T[m].left);
		}
		 if(T[m].right!=Null)
			AddQ(Q,T[m].right);
	}
}

函数总代码如下;

#include<stdio.h>
#include<stdlib.h>
#define Max 10
#define Null -1
typedef struct Node *PtrNode;
typedef struct PNode *Queue;
struct Node
{
	int data;
	PtrNode next;
};
struct PNode
{
	PtrNode front,rear;
};
struct TreeNode
{
	char element;
	int left;
	int right;
}T[Max];
int CreatTree()
{
	int N;
	int i,root;
	int check[Max];
	char c1,c2;
	scanf("%d",&N);
	for(i=0;i<N;i++)
		T[i].element=i;
	if(N)
	{
		for(i=0;i<N;i++)
			check[i]=0;
		for(i=0;i<N;i++)
		{
			scanf(" %c %c",&c1,&c2);
			if(c1!='-')
			{
				T[i].left=c1-'0';
				check[T[i].left]=1;
			}
			else 
			{
				T[i].left=Null;
			}
			if(c2!='-')
			{
				T[i].right=c2-'0';
				check[T[i].right]=1;
			}
			else 
			{
				T[i].right=Null;
			}
			
		}

	}
	else
		return Null;
	for(i=0;i<N;i++)
	{
		if(!check[i])
			break;
	}
	root=i;
	return root;
}
int DeleteQ(Queue Q)
{
	PtrNode frontcell;
	int 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 CreatQ()
{
	Queue Q;
	Q=(Queue)malloc(sizeof(struct PNode));
	Q->front=Q->rear=NULL;
	return Q;
}
void AddQ(Queue Q,int item)
{
	PtrNode Node;
	Node=(PtrNode)malloc(sizeof(struct Node));
	Node->data=item;
	Node->next=NULL;
	if(Q->front==NULL)
	{
		Q->front=Node;
		Q->rear=Node;
	}
	else
	{
		Q->rear->next=Node;
		Q->rear=Node;
	}

}
int IsEmpty(Queue Q)
{
	if(Q->front==NULL)
		return 1;
	else return 0;
}
void Travel(int root)
{
    int m;
	int flag=1;//输出格式标志符
	if(root==Null)
		return;
	Queue Q;
	Q=CreatQ();
	AddQ(Q,root);//根节点入队
	while(!IsEmpty(Q))
	{
        m=DeleteQ(Q);//出队一个元素
		if(T[m].left==Null&&T[m].right==Null)//左右子叶都为空
		{
			if(flag)//第一个元素打印,前面不需要空格
			{
				printf("%d",T[m].element);
                flag=0;
			}
			else
				printf(" %d",T[m].element);
		}
		 if(T[m].left!=Null)//左子树不为空,则存入队列
		{
			AddQ(Q,T[m].left);
		}
		 if(T[m].right!=Null)//右子树不为空,则存入队列
			AddQ(Q,T[m].right);
	}
}
int main()
{
    int root;
    root=CreatTree();
    Travel(root);
    return 0;
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值