Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Level order insertion into a binary tree?

Suppose we are given a level order traversal output. How to construct a binary tree from that populated with the data at correct positions?

Please note that I'm not trying to sketch the tree from the given traversal output, but read off the traversal data from an array then populate a binary tree with it by actual coding in C.

Eg:

Let a[] = {A, B, C, D, E, F, G}; //the traversal output in an array

So level-order tree will look like this:

            A
           / \ 
          B   C
        / \  / \
       D   E F  G

Suppose there is a tree node structure like so:

typedef struct node
{
    char data;
    struct node* left;
    struct node* right;
}tree;

Now I'm trying to read a[] values and code this tree so that it looks like the diagram. There are many examples of level-order traversal but couldn't find anything relevant on actual coding for binary tree construction. This is sort of a "reverse of traversal".

Also please note that this is not homework, though I don't have problems tagging it if more people will notice it that way. :)

like image 372
AruniRC Avatar asked Jul 02 '11 07:07

AruniRC


People also ask

What is level order in binary tree?

What is the level order traversal of a tree? Level Order Traversal is the algorithm to process all nodes of a tree by traversing through depth, first the root, then the child of the root, etc. How do you do level order traversal? Level order traversal can be done by using a queue and traversing nodes by depth.

Does order of insertion matter in binary search tree?

If the insertions are done in an numerical order for a group of numbers, a binary tree becomes a linked list which affects the search performance (it becomes O(n)).

How do you print a binary tree in level order?

Level Order Binary Tree Traversal using Recursion: Find height of tree and run depth first search and maintain current height, print nodes for every height from root and for 1 to height and match if the current height is equal to height of the iteration then print node's data.

What is the order of level order traversal?

So the idea of level order traversal is: We start by processing the root node, then process all nodes at the first level, second level, and so on. In other words, we explore all nodes at the current level before moving on to nodes at the next level.


3 Answers

For a full (or filling) binary tree it's easy to convert a level traversal into any traversal because the children of a node at position n are at 2n and 2n+1 when the array is 1-indexed and at 2n+1 and 2n+2 when the array is 0-indexed.

So you can easily use that formula to turn it into your favorite traversal order for inserting nodes into a tree (like pre-order).

e.g. recursive psuedocode:

void fill( TreeNode* node, char a[], int arrayLength, int n ){
    // n is the position of the current "node" in the array
    if( n < arrayLength ){
        node->data = a[n];
        fill( node->left, a, arrayLength, 2n+1 );
        fill( node->right, a, arrayLength, 2n+2 );
    }
}
like image 39
trutheality Avatar answered Oct 02 '22 07:10

trutheality


One possible solution:

char a[SIZE] = {A,B,C,D,E,F,G}; 
    node* func(int index){
        if(index < SIZE){
            node *tmp = new node();
            tmp->data = a[index];
            tmp->left = func(2*index + 1);
            tmp->right = func(2*index + 2);
        }
        return tmp;
    }

stacktrace for the tree:

                                     A->a[0]
          B->func(2*0 + 1)=[1]                              C->func(2*0 + 2)=[2]
D->func(2*1 + 1)=[3]    E->func(2*1 + 2)=[4]        F->func(2*2 + 1)=[5]     G->func(2*2 + 2)=[6]
like image 174
Benny Tjia Avatar answered Oct 02 '22 05:10

Benny Tjia


This is like a BFS, so you can use a queue.

Notice that you always assign left child and then immediately after that the right child. So start with a queue containging the root. At each iteration pop the node from the queue and then read the next two values from the array (or stream). Make the first one the left child of the popped node and push it into the queue. Then make the second one a right child and push it into the queue. And so on until there are no elements left in the array (or stream).

like image 30
Petar Ivanov Avatar answered Oct 02 '22 05:10

Petar Ivanov