Data structure -- c language implementation of recursive traversal of binary tree (super detailed notes / experimental report)

Data structure - c language implementation of recursive traversal of binary tree (super detailed notes / experimental report)

Knowledge review

Traversal of a binary tree means that each node in the binary tree is accessed only once according to a certain law. The access can calculate the data information of the node in the binary tree, and print the information of the node, including any other operations on the node.

Experimental topic

Output the traversal result of binary tree

Experimental purpose

  1. Familiar with the node structure of binary tree
  2. Binary linked list is used as storage structure to establish binary tree
  3. It is traversed by recursive algorithm (first order, middle order and second order)
  4. Output the traversal result

Experimental requirements

  1. Binary linked list is used as storage structure to establish binary tree
  2. It is traversed by recursive algorithm (first order, middle order and second order)
  3. Output the traversal result

Experiment contents and steps

1. Demand analysis

The user inputs the generalized table form of binary tree, and the program outputs binary tree and traversal results.

2. Outline design

The function of designing and exporting two binary tree and three traversal functions are designed, and then these functions are invoked in the main function to realize operation.

3. Detailed design

Establish a binary tree and output the binary tree code, which has been written before
Create and output binary tree
Let's take a look at the traversal operation
Because the recursive method is adopted, the three methods are almost the same. Just change the order of visit.

//Medium order traversal
void Inorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        Inorder(bt->lc);
        printf("%c ",bt->data);
        Inorder(bt->rc);
    }
}

//Preorder traversal 
void Preorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        printf("%c ",bt->data);
        Preorder(bt->lc);
        Preorder(bt->rc);
    }
}

//Postorder traversal
void Postorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        Postorder(bt->lc);
        Postorder(bt->rc);
        printf("%c ",bt->data);
    }
}

The main function part prints out the program functions and guides the users.

int main()//(a (b (d), C (E))) (a (b (D, e), C (F, g))) (((a, b)), ((), d), (E, f))) this is not a tree
//Let's take an example of a Polish expression (- + (a,*(b,c)),/(d,e)))
{
    BiTree *bt;
    char *gyb,str[MAXSIZE];
    int j=1;
    printf("-------------------------------------------------------");
    printf("\n            Program function");
    printf("\n 1.The string will be represented according to the binary generalized table entered ");
    printf("\n · Generate the corresponding binary tree chain structure.");
    printf("\n 2.Concave representation of output binary tree.");
    printf("\n · G--root L--Left child R--Right child");
    printf("\n 3.Tree print binary tree.");
    printf("\n · Rotate 90 degrees counterclockwise to display the binary tree");
    printf("\n 4.Print the results of preorder, inorder and subsequent traversal.");
    printf("\n * Input example:(A(B(D,),C(,E)))or(-(+(a,*(b,c)),/(d,e)))");
    printf("\n-------------------------------------------------------\n");
    printf("Please enter the generalized form of binary tree:\n");
    gyb=str;
    scanf("%s",str);
    bt =CreatBiTreepre(gyb);
    printf("Binary tree established successfully!\n");
    printf("The concave method of this binary tree is expressed as:\n");
    OutBiTree(bt);
    printf("Tree print binary tree:\n");
    PrintTree(bt,1);
    printf("The preorder traversal sequence is:\n");
    Preorder(bt);
    printf("\n The middle order traversal sequence is:\n");
    Inorder(bt);
    printf("\n The subsequent traversal sequence is:\n");
    Postorder(bt);
    return 0;
}

4. Commissioning analysis

Problems encountered and Solutions

  • With the previous creation and output of binary trees as a cushion, the recursive traversal algorithm is still very simple.

Spatiotemporal analysis of algorithm

Let the binary tree have n nodes, and each node must be put on and out of the stack once, that is, put on and out of the stack n times respectively, and the access to the node is also N times. The time complexity of these binary tree recursive traversal algorithms is O ( n ) O(n) O(n).

experimental result

The experimental results are very good, all operations can be performed normally, and the correct output is obtained by taking Polish expression as an example.
This is the output of the program of the example analyzed above.

You can also take a look at the following example.

Experimental summary

With the foreshadowing of the previous work, recursive traversal is very simple. Repeat a lot and refine everything into steel!

Finally, the complete code is attached

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 255
//The chain storage structure of binary tree is defined. There are three fields: data field, left child field and right child field
typedef struct BiNode
{
    char data;
    struct BiNode *lc;
    struct BiNode *rc;
}BiTree;

//Create a binary tree in the form of a generalized table
BiTree *CreatBiTreepre(char *str)
{
    BiTree *bt,*stack[MAXSIZE],*p=NULL;
    int top=-1,k,j=0;
    char ch;
    bt=NULL;
    ch=str[j];
    while(ch!='\0')
    {
        switch(ch)
        {
        case '(':
            {
                top++;
                stack[top]=p;
                k=1;
                break;
            }
        case ')':
            {
                top--;
                break;
            }
        case ',':
            {
                k=2;
                break;
            }
        default:
            {
                p=(BiTree *)malloc(sizeof(BiTree));
                p->data=ch;
                p->lc=p->rc=NULL;
                if(bt==NULL)
                {
                    bt=p;
                }
                else
                {
                    switch(k)
                    {
                    case 1:
                        {
                            stack[top]->lc=p;
                            break;
                        }
                    case 2:
                        {
                            stack[top]->rc=p;
                            break;
                        }
                    }
                }
            }
        }
        j++;
        ch=str[j];
    }
    return bt;//You only need to know one chain storage, and you'll know it all
}

//Output binary tree by concave method
void OutBiTree(BiTree *bt)
{
    BiTree *stack[MAXSIZE],*p;
    int feature[MAXSIZE][2],top,n,i,width=4;
    char type;
    if(bt!=NULL)
    {
        top=1;
        stack[top]=bt;
        feature[top][0]=width;
        feature[top][1]=2;
        while(top>0)
        {
            p=stack[top];
            n=feature[top][0];
            switch(feature[top][1])
            {
            case 0:
                {
                    type='l';
                    break;
                }
            case 1:
                {
                    type='R';
                    break;
                }
            case 2:
                {
                    type='G';
                    break;
                }
            }
            for(i=1;i<=n;i++)
                printf(" ");
            printf("%c(%c)\n",p->data,type);
            top--;
            if(p->lc!=NULL)
            {
                top++;
                stack[top]=p->lc;
                feature[top][0]=n+width;
                feature[top][1]=0;
            }
            if(p->rc!=NULL)
            {
                top++;
                stack[top]=p->rc;
                feature[top][0]=n+width;
                feature[top][1]=1;
            }

        }
    }
}

void PrintTree(BiTree *bt,int nLayer)
{
    if(bt==NULL)
        return ;
    PrintTree(bt->rc,nLayer+1);
    for(int i=0;i<nLayer;i++)
        printf("  ");
    printf("%c\n",bt->data);
    PrintTree(bt->lc,nLayer+1);
}

//Medium order traversal
void Inorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        Inorder(bt->lc);
        printf("%c ",bt->data);
        Inorder(bt->rc);
    }
}

//Preorder traversal 
void Preorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        printf("%c ",bt->data);
        Preorder(bt->lc);
        Preorder(bt->rc);
    }
}

//Postorder traversal
void Postorder(BiTree *bt)
{
    if(bt!=NULL)
    {
        Postorder(bt->lc);
        Postorder(bt->rc);
        printf("%c ",bt->data);
    }
}

int main()//(a (b (d), C (E))) (a (b (D, e), C (F, g))) (((a, b)), ((), d), (E, f))) this is not a tree
//Let's take an example of a Polish expression (- + (a,*(b,c)),/(d,e)))
{
    BiTree *bt;
    char *gyb,str[MAXSIZE];
    int j=1;
    printf("-------------------------------------------------------");
    printf("\n            Program function");
    printf("\n 1.The string will be represented according to the binary generalized table entered ");
    printf("\n · Generate the corresponding binary tree chain structure.");
    printf("\n 2.Concave representation of output binary tree.");
    printf("\n · G--root L--Left child R--Right child");
    printf("\n 3.Tree print binary tree.");
    printf("\n · Rotate 90 degrees counterclockwise to display the binary tree");
    printf("\n 4.Print the results of preorder, inorder and subsequent traversal.");
    printf("\n * Input example:(A(B(D,),C(,E)))or(-(+(a,*(b,c)),/(d,e)))");
    printf("\n-------------------------------------------------------\n");
    printf("Please enter the generalized form of binary tree:\n");
    gyb=str;
    scanf("%s",str);
    bt =CreatBiTreepre(gyb);
    printf("Binary tree established successfully!\n");
    printf("The concave method of this binary tree is expressed as:\n");
    OutBiTree(bt);
    printf("Tree print binary tree:\n");
    PrintTree(bt,1);
    printf("The preorder traversal sequence is:\n");
    Preorder(bt);
    printf("\n The middle order traversal sequence is:\n");
    Inorder(bt);
    printf("\n The subsequent traversal sequence is:\n");
    Postorder(bt);
    return 0;
}

Tags: C Algorithm data structure Binary tree

Posted on Sat, 27 Nov 2021 21:18:01 -0500 by rroc