修剪二叉树的数量

时间:2011-11-27 20:34:45

标签: algorithm binary-tree

众所周知,任何有序的林都可以用唯一的二叉树来表示。 任何人都可以帮我找到一个确定二叉树修剪数的算法吗?

4 个答案:

答案 0 :(得分:2)

    If a binary tree is represented in L and R array, e.g. 
    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

    Then this algorithm will help you in getting the pruning number:

        public int getPruningNumber()
{
    int index=1;
    int l=0;
    if(L[index-1]!=0)
    {
        l=getPruningNumberRecursilvely(index);
                   //System.out.println("l="+l);
    }
    int r=0;
    boolean allRightSubTreeHaveSamePruningNo=true;
    while(R[index-1]!=0)
    {
        index=R[index-1];
        int k=getPruningNumberRecursilvely(index);
        if(r==0)
            r=k;
        else if(k>r)
        {
            r=k;
            allRightSubTreeHaveSamePruningNo=false;
        }
        else if(k<r)
            allRightSubTreeHaveSamePruningNo=false;
          //    System.out.println("k="+k);
    }
    if(allRightSubTreeHaveSamePruningNo&&r==l)
        return l+1;

    return r>l?r:l;
}
private int getPruningNumberRecursilvely(int index)
{
    if(L[index-1]==0&&R[index-1]==0)
        return 1;
    int l=0,r=0;
    if(L[index-1]!=0)
    {
        l=getPruningNumberRecursilvely(L[index-1]);
      //    System.out.println("in rec l::"+l+" for index:"+index);

        boolean allRightSubTreeHaveSamePruningNo=true;
        index=L[index-1];
        while(index!=0&&R[index-1]!=0)
        {
            int k=getPruningNumberRecursilvely(R[index-1]);
            index=R[R[index-1]-1];
            if(r==0)
                r=k;
            else if(k>r)
            {
                r=k;
                allRightSubTreeHaveSamePruningNo=false;
            }
     //         System.out.println("in rec k::"+" for index:"+index);
        }
        if(allRightSubTreeHaveSamePruningNo&&r==l)
            return l+1;

        return r>l?r:l;
    }
    return 1;
} 

答案 1 :(得分:2)

     1. Yes for the given case the pruning number will be 2 and i guess my program is also  giving the same answer. 

               Given Binary tree:
                           1                                    1
                          /                                    / \
                         2        equivalent forest           2   3
                          \
                           3
     2. All the right children of the root of binary tree is representing a tree in forest and so does the right of this right child of root. i.e.
                             1
                            /  \
                           2    5
                          / \   / \
                         3   4 6   10
                                \  
                                 7
                                / \
                               8   9

   this binary tree is representing this forest.
                   1            5      10
                  / \          / \ \    
                 2   4        6   7 9  
                /                / 
               3                8


   3. i m pasting the full code to compute the pruning number:-

  public class PruningNumber {

private int L[]=null;
private int R[]=null;
public PruningNumber(int L[],int R[])
{
    this.L=L;
    this.R=R;
}
public int getPruningNumber()
{
    int index=1;
    int l=0;
    if(L[index-1]!=0)
    {
        l=getPruningNumberRecursilvely(index);

    }
    int r=0;
    boolean allRightSubTreeHaveSamePruningNo=true;
    while(R[index-1]!=0)
    {
        index=R[index-1];
        int k=getPruningNumberRecursilvely(index);
        if(r==0)
            r=k;
        else if(k>r)
        {
            r=k;
            allRightSubTreeHaveSamePruningNo=false;
        }
        else if(k<r)
            allRightSubTreeHaveSamePruningNo=false;

    }
    if(allRightSubTreeHaveSamePruningNo&&r==l)
        return l+1;

    return r>l?r:l;
}
private int getPruningNumberRecursilvely(int index)
{
    if(L[index-1]==0&&R[index-1]==0)
        return 1;
    int l=0,r=0;
    if(L[index-1]!=0)
    {
        l=getPruningNumberRecursilvely(L[index-1]);


        boolean allRightSubTreeHaveSamePruningNo=true;
        index=L[index-1];
        while(index!=0&&R[index-1]!=0)
        {
            int k=getPruningNumberRecursilvely(R[index-1]);
            index=R[R[index-1]-1];
            if(r==0)
                r=k;
            else if(k>r)
            {
                r=k;
                allRightSubTreeHaveSamePruningNo=false;
            }

        }
        if(allRightSubTreeHaveSamePruningNo&&r==l)
            return l+1;

        return r>l?r:l;
    }
    return 1;
}

public static void main(String args[])
{

    int L[]={2,0,4,0};
    int R[]={3,0,0,0};

    System.out.println(new PruningNumber(L,R).getPruningNumber());

    int L1[]={2,3,0,0,6,0,0};
    int R1[]={5,4,0,0,7,0,0};
    System.out.println(new PruningNumber(L1,R1).getPruningNumber());

    //the case u r discussing
    int L3[]={2,0,0};
    int R3[]={0,3,0};


    PruningNumber pruningNumber=new PruningNumber(L3,R3);
    System.out.println(pruningNumber.getPruningNumber());

    int L4[]={2 ,3,0,5,6,0,0,9,0,0, 12,13,0,15,0,17,18,0,0,21,0,0,0};
    int R4[]={11,4,0,8,7,0,0,10,0,0,0,14,0,16,0,20,19,0,0,22,0,0,0};
    pruningNumber=new PruningNumber(L4,R4);
    System.out.println(pruningNumber.getPruningNumber());

    int L5[]={2,3,0,0,6,0,0,0};
    int R5[]={0,5,4,0,8,7,0,0};
    pruningNumber=new PruningNumber(L5,R5);
    System.out.println(pruningNumber.getPruningNumber());

    int L6[]={2,3,4,0,0,0,0};
    int R6[]={0,0,0,5,6,7,0};
    pruningNumber=new PruningNumber(L6,R6);
    System.out.println(pruningNumber.getPruningNumber());
  }
    }

答案 2 :(得分:1)

我对这个解决方案有点怀疑,特别是对于案例

int L[]={2,0,4,0};
 int R[]={3,0,0,0};

为什么它的修剪数是2?

它的森林由

提供
    1   && 3
   /      /
  2      4

所以它的修剪数应该是1.答案显示为2

答案 3 :(得分:0)

算法检查L [index-1] ... L [0]为0。 在上面的二叉树表示中,从L [1]开始(你的索引从1开始,而不是0)。

您的算法将始终返回1.

还要考虑这种情况:

L[1]=2, R[1]=0 
L[2]=0, R[2]=3
L[3]=0, R[3]=0

这应该导致修剪数为2.原因是因为3在2的右边,这意味着在有序的森林2和3中是兄弟姐妹和1的孩子。细丝是2和如图3所示,将在第一次修剪中移除,然后在第二次修剪中移除1。