7-7 create a binary tree for pre order sequence, output pre order sequence, middle order sequence and post order sequence, and output the number of leaf nodes (20 points)

Posted by webosb on Thu, 10 Mar 2022 14:33:12 +0100

I feel that I haven't learned the part of the tree very well. PTA encountered this problem and didn't write it out at first. After writing it, I found that it's actually very simple. Record it.

In fact, what I learned is the process of writing this question

That is, if you modify a structure with typedef, you can't define the structure variable directly after the structure definition, but take another line of definition.

 

typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;//Error!!


typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode;
BiTNode *BiTree;//correct!!!

 

7-7 create a binary tree for pre order sequence, output pre order sequence, middle order sequence and post order sequence, and output the number of leaf nodes (20 points)

For a given binary tree, the pre order sequence, middle order sequence and post order sequence are output, and the number of leaf nodes is output.

Input format:

Preorder traversal sequence of binary tree.

Tip: the pre sequence of a binary tree is a string. If the character is' # ', it means that the binary tree is an empty tree, otherwise the character is the data element of the corresponding node.

Output format:

If it is a non empty binary tree, four lines of content will be output. The first line is the preordered traversal sequence of the binary tree; The second line is the middle order traversal sequence of binary tree; The third line is the post order traversal sequence of the binary tree; The fourth line is the number of leaf nodes;

If it is an empty binary tree, you only need to output the number of leaves 0

Input example 1:

FCA##DB###EHM###G##

Output example 1:

FCADBEHMG
ACBDFMHEG
ABDCMHGEF
4

Input example 2:

#

Output example 2:

0

 

We know that the first, middle and last order traversals are easy to write, but we should learn to transform and change the output line into the operations we need to perform, such as finding leaf nodes in this problem. After creating a tree according to the input order traversal, we can first traverse each node and find the node without left and right subtrees, which is the leaf node.

Remember how to write the structure definition tree node:

typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode;

Build a tree according to the entered pre order traversal string (with '#'):

BiTNode * creatTree(BiTNode* root)
{
    char ch;
    scanf("%c", &ch);
    if (ch == '#')
        root = NULL;
    else
    {
        root = (BiTNode*)malloc(sizeof(BiTNode));
        root->data = ch;
        root->lchild = creatTree(root->lchild);//Construct left subtree
        root->rchild = creatTree(root->rchild);//Construct right subtree
    }
    return root;
}

Output preorder traversal sequence

void preOrder(BiTNode *T)
{
    if(T == NULL)
    {
        return;
    }
    cout<<T->data;
    preOrder(T->lchild);
    preOrder(T->rchild);
}

Output middle order traversal sequence

void inOrder(BiTNode *T)
{
    if(T == NULL)return;
    inOrder(T->lchild);
    cout<<T->data;
    inOrder(T->rchild);
}

Output subsequent traversal sequence

void postOrder(BiTNode *T)
{
    if(T == NULL)return;
    postOrder(T->lchild);
    postOrder(T->rchild);
    cout<<T->data;
}

Find leaf node

void left(BiTNode *T)//The pre order traverses each point and checks that the node without left and right subtrees is the leaf node
{
    if(T == NULL)return;
    if(T->lchild == NULL && T->rchild == NULL)
        n++;
    left(T->lchild);
    left(T->rchild);
}

Full code:

#include<bits/stdc++.h>
using namespace std;

int n=0;
typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode;

BiTNode * creatTree(BiTNode* root)
{
    char ch;
    scanf("%c", &ch);
    if (ch == '#')
        root = NULL;
    else
    {
        root = (BiTNode*)malloc(sizeof(BiTNode));
        root->data = ch;
        root->lchild = creatTree(root->lchild);//Construct left subtree
        root->rchild = creatTree(root->rchild);//Construct right subtree
    }
    return root;
}

void preOrder(BiTNode *T)
{
    if(T == NULL)
    {
        return;
    }
    cout<<T->data;
    preOrder(T->lchild);
    preOrder(T->rchild);
}

void inOrder(BiTNode *T)
{
    if(T == NULL)return;
    inOrder(T->lchild);
    cout<<T->data;
    inOrder(T->rchild);
}

void postOrder(BiTNode *T)
{
    if(T == NULL)return;
    postOrder(T->lchild);
    postOrder(T->rchild);
    cout<<T->data;
}
void left(BiTNode *T)//The pre order traverses each point and checks that the node without left and right subtrees is the leaf node
{
    if(T == NULL)return;
    if(T->lchild == NULL && T->rchild == NULL)
        n++;
    left(T->lchild);
    left(T->rchild);
}
int main()
{
    BiTNode *BiTree;
    BiTree=creatTree(BiTree);
    if(BiTree == NULL)cout<<"0";
    else
    {
        preOrder(BiTree);
        cout<<endl;
        inOrder(BiTree);
        cout<<endl;
        postOrder(BiTree);
        cout<<endl;
        left(BiTree);
        cout<<n;
    }
    return 0;
}

Topics: Algorithm data structure Binary tree