Question

this is a decision tree base on pre-order Here is the tree fill in code

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);

below is my code, i tried to divide tree to left subtree and right subtree, and put them to an array list.but for each part, also need to consider the height of this sub-tree as a loop num

//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);

                }

but the Output are

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

the expected result should be

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

and the final result i want is

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

I know it may use SringBuilder to complete the indentation with 3 spaces right for each child decision. but don't know how to do it, and before that, need fix 'array input2'

New code here

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);

    }
Was it helpful?

Solution

actually you need to traverse the nodes and put the values in the arrays. assume you have this:

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);
   }
}

use it in your function like this:

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);

i hope i didnt forget something:)

edit1: traverse is static now edit2:nah, static isnt good because of tree...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top