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)的结点称为是叶节点,叶节点又被称为终端结点
由输入进行分析:
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|
left | 1 | - | 0 | 2 | - | - | 5 | 4 |
right | - | - | - | 7 | - | - | - | 6 |
程序框架设计:
- 输入函数(读取输入的数据)保存到树里,找到根节点
- 叶节点查找函数(读取根节点)利用层序遍历的算法进行查询
- 结合队列的操作进行数据的存取
程序设计
读取输入函数
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);
}