简体   繁体   中英

Convert Level Order Traversal to Inorder Traversal of a complete binary tree

Given the level order traversal of a complete binary tree in an array, how to store the inorder traversal of the said tree in the given array, without building up the tree. This is what I came up with.

void recurse (int *inp, int size_array, int *output, int iter_a, int &iter_b)
{
    if (iter_a>=size_array)
        return;

    recurse (inp,size_array,output,2*iter_a+1,iter_b);


    output[iter_b] = inp[iter_a];
    iter_b++;


    recurse (inp,size_array,output,2*iter_a+2,iter_b);

}

Is there an in-place non-recursive O(n) solution for the said problem?

this is the function that i created to store the inorder traversal in array e from levelorder traversal of array a,n is length of array a.Set initial k=0 and x=0.

void convert(long long int a[],long long int e[],long long int n,long long int k,long long int x)
{
    if((2*k+1)>=n||(2*k+2)>=n)
        return;
    convert(a,e,n,2*k+1,x);
    e[x]=a[k];
    x++;
    convert(a,e,n,2*k+2,x);

    return;
}

This is a iterative solution for converting level order to inorder but not inplace

private class Entry{
    int data;
    int pos;

    Entry(int data, int pos){
        this.data = data;
        this.pos = pos;
    }
}

public void convertLevelToInorder(int[] levelOrder){

    // nodes are stored from index 1

    int len = levelOrder.length;
    int[] inOrder = new int[len];

    Stack<Entry> stack = new Stack<Entry>();

    int pos = 1;
    int count = 1;

    while(!stack.isEmpty() || pos < len){

        while(pos < len && levelOrder[pos] != -1 ){
            stack.push(new Entry(levelOrder[pos],pos));
            pos = pos*2;
        }

        Entry e = stack.pop();
        inOrder[count++] = e.data;
        pos = e.pos*2+1;
    }

    for(int i=1;i<len;i++)
        System.out.print(inOrder[i] + "  ");
    System.out.println();
}

I have used the observation that, for a complete binary tree(this can easily be changed for other sizes), the mapping from level order to in order goes like this:

Let the complete binary tree be of size (2^m) - 1 = n

G = (n+1)/2

Then mapping is observed to be(using properties of tree sizes)

G...

G/2, G/2+G...

G/4, G/4+G/2, G/4+2G/2, G/4+3G/2...

etc etc...

until G/exponent=1

That is, the offset goes from G to 1, and the iterable is twice that.

void inorderconversion(int n,int *inp,int *out){
    int G = (n+1)/2;
    int temp = 1,fac = 1;

    for(int i=0;i<temp;i++){
        int j;
        for(j=i;j<temp;j++){
            int outindex=(2*(j-i)+1)*G, inindex=j+1;
            
            out[(2*(j-i)+1)*G -1] = inp[j];
            //printf("%d %d\n", outindex, inindex);
        }
    
        {
            fac = fac*2;
            temp = temp+fac;
        }
        i =j-1;
        G = G/2;
        
        if(G == 0){
            break;
        }
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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