简体   繁体   English

我想将左平衡二叉树的数组表示形式从按顺序转换为级别顺序表示形式:

[英]I want to convert an array represeantation of left balanced binary tree from In-order to Level order representation:

As shown in the following fig, I have a program which stores data of a tree in in-order manner. 如下图所示,我有一个程序来按顺序存储树的数据。

在此处输入图片说明

for example: A[7]={0,1,2,3,4,5,6}; 例如: A[7]={0,1,2,3,4,5,6};

I want that to convert in level-order manner: Such that the first node is root, and left children can be found at 2i+1 the location, right child can be found at 2i+2 th location, and parent of any node can be found at i/2th location. 我希望以级别顺序的方式进行转换:这样第一个节点是根,可以在2i + 1位置找到左子节点,在2i + 2位置找到右子节点,并且任何节点的父节点都可以在第i / 2个位置找到。

Namely: 即:

B[0]=A[3]
B[1]=A[1]
B[2]=A[5]
B[3]=A[0]
B[4]=A[2]
B[5]=A[4] and
B[6]=A[6]

I am stuck and unable to find any strategy. 我陷入困境,无法找到任何策略。

I need to perform this operation to make a compatible tree structure for an algorithm I've written earlier. 我需要执行此操作,以为我之前编写的算法创建兼容的树结构。

The data set contains thousand of nodes and it's impossible to do it manually. 数据集包含数千个节点,因此无法手动进行。 Kindly suggest a iterative or recursive algorithm for that. 请为此提出一种迭代或递归算法。

Thanks. 谢谢。

Though not much confidence... 虽然信心不大...

#include <stdio.h>

void toOrder(int a[], int b[], size_t index, size_t size, size_t a_size){
    int i;

    if(index >= a_size) return;
    if(size == 1){
        b[index] = a[0];
        return;
    }
    size /=2;
    b[index]=a[size];
    toOrder(&a[0]     , b, index * 2 + 1, size, a_size);
    toOrder(&a[size+1], b, index * 2 + 2, size, a_size);
}


#define SIZE 7

int main(void){
    int a[SIZE] = {0,1,2,3,4,5,6};
    int b[SIZE];
    int i;

    toOrder(a, b, 0, SIZE, SIZE);
    for(i=0;i<SIZE;++i)
        printf("b[%d]=%d\n", i, b[i]);
    printf("\n");

    return 0;
}

Here's an algorithm that takes one record-keeping sweep and then one output. 这是一种算法,先进行一次记录保持扫描,然后进行一次输出。

Take an array of the size of the tree, n. 取树大小为n的数组。

Create a record-keeping array depths of size depth, initialized to 0. Each entry is how many nodes are at depth i. 创建大小为depth的记录保持数组深度 ,深度初始化为0。每个条目是深度i处有多少个节点。

Walk the tree, noting each node's depth. 在树上行走,注意每个节点的深度。 At each node: 在每个节点上:

depths[d] += 1

This'll leave you with a list of how many nodes are at each depth. 这将为您提供每个深度有多少个节点的列表。

Make a new array, depth-offset . 制作一个新的数组depth-offset Where depth-offset[i] = depth-offset[i-1] + depths[i-1] and depth-offset[0] = 0. This shows you where a node of a given depth starts in the output array. 其中depth-offset [i] = depth-offset [i-1] + depths [i-1]depth-offset [0] =0。这显示给定深度的节点在输出数组中的起始位置。 Now it's just a matter of walking and placing, while incrementing the position of the next node at that level. 现在只需要走动和放置,同时在该级别增加下一个节点的位置即可。

Walk the tree in-order, noting the depth again. 按顺序行走树,再次注意深度。

offset = depth-offset[d]
output[offset] = value-at-node
depth-offset[d] = depth-offset[d]+1

output should be your list. 输出应该是您的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM