Data structure - tree and binary tree-1

Posted by searchman on Sun, 16 Jan 2022 15:06:51 +0100

Experimental preparation:

1. Skillfully use pointer for program design and master structure pointer.
2. Master the use of structure pointer to access structure variables.
3. Master the use of pointers as function parameters.
4. Understand the meaning, purpose and processing method of tree and binary tree.

1, Experimental purpose

  1. Understand and master the type definition method of tree and binary tree.
  2. The basic storage structure of binary tree is defined, the basic operation is realized, and the binary tree algorithm library is constructed.
  3. Learn to use trees and binary trees to solve practical problems

2, Experimental requirements

[item 1] - establishment of binary tree algorithm library
Define the chain storage structure of binary tree, realize its basic operation, and complete the test.
[requirements]:

  1. Header file BTREE H defines the data structure and declares the function used to complete the basic operation. Functions corresponding to basic operations include:
void CreateBTNode(BTNode *&b,char *str);     //Creating binary chains from str strings
BTNode *FindNode(BTNode *b,ElemType x);  //Returns the node pointer whose data field is x
BTNode *LchildNode(BTNode *p);      //Returns the left child node pointer of the * p node
BTNode *RchildNode(BTNode *p);      //Returns the right child node pointer of the * p node
int BTNodeDepth(BTNode *b);     //Find the depth of binary tree b
void DispBTNode(BTNode *b);     //Output binary tree in parenthesis
void DestroyBTNode(BTNode *&b);    //Destroy binary tree

  1. In BTREE These functions are implemented in CPP.
  2. Complete the test in the main function, including the following:

(1) Use "a (b (D, e (H (J, K (L, m (, n)))), C (F, G (, I)))" to create a binary tree as shown in the figure for testing.
(2) Output binary tree
(3) Find the node with the value of 'H'. If found, output the values of the left and right children of the node with the value of 'H'
(4) Find the height of binary tree
(5) Destroy binary tree

The linked storage algorithm library of binary tree adopts the multi file organization form of program, including:

1. Header file: BTREE h. Including the code defining the linked storage data structure of the binary tree, the macro definition, and the declaration of the function to implement the algorithm;
2. Source file: BTREE CPP, including the definition of functions to implement various algorithms;
3. In the process of establishing the algorithm library, in order to complete the test, establish a source file (such as main.cpp) in the same project, compile the main function and complete the relevant test work.

Program code:
Main.cpp:

#include <iostream>
#include "btree.h"

using namespace std;

int main()
{
    char str[]="A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    BTNode *b,*p;
    CreateBTNode(b,str);
    DispBTNode(b);
    if(FindNode(b,'H'))
        {
            printf("\n eureka\n");
            p=FindNode(b,'H');
            printf("%c  ",LchildNode(p)->data);
            printf("%c\n",RchildNode(p)->data);
        }
    else
        printf("Can't find\n");
    printf("The height of the binary tree is:%d\n",BTNodeDepth(b));
    DestroyBTNode(b);
    printf("The binary tree has been destroyed\n");
    return 0;
}

Btree.h:

#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
    ElemType data;              //data elements
    struct node *lchild;        //Point to left child
    struct node *rchild;        //Point to the right child
} BTNode;
void CreateBTNode(BTNode *&b,char *str);        //Creating binary chains from str strings
BTNode *FindNode(BTNode *b,ElemType x);     //Returns the node pointer whose data field is x
BTNode *LchildNode(BTNode *p);  //Returns the left child node pointer of the * p node
BTNode *RchildNode(BTNode *p);  //Returns the right child node pointer of the * p node
int BTNodeDepth(BTNode *b); //Find the depth of binary tree b
void DispBTNode(BTNode *b); //Output binary tree in parenthesis
void DestroyBTNode(BTNode *&b);  //Destroy binary tree

void preOrder(BTNode *b);
#endif // BTREE_H_INCLUDED

Btree.cpp:

#include <stdio.h>
#include <malloc.h>
#include "btree.h"

void CreateBTNode(BTNode *&b,char *str)     //Creating binary chains from str strings
{
    BTNode *St[MaxSize],*p=NULL;
    int top=-1,k,j=0;
    char ch;
    b=NULL;             //The established binary tree is initially empty
    ch=str[j];
    while (ch!='\0')    //Loop when str is not finished scanning
    {
        switch(ch)
        {
        case '(':
            top++;
            St[top]=p;
            k=1;
            break;      //Left node
        case ')':
            top--;
            break;
        case ',':
            k=2;
            break;                          //Right node
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=ch;
            p->lchild=p->rchild=NULL;
            if (b==NULL)                    //p points to the root node of the binary tree
                b=p;
            else                            //Binary tree root node established
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
            }
        }
        j++;
        ch=str[j];
    }
}
BTNode *FindNode(BTNode *b,ElemType x)  //Returns the node pointer whose data field is x
{
    BTNode *p;
    if (b==NULL)
        return NULL;
    else if (b->data==x)
        return b;
    else
    {
        p=FindNode(b->lchild,x);
        if (p!=NULL)
            return p;
        else
            return FindNode(b->rchild,x);
    }
}
BTNode *LchildNode(BTNode *p)   //Returns the left child node pointer of the * p node
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)   //Returns the right child node pointer of the * p node
{
    return p->rchild;
}
int BTNodeDepth(BTNode *b)  //Find the depth of binary tree b
{
    int lchilddep,rchilddep;
    if (b==NULL)
        return(0);                          //The height of the empty tree is 0
    else
    {
        lchilddep=BTNodeDepth(b->lchild);   //Find the height of the left subtree as lchilddep
        rchilddep=BTNodeDepth(b->rchild);   //Find the height of the right subtree as rchilddep
        return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
    }
}
void DispBTNode(BTNode *b)  //Output binary tree in parenthesis
{
    if (b!=NULL)
    {
        printf("%c",b->data);
        if (b->lchild!=NULL || b->rchild!=NULL)
        {
            printf("(");
            DispBTNode(b->lchild);
            if (b->rchild!=NULL) printf(",");
            DispBTNode(b->rchild);
            printf(")");
        }
    }
}
void DestroyBTNode(BTNode *&b)   //Destroy binary tree
{
    if (b!=NULL)
    {
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}
void preOrder(BTNode *b)
{
    if(b != NULL)
    {
        printf("%c",b->data);
        preOrder(b->lchild);
        preOrder(b->rchild);
    }
}

Screenshot of operation results:

[item 2] implement a binary tree traversal algorithm, test with the created binary tree, and output each traversal result. Fill in the following table with the reference code Please use the binary tree algorithm library.

Program code:
Main.cpp:

#include <iostream>
#include "btree.h"

using namespace std;

int main()
{
    char str[]="A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    BTNode *b;
    CreateBTNode(b,str);
    DispBTNode(b);

    return 0;
}

Btree.h:

#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
    ElemType data;              //data elements
    struct node *lchild;        //Point to left child
    struct node *rchild;        //Point to the right child
} BTNode;
void CreateBTNode(BTNode *&b,char *str);        //Creating binary chains from str strings
BTNode *FindNode(BTNode *b,ElemType x);     //Returns the node pointer whose data field is x
BTNode *LchildNode(BTNode *p);  //Returns the left child node pointer of the * p node
BTNode *RchildNode(BTNode *p);  //Returns the right child node pointer of the * p node
int BTNodeDepth(BTNode *b); //Find the depth of binary tree b
void DispBTNode(BTNode *b); //Output binary tree in parenthesis
void DestroyBTNode(BTNode *&b);  //Destroy binary tree

void preOrder(BTNode *b);
#endif // BTREE_H_INCLUDED

Btree.cpp:

#include <stdio.h>
#include <malloc.h>
#include "btree.h"

void CreateBTNode(BTNode *&b,char *str)     //Creating binary chains from str strings
{
    BTNode *St[MaxSize],*p=NULL;
    int top=-1,k,j=0;
    char ch;
    b=NULL;             //The established binary tree is initially empty
    ch=str[j];
    while (ch!='\0')    //Loop when str is not finished scanning
    {
        switch(ch)
        {
        case '(':
            top++;
            St[top]=p;
            k=1;
            break;      //Left node
        case ')':
            top--;
            break;
        case ',':
            k=2;
            break;                          //Right node
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=ch;
            p->lchild=p->rchild=NULL;
            if (b==NULL)                    //p points to the root node of the binary tree
                b=p;
            else                            //Binary tree root node established
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
            }
        }
        j++;
        ch=str[j];
    }
}
BTNode *FindNode(BTNode *b,ElemType x)  //Returns the node pointer whose data field is x
{
    BTNode *p;
    if (b==NULL)
        return NULL;
    else if (b->data==x)
        return b;
    else
    {
        p=FindNode(b->lchild,x);
        if (p!=NULL)
            return p;
        else
            return FindNode(b->rchild,x);
    }
}
BTNode *LchildNode(BTNode *p)   //Returns the left child node pointer of the * p node
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)   //Returns the right child node pointer of the * p node
{
    return p->rchild;
}
int BTNodeDepth(BTNode *b)  //Find the depth of binary tree b
{
    int lchilddep,rchilddep;
    if (b==NULL)
        return(0);                          //The height of the empty tree is 0
    else
    {
        lchilddep=BTNodeDepth(b->lchild);   //Find the height of the left subtree as lchilddep
        rchilddep=BTNodeDepth(b->rchild);   //Find the height of the right subtree as rchilddep
        return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
    }
}
void DispBTNode(BTNode *b)  //Output binary tree in parenthesis
{
    if (b!=NULL)
    {
        printf("%c",b->data);
        if (b->lchild!=NULL || b->rchild!=NULL)
        {
            printf("(");
            DispBTNode(b->lchild);
            if (b->rchild!=NULL) printf(",");
            DispBTNode(b->rchild);
            printf(")");
        }
    }
}
void DestroyBTNode(BTNode *&b)   //Destroy binary tree
{
    if (b!=NULL)
    {
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}
void preOrder(BTNode *b)
{
    if(b != NULL)
    {
        printf("%c",b->data);
        preOrder(b->lchild);
        preOrder(b->rchild);
    }
}

Screenshot of operation results:

[item 3] use the algorithm in Item 2 to implement an algorithm to solve the number of nodes in the binary tree.

Program code:
Main.cpp:

#include <iostream>
#include "btree.h"

using namespace std;

int main()
{
    char str[]="A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))";
    BTNode *b,*p;
    int n=0;
    CreateBTNode(b,str);
    DispBTNode(b);
    if(FindNode(b,'H'))
        {
            printf("\n eureka\n");
            p=FindNode(b,'H');
            printf("%c  ",LchildNode(p)->data);
            printf("%c\n",RchildNode(p)->data);
        }
    else
        printf("Can't find\n");
    printf("The height of the binary tree is:%d\n",BTNodeDepth(b));
    countNode(b,n);
    printf("The number of nodes of the binary tree is:%d\n",n);
    DestroyBTNode(b);
    printf("The binary tree has been destroyed\n");
    return 0;
}

Btree.h:

#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
    ElemType data;              //data elements
    struct node *lchild;        //Point to left child
    struct node *rchild;        //Point to the right child
} BTNode;
void CreateBTNode(BTNode *&b,char *str);        //Creating binary chains from str strings
BTNode *FindNode(BTNode *b,ElemType x);     //Returns the node pointer whose data field is x
BTNode *LchildNode(BTNode *p);  //Returns the left child node pointer of the * p node
BTNode *RchildNode(BTNode *p);  //Returns the right child node pointer of the * p node
int BTNodeDepth(BTNode *b); //Find the depth of binary tree b
void DispBTNode(BTNode *b); //Output binary tree in parenthesis
void DestroyBTNode(BTNode *&b);  //Destroy binary tree

void preOrder(BTNode *b);
void countNode(BTNode *b,int &n);
#endif // BTREE_H_INCLUDED

Btree.cpp:

#include <stdio.h>
#include <malloc.h>
#include "btree.h"

void CreateBTNode(BTNode *&b,char *str)     //Creating binary chains from str strings
{
    BTNode *St[MaxSize],*p=NULL;
    int top=-1,k,j=0;
    char ch;
    b=NULL;             //The established binary tree is initially empty
    ch=str[j];
    while (ch!='\0')    //Loop when str is not finished scanning
    {
        switch(ch)
        {
        case '(':
            top++;
            St[top]=p;
            k=1;
            break;      //Left node
        case ')':
            top--;
            break;
        case ',':
            k=2;
            break;                          //Right node
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=ch;
            p->lchild=p->rchild=NULL;
            if (b==NULL)                    //p points to the root node of the binary tree
                b=p;
            else                            //Binary tree root node established
            {
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
            }
        }
        j++;
        ch=str[j];
    }
}
BTNode *FindNode(BTNode *b,ElemType x)  //Returns the node pointer whose data field is x
{
    BTNode *p;
    if (b==NULL)
        return NULL;
    else if (b->data==x)
        return b;
    else
    {
        p=FindNode(b->lchild,x);
        if (p!=NULL)
            return p;
        else
            return FindNode(b->rchild,x);
    }
}
BTNode *LchildNode(BTNode *p)   //Returns the left child node pointer of the * p node
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)   //Returns the right child node pointer of the * p node
{
    return p->rchild;
}
int BTNodeDepth(BTNode *b)  //Find the depth of binary tree b
{
    int lchilddep,rchilddep;
    if (b==NULL)
        return(0);                          //The height of the empty tree is 0
    else
    {
        lchilddep=BTNodeDepth(b->lchild);   //Find the height of the left subtree as lchilddep
        rchilddep=BTNodeDepth(b->rchild);   //Find the height of the right subtree as rchilddep
        return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
    }
}
void DispBTNode(BTNode *b)  //Output binary tree in parenthesis
{
    if (b!=NULL)
    {
        printf("%c",b->data);
        if (b->lchild!=NULL || b->rchild!=NULL)
        {
            printf("(");
            DispBTNode(b->lchild);
            if (b->rchild!=NULL) printf(",");
            DispBTNode(b->rchild);
            printf(")");
        }
    }
}
void DestroyBTNode(BTNode *&b)   //Destroy binary tree
{
    if (b!=NULL)
    {
        DestroyBTNode(b->lchild);
        DestroyBTNode(b->rchild);
        free(b);
    }
}
void preOrder(BTNode *b)
{
    if(b != NULL)
    {
        printf("%c",b->data);
        preOrder(b->lchild);
        preOrder(b->rchild);
    }
}
void countNode(BTNode *b,int &n)
{
    if(b!=NULL)
    {
        n++;
        countNode(b->lchild,n);
        countNode(b->rchild,n);
    }
}

Screenshot of operation results:

Topics: C++ data structure