Cattle-off - an array of memory beef and binary tree

Learning is like rowing upstream

Taurus storage array with binary tree

Title Description

Is an important nonlinear tree data structure, Intuitively, which are data elements (referred to as nodes in the tree) organized by the relationship between the branch structure, much like in nature as a tree. Tree widespread in the objective world, such as genealogy and various social organizations of human society are available tree image representation. Tree in the computer also been widely used in the art, such as when compiling a source program, the source of available source tree represents the syntactic structure. Another example is in the database system, the tree structure is also an important organizational form of information. All issues are available in a hierarchy tree to describe. Full binary tree, complete binary tree, binary tree sort.
When the binary tree is a full binary tree, the following rules can be stored:
1. array subscript 0 is not used
left child node 2. The node i is at the position (2 i);
the right child node 3. The node at the position i to (2
i + 1);
parent node 4. the node i is at the position (i / 2);
5. the root node is stored in the array subscript 1 position.
Here Insert Picture Description
The figure is a full binary tree, for each node I, I 2 is its left subtree, I 2 +. 1 is a right subtree, i / 2 is its parent node.
Of course, when the tree is not as full binary tree can actually store.
Here Insert Picture Description
The figure is a full binary tree is not the case, let's use some imaginary point supplemented into a full binary tree.
Or the root node is stored into the first array position.
Then for the next labeled node i, left his child subscript i 2, its right child of subscript i 2 + 1, subscript its parent node is i / 2.
To give you a length n of the array, which stores a binary tree, the array contains only 1 and positive integers, and n is a whole integer array is from 1 to the size of the contiguous tree, will not be 1, 2, 3,5,6, 4 broken this absence.
Would you please tell me the size and the root of the tree, father and print each node in the order, left child, right child who are?

Enter a description:

The first line is a positive integer n-(1≤n≤10 . 5 ), represents the size of the array.
The next line n integers, comprising only the integer 1 and n, if the input is -1, it means that the position of the node is empty, otherwise it is the node number. Ensure that the entire array of data input node number from 1 to the size of the contiguous tree.

Output Description:

For each case:
firstly output in the first line: "The size of the tree is X", X represents the size of the tree, the size of the tree is the number of nodes in a binary tree.
Next, in the second line of output: "Node X is the root node of the tree", X represents the root node of the binary tree, i.e. node position array subscript 1 is stored.
The next output row size, size indicates the size of the tree.
Each line format is: "The father of node I is X, the left child is Y, and the right child is Z", the meaning of I, X, Y, Z is: a parent node I, node is X, it the children left for the Y, its right child to Z.
If the node is the root node, no parent node, then X -1.
If the node has no left child, then Y is -1.
If the node has no right child, then Z is -1.

Sample Input

7
1 2 3 4 5 6 7

Sample Output

The size of the tree is 7
Node 1 is the root node of the tree
The father of node 1 is -1, the left child is 2, and the right child is 3
The father of node 2 is 1, the left child is 4, and the right child is 5
The father of node 3 is 1, the left child is 6, and the right child is 7
The father of node 4 is 2, the left child is -1, and the right child is -1
The father of node 5 is 2, the left child is -1, and the right child is -1
The father of node 6 is 3, the left child is -1, and the right child is -1
The father of node 7 is 3, the left child is -1, and the right child is -1

Sample Input

7
3 -1 2 -1 -1 1 4

Sample Output

The size of the tree is 4
Node 3 is the root node of the tree
The father of node 1 is 2, the left child is -1, and the right child is -1
The father of node 2 is 3, the left child is 1, and the right child is 4
The father of node 3 is -1, the left child is -1, and the right child is 2
The father of node 4 is 2, the left child is -1, and the right child is -1

Analysis of the wrong questions

& # ¥% ...... ¥ ¥ & ( + ... (* - ** ... &% & ...... This is a segment array, I want to open four times the memory ye forget the pinch ???

This problem pure analog, not a bit tricky!
Write an outline, or not to write, direct AC to get started
Time to AC

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<string>
#include<math.h>
#include<stdio.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const ll ll_inf=9223372036854775807;
const int int_inf=2147483647;
inline ll read() {
    ll c=getchar(),Nig=1,x=0;
    while(!isdigit(c)&&c!='-')c=getchar();
    if(c=='-')Nig=-1,c=getchar();
    while(isdigit(c))x=((x<<1)+(x<<3))+(c^'0'),c=getchar();
    return Nig*x;
}
#define read read()
int save[400005];
struct node {
    int l,r,f,m;
} ans[4000005];
bool cmp(node a,node b)
{
    return a.m<b.m;
}
int main() {
    int n=read;
    memset(save,-1,sizeof(save));
    int q=0;
    for(int i=1; i<=n; i++) {
        save[i]=read;
        if(save[i]!=-1)q++;
    }
    cout<<"The size of the tree is "<<q<<endl;
    for(int i=1; i<=n; i++) {
        if(save[i]!=-1) {
            cout<<"Node "<<save[i]<<" is the root node of the tree"<<endl;
            break;
        }
    }
    q=0;
    for(int i=1; i<=n; i++) {
        if(save[i]!=-1) {
            ans[q].m=save[i];
            ans[q].f=save[i/2];
            ans[q].l=save[i*2];
            ans[q++].r=save[i*2+1];
        }
    }
    sort(ans,ans+q,cmp);
    for(int i=0;i<q;i++)
    printf("The father of node %d is %d, the left child is %d, and the right child is %d\n",ans[i].m,ans[i].f,ans[i].l,ans[i].r);
}

Summary: careless moment, I failed, it could last 10 seconds of a red list of results ............

By- wheel month

book mountain road to, learning is endless suffering for the boat.

Published 32 original articles · won praise 12 · views 1190

Guess you like

Origin blog.csdn.net/qq_35339563/article/details/104226647