Fourth Homework 4 - Trees and Binary Trees

1. Learning summary

1.1 Tree structure mind map

1.2 Tree structure learning experience

Can't learn, can't learn, there are problems everywhere, say goodbye.

2.PTA experiment work

2.1

7-1 Restore Binary Tree (25 points)

7-8 jmu-ds-binary tree leaf node weighted path length sum (25 points)

6-4 jmu-ds-expression trees (25 points)

2.2 Design ideas (pseudo code or flowchart)

 

7-1 Restore Binary Tree (25 points)

bt*findTree(char*a,char*b,int length) Preorder
traversal to determine the head node, in-order traversal to determine the leaf node and its parent node
{
if (is empty tree)
return null;
open up a memory space for a bt structure , assign to T and assign
the value of b to T's data
for(i!=length)
{
find the same value
when the value of data is found in the array a
break
}
Recursively visit the left and right subtrees of T respectively
return T
}

int GetHeight(bt *T)
{
if(T==NULL)
return 0

else
{
recursively access the left and right subtrees
, return when the leaf is accessed, and select the longest path at the same time
}
}

7-8 jmu-ds-binary tree leaf node weighted path length sum (25 points)

BTree CreateBTree(int i)
{
Set BTree variable BT
if(i>str.size()-1)return NULL;
if(is '#', it is an empty node) return NULL;
open up a space for BT
BT->data =str[i];
lc of BT should be the 2*i element
rc should be the 2*i+1 element
return BT;

}

 

void GetWpl(BTree BT,int &wpl,int h)

{
Define h to indicate depth
First judge whether it is a leaf node
if(BT->Left==NULL&&BT->Right==NULL)
{
wpl=depth * value of current data,
depth reset to zero
}
h++
If it is an empty node, return is
non-empty and non-leaf
Recursively access its left and right subtrees
GetWpl(BT->Left,wpl,h);
GetWpl(BT->Right,wpl,h);
}

6-4 jmu-ds-expression trees (25 points)

void InitExpTree(BTree &T,string str)
{
    Define the node where the stack shu saves the tree
    Define stack ch to save characters
    ch底为'#'
    while(str[i])
    {
        if (is a number)
        {
            Numbers are leaf nodes, create leaf nodes, i ++ 
        }
        else
        {
            divided into three cases
            if (top of stack < string)
            {
                push, i ++ ;
            }
            else if(=)
            {
                It is a left parenthesis, you can directly pop it from the stack
            }
            else
            {
                Pop the first of ch and the first two of shu
                and store the two nodes as their right and left subtrees
                Push the parent node back to the shu stack  
            }
        }
    }
    while(ch.top()!='#')
    {Action when 
        repeating >
    }
}

2.3 Code screenshots

7-1 Restore Binary Tree (25 points)

 

7-8 jmu-ds-binary tree leaf node weighted path length sum (25 points)

 

6-4 jmu-ds-expression trees (25 points)

 

 

 

 

2.4 PTA Submission List Instructions.

7-1 Restore Binary Tree (25 points)

At first, I skipped the function problem to do this, but I couldn't build a tree. After learning one, preorder traversal can determine the location of the root node, and inorder traversal can determine the parent nodes of two child nodes. Returning the height is the easy part.

7-8 jmu-ds-binary tree leaf node weighted path length sum (25 points)

At the beginning, the value of wpl was returned with the int type, but the result has not been able to be called. The whole process was very painful, and after being instructed by Brother Guang, a global variable was set up.

6-4 jmu-ds-expression trees (25 points)

At the beginning, the method of counting the number of loops eliminates the error of the tree-building function. But the function that calculates the value always overflows. After many investigations, the problem is that I forgot to re-push the parent node that determines the left and right subtrees.

When dividing, I used return at first, but it will return a value. Later, I read Brother Guang's code and used exit (although it cannot be compiled in the compiler)

 

3. Screenshot of the PTA final ranking of this week's topic set

Score: 2

 

4. Read the code (required)

 

Use recursive method to simultaneously determine whether the child nodes of two trees are the same.

5. Screenshot of code Git commit record

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325300893&siteId=291194637