数据结构基础— 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
所有的查找什么东西在树中 都是遍历的问题
这里是查找叶节点
那就是 “层次遍历 + 判断是否为叶节点”
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; }
相关文章推荐
- 中国大学MOOC-数据结构基础习题集、03-2、List Leaves
- 数据结构基础(12) --双向循环链表的设计与实现
- 数据结构基础温故-5.图(中):图的遍历算法
- 中国大学MOOC-数据结构基础习题集、07-2、Insert or Merge
- hrbust 基础数据结构——单链表(1)
- 数据结构基础概念
- 数据结构基础(9) --单链表的设计与实现(2)之高级操作
- 【数据结构与算法的语言基础】数组与字符串
- 数据结构之红黑树(一)——基础分析
- 1.5 数据结构基础(一)
- 数据结构基础(12) --双向循环链表的设计与实现
- 数据结构基础(5)--C语言实现循环队列--静态
- 数据结构基础(2) --顺序查找 & 二分查找
- 数据结构基础之队列
- [数据结构基础] 图
- 数据结构基础5_顺序栈的实现
- 【数据结构基础】栈--顺序存储结构
- openCV之基础数据结构
- windows基础数据结构盲点集合
- 【数据结构基础】队列的链式存储结构