PTA_07_03_树2_list leaves(详细讲解)

PTA_07_03_树2_list leaves

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

给定一棵树,你应该按照从上到下,从左到右的顺序列出所有的叶子。

输入规格

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.

每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(≤10),这是树中的节点总数——因此节点编号从0到N−1。接下来是N行,每一行对应一个节点,并给出该节点的左右子节点的索引。如果孩子不存在,一个“-”将放在位置。任何一对孩子都被一个空间隔开。

输入样式

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

输出规格

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.

对于每个测试用例,按从上到下、从左到右的顺序在一行中打印所有叶子的索引。任何相邻的数字之间必须正好有一个空格,并且在行尾没有额外的空格。

输出样式
4 1 5
算法分析

叶节点的定义:树中没有子结点(度为0)的结点称为是叶节点,叶节点又被称为终端结点

由输入进行分析:

index01234567
left1-02--54
right---7---6

在这里插入图片描述

程序框架设计:

  1. 输入函数(读取输入的数据)保存到树里,找到根节点
  2. 叶节点查找函数(读取根节点)利用层序遍历的算法进行查询
  3. 结合队列的操作进行数据的存取

程序设计

读取输入函数

在这里插入图片描述

tree buildtree(treenode t[])
{
	char cl, cr;
	int i,root=0;
	int arr[maxsize];

	cin >> n;
	if (n)
	{
		for (i = 0; i < n; i++)
		{
			arr[i] = 0;
		}
		for (i = 0; i < n; i++)
		{
			cin >> cl >> cr;
			if (cl != '-')
			{
				t[i].left = cl - '0';
				arr[t[i].left] = 1;
			}
			else
			{
				arr[t[i].left] = -1;
				t[i].left = -1;
			}
			if (cr != '-')
			{
				t[i].right = cr - '0';
				arr[t[i].right] = 1;
			}
			else
			{
				arr[t[i].right] = -1;
				t[i].right = -1;
			}
		}
		for (i = 0; i < n; i++)
		{
			if (!arr[i])
			{
				break;
			}
		}
		root = i;
	}
	return root;
}

建立队列

在这里插入图片描述

//创建队列

struct treequeue {
	int* index;//存放下标
	int front, rear;
};

typedef struct treequeue* tque;
int n;

tque createqueue()
{
	tque q = (tque)malloc(sizeof(struct treequeue));
	q->index = (int*)malloc(maxsize * sizeof(int));
	q->front = q->rear = -1;
	return q;
}

bool isempty(tque q)
{
	if (q->front == q->rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//入队
void addqueue(tque q, int x)
{
	if ((q->rear + 1) % 10 == q->front)
	{
		return;
	}
	else
	{
		q->rear = (q->rear + 1) % maxsize;
		q->index[q->rear] = x;
	}
}

//出队
int delequeue(tque q)
{
	if(q->front==q->rear)
	{
		return -1;
	}
	else
	{
		q->front = (q->front + 1) % maxsize;
		return q->index[q->front];
	}
}

层序遍历函数

由于本次输出需要满足输出的叶节点,需要满足从上到下,从左到右的方式进行输出。为此需要采用队列方式进行。队列在每次执行的过程中,不断将属于该节点的左右子结点做入队操作,同时不断将根节点做出队操作。当出队操作结束便是整个树的结点个数。同时在左右子结点做入队操作的时候,不断对该节点进行判决

在这里插入图片描述

在这里插入图片描述

void traversetree(int top)
{
	int i,num=0;
	if (top == -1)return;

	tque q = createqueue();
	addqueue(q, top);
	while (q->front != q->rear)//当队列不为空时,不断循环
	{
		num++;
		i = delequeue(q);//弹出第一个入队的元素

		if ((t1[i].left != -1) && (t1[i].right != -1))
		{
			addqueue(q, t1[i].left);//当左右都不空,先做儿子入队
			addqueue(q, t1[i].right);
		}
		else if ((t1[i].left != -1) && (t1[i].right == -1))
			addqueue(q, t1[i].left);
		else if ((t1[i].left == -1) && (t1[i].right != -1))
			addqueue(q, t1[i].right);
		else if ((t1[i].left == -1) && (t1[i].right == -1))
		{
			if (num != n)
			{
				cout << i << " ";
			}
			else {
				cout << i;
			}
		}
	}


}
全部程序
#include<stdio.h>
#include<iostream>

using namespace std;
typedef int elementtype;
typedef int tree;

constexpr auto maxsize = 100;;

struct treenode
{
	int left;
	int right;
}t1[maxsize];

//创建队列

struct treequeue {
	int* index;//存放下标
	int front, rear;
};

typedef struct treequeue* tque;
int n;

tque createqueue()
{
	tque q = (tque)malloc(sizeof(struct treequeue));
	q->index = (int*)malloc(maxsize * sizeof(int));
	q->front = q->rear = -1;
	return q;
}

bool isempty(tque q)
{
	if (q->front == q->rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//入队
void addqueue(tque q, int x)
{
	if ((q->rear + 1) % 10 == q->front)
	{
		return;
	}
	else
	{
		q->rear = (q->rear + 1) % maxsize;
		q->index[q->rear] = x;
	}
}

//出队
int delequeue(tque q)
{
	if(q->front==q->rear)
	{
		return -1;
	}
	else
	{
		q->front = (q->front + 1) % maxsize;
		return q->index[q->front];
	}
}








tree buildtree(treenode t[])
{
	char cl, cr;
	int i,root=0;
	int arr[maxsize];

	cin >> n;
	if (n)
	{
		for (i = 0; i < n; i++)
		{
			arr[i] = 0;
		}
		for (i = 0; i < n; i++)
		{
			cin >> cl >> cr;
			if (cl != '-')
			{
				t[i].left = cl - '0';
				arr[t[i].left] = 1;
			}
			else
			{
				arr[t[i].left] = -1;
				t[i].left = -1;
			}
			if (cr != '-')
			{
				t[i].right = cr - '0';
				arr[t[i].right] = 1;
			}
			else
			{
				arr[t[i].right] = -1;
				t[i].right = -1;
			}
		}
		for (i = 0; i < n; i++)
		{
			if (!arr[i])
			{
				break;
			}
		}
		root = i;
	}
	return root;
}


void traversetree(int top)
{
	int i,num=0;
	if (top == -1)return;

	tque q = createqueue();
	addqueue(q, top);
	while (q->front != q->rear)//当队列不为空时,不断循环
	{
		num++;
		i = delequeue(q);//弹出第一个入队的元素

		if ((t1[i].left != -1) && (t1[i].right != -1))
		{
			addqueue(q, t1[i].left);//当左右都不空,先做儿子入队
			addqueue(q, t1[i].right);
		}
		else if ((t1[i].left != -1) && (t1[i].right == -1))
			addqueue(q, t1[i].left);
		else if ((t1[i].left == -1) && (t1[i].right != -1))
			addqueue(q, t1[i].right);
		else if ((t1[i].left == -1) && (t1[i].right == -1))
		{
			if (num != n)
			{
				cout << i << " ";
			}
			else {
				cout << i;
			}
		}
	}


}



int main()
{
	int r1,temp=0;
	r1 = buildtree(t1);
	//cout << r1 << endl;
	//int i;
	//for (i = 0; i < 8; i++)
	//{
	//	cout << "position" << i << endl;
	//	cout << "left" << t1[i].left << endl;
	//	cout << "right" << t1[i].right << endl;
	//	cout << endl;
	//}
	traversetree(r1);
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值