ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MOOC数据结构PTA-03-树2 List Leaves

2021-08-02 22:33:01  阅读:173  来源: 互联网

标签:Node 03 MOOC int Leaves tree queue next Queue


题目表述

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
结尾无空行

题目理解

按顺序输出叶子节点的序号,仍是构建一个静态链表存储输入数据,然后build一个二叉树,用层序输出的方式,判断是否是叶子节点。

代码

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define element int
#define Maximum 10
#define NON -1
typedef struct node {
    element lchild, rchild;
    struct node* next;
} Node;
typedef struct queue {
    struct node *front, *rear;
} Queue;
//**********函数声明**********//
int buildTree(Node*);
void showLeaves(Node* tree, int Root);
Queue* Init_Queue();
bool isempty(Queue* queue);
void EnQueue(Queue* queue, Node* x);
bool DeQueue(Queue* queue, Node* x);
void visit(Node*, Queue*, Node*);

int main() {
    Node tree[Maximum];
    int Root = buildTree(tree);
    showLeaves(tree, Root);
    return 0;
}

int buildTree(Node* p) {
    int N;
    scanf("%d", &N);
    int* check = (int*)malloc(sizeof(int) * N);
    char left, right;
    for (int i = 0; i < N; i++)
        check[i] = 0;
    for (int i = 0; i < N; i++) {
        scanf(" %c %c", &left, &right);
        (p + i)->lchild = (left == '-') ? NON : (left - '0');
        (p + i)->rchild = (right == '-') ? NON : (right - '0');
        if (left != '-')
            check[(p + i)->lchild] = 1;
        if (right != '-')
            check[(p + i)->rchild] = 1;
    }
    int root = NON;
    for (int i = 0; i < N; i++) {
        if (check[i] == 0) {
            root = i;
            break;
        }
    }
    free(check);
    return root;
}
void showLeaves(Node* tree, int Root) {
    Queue* q = Init_Queue();
    EnQueue(q, tree + Root);
    Node* x = (Node*)malloc(sizeof(Node));
    while (!isempty(q)) {
        DeQueue(q, x);
        if (x->lchild != NON)
            EnQueue(q, tree + x->lchild);
        if (x->rchild != NON)
            EnQueue(q, tree + x->rchild);
        // printf("%d->", (x->next - tree));
        visit(tree, q, x);
    }
    free(x);
}
Queue* Init_Queue() {
    Queue* q = (Queue*)malloc(sizeof(Queue));
    q->front = (Node*)malloc(sizeof(Node));
    q->rear = q->front;
    q->front->next = NULL;
    return q;
}
bool isempty(Queue* queue) {
    if (queue->front == queue->rear)
        return true;
    else
        return false;
}
void EnQueue(Queue* queue, Node* x) {
    queue->rear->next = x;
    queue->rear = x;
}
bool DeQueue(Queue* queue, Node* x) {
    if (isempty(queue))
        return false;
    Node* temp = queue->front->next;
    x->lchild = temp->lchild;
    x->rchild = temp->rchild;
    x->next = temp;
    queue->front->next = temp->next;
    if (temp == queue->rear)
        queue->rear = queue->front;
    // free(temp);
    return true;
}
void visit(Node* tree, Queue* q, Node* x) {
    if (x->rchild == NON && x->lchild == NON) {
        printf("%d", x->next - tree);
        if (!isempty(q))
            putchar(' ');
    }
}

image

标签:Node,03,MOOC,int,Leaves,tree,queue,next,Queue
来源: https://www.cnblogs.com/wundt-asim/p/15090682.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有