预订决策二叉树打印输出

时间:2013-09-07 14:52:26

标签: java tree binary-tree stringbuilder preorder

这是一个基于预订的决策树 这是树填充代码

Position<Integer> n0 = tree.addRoot(0);
Position<Integer> n1 = tree.insertLeft(n0, 1);
Position<Integer> n3 = tree.insertRight(n0, 3);
Position<Integer> n2 = tree.insertLeft(n1, 2);
Position<Integer> r2 = tree.insertRight(n1, 1);
Position<Integer> r0 = tree.insertLeft(n2, 2);
Position<Integer> r1 = tree.insertRight(n2, 9);     
Position<Integer> r3 = tree.insertLeft(n3, 2);      
Position<Integer> r4 = tree.insertRight(n3, 8);     
Position<Integer> r5 = tree.insertLeft(r4, 4);      
Position<Integer> r6 = tree.insertRight(r4, 5);

下面是我的代码,我试图将树划分为左子树和右子树,并将它们放入数组列表。但是对于每个部分,还需要将此子树的高度视为循环数

//calculate height of heap
int a = (int) Math.floor(Math.log(tree.size()) / Math.log(2));

List<String> input1 = new ArrayList<String>();
List<String> input2 = new ArrayList<String>();

        Position<Integer> p1 = tree.root();
        Position<Integer> p2 = tree.root();

        //3 is the height of left side heap
        for(int i=0;i<3;i++)
        {       
            //left hand-side
            if (tree.hasLeft(p1) == true && tree.hasLeft(tree.left(p1))==true)
            {   
                input1.add("Y->Q"+tree.left(p1).element());             

                if (tree.hasLeft(tree.right(p1))==true)
                {
                    input1.add("N->Q"+tree.right(p1).element());
                }
                else
                {
                    input1.add("N->R"+tree.right(p1).element());
                }
            }
            else
            {
                input1.add("Y->R"+tree.left(p1).element());
                input1.add("N->R"+tree.right(p1).element());
            }
            p1 = tree.left(p1);

        }   

        //3 is the heap height of right
                for(int n=0;n<3;n++)
                {
                    //right hand-side
                    if (tree.hasRight(p2) == true && tree.hasLeft(tree.right(p2))==true)
                    {                       
                        input2.add("N->Q"+tree.right(p2).element());

                    }
                    else
                    {
                        input2.add("Y->R"+tree.left(p2).element());
                        input2.add("N->R"+tree.right(p2).element());
                    }

                    p2 = tree.right(p2);

                }

但输出

array input1: [Y->Q1, N->Q3, Y->Q2, N->R1, Y->R2, N->R9]
array input2: [N->Q3, N->Q8, Y->R4, N->R5]

预期结果应为

array input1: [Y->Q1, N->Q3, Y->Q2, N->R1, Y->R2, N->R9]
array input2: [Y->R2, N->Q8, Y->R4, N->R5]

我想要的最终结果是

Y->Q1:
   Y->Q2:
      Y->R2
      N->R9
   N->R1
N->Q3:
      Y->R2
      N->Q8
         Y->R4
         N->R5

我知道可以使用SringBuilder为每个孩子的决定完成3个空格的缩进。但不知道该怎么做,在此之前,需要修复'array input2'

此处的新代码

public static void printDecisionTree(BinaryTree<Integer> tree) {    

        Position<Integer> root = tree.root();
        boolean isLeft =true;

        StringBuilder output = new StringBuilder();
        String LS = System.getProperty("line.separator");

           if(tree.hasLeft(root) || tree.hasRight(root)){
               if(isLeft)
                  output.append("Y->Q"+root.element()+LS);
               else
                  output.append("N->Q"+root.element()+LS);
           }
           else{
               if(isLeft)
                  output.append("Y->R"+root.element()+LS);
               else
                  output.append("N->R"+root.element()+LS);
           }
           if (tree.hasLeft(root)&& isLeft==true){
               printDecisionTree(tree);
           }
           if (tree.hasRight(root)&& isLeft==false){
               printDecisionTree(tree);
           }
           System.out.print(output);

    }

1 个答案:

答案 0 :(得分:1)

实际上,您需要遍历节点并将值放入数组中。 假设你有这个:

public void traverse (Position<Integer> root, List<String> input, boolean isLeft){
   if(tree.hasLeft(root) || tree.hasRight(root)){
       if(isLeft)
          input.add("Y->Q"+root.element());
       else
          input.add("N->Q"+root.element());
   }
   else{
       if(isLeft)
          input.add("Y->R"+root.element());
       else
          input.add("N->R"+root.element());
   }
   if (tree.hasLeft(root)){
       traverse(tree.left(root), input, true);
   }
   if (tree.hasRight(root)){
       traverse(tree.right(root), input, false);
   }
}

在你的函数中使用它:

List<String> input1 = new ArrayList<String>();
List<String> input2 = new ArrayList<String>();

Position<Integer> left = tree.left(tree.root());
Position<Integer> right = tree.right(tree.root());

traverse(left, input1, true);
traverse(right, input2, false);

我希望我没有忘记一些事情:)

edit1:traverse现在是静态的 edit2:不,静电不好因为树......