Data structure exercises: Build a tree represented by a generalized table and output in a sequence

Title description:

【问题描述】
 给定一颗二叉树,要求从下至上按层遍历二叉树,每层的访问顺序是从左到右,每一层单独输出一行。

【输入形式】
 广义表表示的二叉树,结点元素类型为整型,且都大于0,例如:1( 2( 3 ( 4, 5 ) ), 6( 7, 8( 9, 10 ) ) )

【输出形式】
 从下至上,打印每一层的结点元素值,元素间以空格隔开。每层的访问顺序是从左到右,每一层单独输出一行。

【样例输入】
 1(2(3(4,5)),6(7,8(9,10))),字符串内没有空格

【样例输出】
 4 5 9 10
 3 7 8
 2 6
 1

【样例说明】

【评分标准】
 本题目主要考察两个知识点:
 1.创建二叉树存储结构
 2.按层次遍历二叉树的算法

Question idea:

  • This question was a little silly when I first looked at it, because this is a generalized table to build a binary tree, I deal with it as follows:
  1. If you encounter "(", it means that you need to create the left subtree of this node
  2. If you encounter ")", it means you need to operate on the parent node of this node
  3. If you encounter ",", it means that the left node has been created (that is, the current T points to the left node), and the right node of the parent node of T must be created

Therefore, it is clear that you can use a manually created stack (in fact, I have also used recursion, but the code written recursively is a bit troublesome)

  • For output from the last layer, we can also think of using layer sequence traversal, but how to modify the code of layer sequence traversal to output from the last layer online?
  1. I used a queue (the basis of layer sequence traversal), but first enter the right node, then enter the left node, and record the level of each node
  2. Then traverse the queue from back to front (this explains why you need to enter the right node first and then the left node) [because this will traverse the left node of the tree first, and then traverse the right node when the queue is traversed from back to front Node], for nodes of the same level, output in one line, and output in different lines.

Code:

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

typedef struct tree {
    
    
    int data;
    struct tree* lchild;
    struct tree* rchild;
}*Ptree;

typedef struct quee {
    
    
    Ptree data[100];
    int lay[100];
    int num;
    int rear, front;
}*Pquee;

typedef struct stack {
    
    
    Ptree data[100];
    int rear;
}*Pstack;

void in(Pstack s, Ptree T) {
    
    
    s->data[s->rear++] = T;
}

void pop(Pstack s, Ptree& T) {
    
    
    T = s->data[--s->rear];
}

void creat(Ptree& T, Pstack& s, char string[], int& i) {
    
      // 1(2(3(4,5)),6(7,8(9,10)))
    T = (Ptree)malloc(sizeof(struct tree));
    T->lchild = NULL;   T->rchild = NULL;
    while (string[i] != '\0') {
    
    
        if (string[i] >= '0' && string[i] <= '9') {
    
    
            int num[10], n = 0;
            while (string[i] >= '0' && string[i] <= '9') {
    
    
                num[n++] = string[i] - '0';
                i++;  // 退出循环后i指向的为符号不是数字
            }
            int NUM = 0;
            for (int j = n; j > 0; j--) {
    
    
                int jiec = 1;
                for (int j1 = 1; j1 < j; j1++) {
    
    
                    jiec *= 10;
                }
                NUM += num[n - j] * jiec;
            }
            // printf("NUM = %d\n", NUM);
                //T = (Ptree)malloc(sizeof(struct tree));
                //T->lchild = NULL;   T->rchild = NULL;
            T->data = NUM;
            in(s, T);
        }
        // printf("string%d = %c\n", i, string[i]);

        if (string[i] == '(') {
    
    
            i = i + 1;
            T->lchild = (Ptree)malloc(sizeof(struct tree));
            T->lchild->lchild = NULL;   T->lchild->rchild = NULL;
            T = T->lchild;
        }
        if (string[i] == ')') {
    
    
            i = i + 1;
            pop(s, T);
            T = s->data[s->rear - 1];
        }
        if (string[i] == ',') {
    
    
            i = i + 1;
            pop(s, T);
            T = s->data[s->rear - 1];
            T->rchild = (Ptree)malloc(sizeof(struct tree));
            T->rchild->lchild = NULL;   T->rchild->rchild = NULL;
            T = T->rchild;
        }
    }
}

void print(Ptree T, Pquee q) {
    
    
    Ptree j = NULL;
    q->lay[q->rear] = 0;
    q->data[q->rear++] = T;
    int layer;
    while (q->front != q->rear) {
    
    
        layer = q->lay[q->front];
        j = q->data[q->front++];
        // 先入右结点
        if (j->rchild) {
    
    
            // printf("rch\n");
            q->lay[q->rear] = layer + 1;
            q->data[q->rear++] = j->rchild;
        }
        if (j->lchild) {
    
    
            // printf("lch\n");
            q->lay[q->rear] = layer + 1;
            q->data[q->rear++] = j->lchild;
        }
    }
    layer = q->lay[q->rear - 1];
    // printf("layer %d\n", layer);
    for (int i = q->rear - 1; i >= 0;) {
    
    
        // printf("lay[%d] i = %d\n", q->lay[i], i);
        if (q->lay[i] == layer) {
    
    
            printf("%d ", q->data[i]->data);
            i--;
        }
        else {
    
    
            printf("\n");
            layer--;
        }
    }
}

int main() {
    
    
    char string [100];
    scanf("%s", string);
    Pquee q = (Pquee)malloc(sizeof(struct quee));
    Pstack s = (Pstack)malloc(sizeof(struct stack));
    s->rear = 0;
    q->front = 0; q->rear = 0; q->num = 0;
    Ptree T = (Ptree)malloc(sizeof(struct tree));
    T->lchild = NULL;   T->rchild = NULL;
    int i = 0;
    creat(T->lchild, s, string, i);
    print(T->lchild, q);
    return 0;
}

operation result:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43779658/article/details/106531536