How to generate all possible binary tree in reverse lexicographic order

Standard

Suppose we are representing binary as L and R array, for example
The tree would be represented then
1            L[1]=2, R[1]=4
/  \           L[2]=0, R[2]=3
2   4        L[3]=0, R[3]=0
\             L[4]=0, R[4]=0
3

Now, if we read tree in preorder, in such a way that if node present it print 1 otherwise 0 then this tree will also be represented as 110100100. Now the problem we are going to solve here is: “ if we have to generate all possible binary tree for a give number of node in such a way that if we traverse the tree in preorder just described then it should be in reverse lexicographic order.

Now, first we will develop the algo then will code it.

Our first step will be to observe the pattern and see what will be the next generated tree for a given tree. Suppose we have this tree

                       1

                  /

             2

           /

        3

tree in 1,0 format : 1110000

then the next generated tree in the reverse lexicographic order will be this

                         1

                     /

                  2

                     \

                       3

tree in 1,0 format : 1101000

After looking at then newly generated tree, we can find that node 3 has moved and its moved from left of 2 to right of 3.  Let us generate one more tree from the last generated tree and the new tree will look like this:

                1

            /       \

          2            3

tree in 1,0 format : 1100100 (Note: observe this no for all the generated tree, you will find its decreasing)

and next  tree generated from this tree is:

               1

             /   \

        null      2

                   /      \

              3          null

tree in 1,0 format : 1011000 (Note: observe this no for all the generated tree, you will find its decreasing)

If we compare with the last generated one then we will find that this time node 2 has moved and now node 3 is left child of node 2.  If we generate  some more trees then we will find that this algo:

1. take the nth node and try to find the best position for it i.e. the next possible position for it in preorder.

2. if we are not able to find any position for nth node then try to find for n-1 th node and moved the node there

and then we make all node greater then this left child of its parent.

For example :-

1 we take the first generated tree as an example here:

1. let us take the nth node , 3 in this case, now try to find the next possible position for it in the tree after traversing it in preorder then we will find that it it is the right of 2nd node.

2. move the 3 rd node to right of 2nd node and since it has no child node, hence this is the answer.

2. Now try, the last generated tree:

1. let us take the nth node first here i.e. 3rd node in this case, now if we try to next possible position. We see there is no possible next position for so

2. try the node higher then it i.e. 2nd node. We find that next possible position for it is right of 1. Now shift 2nd to this new position and move 3 rd as its left child, if we have 4 th node then we would have made it left of 3rd.

The most important this, we have to do now, is to find the way to find the next possible position for the given node. After,  we do pre-order traversal of the tree then we will find that the 3 rd node traversed after traversing the selected node is the possible position for it. For example,

take the example of first generated tree:-

                            1

                       /       \

                      2           null 4

                  /    \

              3         null 3

             /    \

     null  1   null 2

now if we try to find , the possible position for 3 we see that after traversing node 3 in pre-order, we traverse null 1 first then null 2 and then null3 which is the possible position and which is exactly third node traversed after traversing node 3. Hence our logic is correct in this example.

Now, we consider the last generated tree, the parent tree is

                                      1

                                  /      \

                              2             3

                         /     \             /   \

                 null1  null2     null3  null4

Now, we try to find,  best  position for nth node, i.e. 3 rd node, now if we do pre-order traversal, then we find that after traversing node 3rd we can only 2 nodes which are its child and hence there is no possible shift for node 3 rd hence we will do the same for node 2 nd and we will find that 3 rd node traversed after traversing node 2 is the node where currently node 3 is, hence we shift 2 to this new position and make node 3 its left child and if we would has 4 th node then it would have been the left child of node 3rd and so on.

Before we start coding, one more point left to discuss is: from where to start, what actually I mean is what will be the first base tree?

For, generating the reverse lexicographic order  tree, the firs tree should be the left linear tree i.e.

                                                   1

                                            /

                                    2

                                 /

                            3

                        .

                      .

                  n

and the final tree will be right linear tree i.e.

1

   \

      2

        \

           3

               .

                  .

                   n


 

Advertisements