您的位置:首页 > 理论基础 > 数据结构算法

数据结构基础— List Leaves

2018-03-21 22:19 197 查看
从上到下,从左到右输出,就是层次遍历

所有的查找什么东西在树中 都是遍历的问题

这里是查找叶节点

那就是 “层次遍历 + 判断是否为叶节点”

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 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

//
//  main.c
//  List leaves
//
//  Created by air on 2018/3/21.
//
#define Null -1
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int check[15]; /* 最大是10,所以我定个15绰绰有余了,而且队列也好判断 */
int root,N;
int store[15];
typedef struct Node{
int index;
int left;
int right;
}treeNode;

typedef struct Qnode{
treeNode Tree [15];
int front;
int rear;
} my_queue;

typedef my_queue * ptrToQueue;
typedef ptrToQueue queue;

void makeEmpty(ptrToQueue q){
q->front = 0;
q->rear = 0;
}

void pop(ptrToQueue q){
q->front ++;
}

int top(ptrToQueue q){
return q->Tree[q->front].index;
}

void push(ptrToQueue q, treeNode x){
q->Tree[(q->rear)] = x;
q->rear ++;
}

bool isEmpty(queue q){
return q->rear == q->front;
}

/* read funxtion test fine */
void readQueue(treeNode arrayOfTree[]){
scanf("%d\n",&N);
char lc,rc;
for(int i = 0; i < 15; i++){
check[i] = 0;
store[i] = 0;
}
for(int i = 0; i < N; i++){
arrayOfTree[i].index = i;
if(i == N-1)
scanf("%c %c", &lc, &rc);         /* the last line of input is different */
else
scanf("%c %c\n", &lc, &rc);
if(lc != '-'){
arrayOfTree[i].left = lc -'0';    /* set the left number of tree*/
check[arrayOfTree[i].left] = 1;   /* set the number of check*/
}
else
arrayOfTree[i].left = Null;
if(rc != '-'){
arrayOfTree[i].right = rc -'0';
check[arrayOfTree[i].right] = 1;
}
else
arrayOfTree[i].right = Null;
}
}

void findRoot(void){
for (int i = 0; i < N; i ++) {
if(check[i] == 0)
root = i;
}
}
/* level order traversal */
void levelOrderTraversal(queue q, treeNode arrayOfTree[]){  //混乱- -
int count = 0;
push(q, arrayOfTree[root]);
while (!isEmpty(q)) { /*是不是很像BFS- -  我觉得层次遍历就是个BFS*/
int topIndex = top(q);
pop(q);
if(arrayOfTree[topIndex].left == Null && arrayOfTree[topIndex].right == Null){
store[count++] = topIndex;
continue;
}
if(arrayOfTree[topIndex].left != Null)
push(q, arrayOfTree[arrayOfTree[topIndex].left]);
if(arrayOfTree[topIndex].right != Null)
push(q, arrayOfTree[arrayOfTree[topIndex].right]);
}
/* 因为很奇葩的输出方式,我只好把结果先出存在 store 这个数组这里*/
for(int i = 0; i < count-1; i++)
printf("%d ",store[i]);
printf("%d",store[count-1]);
}

int main(int argc, const char * argv[]) {
queue q = (ptrToQueue)malloc(sizeof(my_queue));
makeEmpty(q);
treeNode arrayOfTree[15];
readQueue(arrayOfTree); /* 读入数组 */
findRoot(); /* 找到根节点 */
levelOrderTraversal(q,arrayOfTree); /* 层次遍历并打印 */
free(q);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构