An array of binary tree

Claim:

Now a series of numbers, in order from the root is inserted into the binary tree. However, to be noted that the process of inserting the value of the left subtree is less than or equal to the parent node, the value of the right subtree of the parent node is greater than.

For example, the sequence 134 230, is inserted into the corresponding binary

                      1

                   /        \

                 0            3

                             /      \

                           2         4

                             \

                              3

After creating the binary tree from the root to the digital output of each row, each output in order from left to right.

 

Input:

The first line number sequence length N (1≤N≤11)

Next, the n numbers, each greater than or equal to 0

 

Output:

Whole grain digital binary tree. Each number a newline

 

Sample input:

1 3 4 2 3 0

 

Sample output:

0

2

3

 

Hint : when the position of the parent node i, left child node i * 2 + 1, the right child node is i * 2 + 2.

 

 

 

How to explain the array represents a binary tree:

An array of a [10], then it is represented as a complete binary tree (hereinafter, subscript figures only represent array, an array value does not represent)

                                          0

                                      /          \

                                    1             2

                                 /       \        /     \

                               3         4      5      6

                            /     \      /   

                           7      8   9

This is a complete binary tree representation of the array.

In other words, when we represent a binary tree with an array, the array does not need to conduct any operation,

But we believe that the awareness of this array is a binary tree, then follow the binary tree to deal with this array.

That is an array subscript follow the laws of the binary tree. For example, it is about the relationship between child and parent tree nodes.

Although our results are not complete binary tree this question, just as examples,

But we can imagine it as a full value for the complete binary tree -1, -1 then insert value to the node.

 

Analysis of ideas:

As can be seen from the tips, it's like a hash lookup (mapped to the array according to certain rules)

This rule is less left subtree of the parent node, the parent node is greater than the right subtree.

The parent and child about the subject of the relationship between the value of the tree has been given out.

The value of the node is not negative, so we can target an array of all assigned to -1, in order to determine whether there value target array.

So the essence of this question is to insert data into an empty array process.

When the output value is not what we need is a node-1

(We created this array, follow the prompts to insert data rules, in fact, the array as a complete binary tree)

So we insert in accordance with the result, according to the array index output on the line.

Following detailed code. (If the result is wrong, the array may be target opening is not big enough)

 

 

 1  1 #include<bits/stdc++.h>
 2  2 using namespace std;
 3  3 int main()
 4  4 {
 5  5     int n;
 6  6     cin>>n;
 7  7     int a[n];       //写入数据的数组
 8  8     for (int i = 0; i < n; i++) cin>>a[i];
 9  9     int target[100];     //空数组,用来接受映射结果
10 10     target[0]=a[0];      //树根是第一个位置
11 11     for (int i = 1; i < 100; i++) target[i]=-1;  //因为a[n]大于等于零
12 12     //所以我们可以把其他的数都赋为-1,来区分数组这个位置有没有存数
13 13     for (int i = 1; i < n; i++)   //这里开始插入
14 14     {
15 15         int j=0;                 //从树根开始找位置
16 16         while(target[j]!=-1)     //不等于-1的话说明这个位置已经插入数字了
17 17         {
18 18             if (a[i]<=target[j])    j=2*j+1; //判断大小,(小于等于父节点)检查左子树有没有插入
19 19             else j=2*j+2;                   //检查右子树
20 20         }
21 21         target[j]=a[i];           //找到位置后,赋值
22 22     }
23 23     int j=0;                     //开始从头开始找插入的数字
24 24     for (int i = 0; i < n; i++)  //数组大小为n,所以找到n个即可
25 25     {
26 26         while (target[j]==-1) j++; //说明这个位置是空的,继续找
27 27         cout<<target[j]<<endl;   //退出循环,说明找到了,把数字输出来
28 28         j++;                    //输出之后,继续找,所以j要递增
29 29     }
30 30     return 0;
31 31 }

Guess you like

Origin www.cnblogs.com/Arthas8086/p/12095005.html