Test: Computer Science

Consider the code below:

public static class BTNode {

     public static final int PARSE_IN = 1;

     public static final int PARSE_PRE = 2;

     public static final int PARSE_POST = 3;

     String name;

     BTNode lPointer,rPointer;

     public BTNode(String s) {

          name = s;

          lPointer = rPointer = null;

     }

    

     public void insert(String s) {

          insert(this,s);

     }

    

     private static void insert(BTNode node,String s) {

          int comparison = s.compareTo(node.name);

          if(comparison < 0) {

               if(node.lPointer != null) {

                    insert(node.lPointer,s);

               } else {

                    node.lPointer = new BTNode(s);

               }

          } else if(comparison > 0) {

               if(node.rPointer != null) {

                    insert(node.rPointer,s);

               } else {

                    node.rPointer = new BTNode(s);

               }

          }

     }

    

     public ArrayList<String> parse(final int parseOrder) {

          return parse(this,parseOrder);

     }

    

     private static ArrayList<String> parse(BTNode node, final int parseOrder) {

          ArrayList<String> retVal = new ArrayList<String>();

          if(node == null) {

               return(retVal);

          }

          ArrayList<String> leftList = parse(node.lPointer,parseOrder);

          ArrayList<String> rightList = parse(node.rPointer,parseOrder);

          if(parseOrder == PARSE_PRE) {

               retVal.add(node.name);

               retVal.addAll(leftList);

               retVal.addAll(rightList);

          } else if (parseOrder == PARSE_POST) {

               retVal.addAll(leftList);

               retVal.addAll(rightList);

               retVal.add(node.name);

          } else {

               retVal.addAll(leftList);

               retVal.add(node.name);

               retVal.addAll(rightList);

          }

          return retVal;

     }

}

public static void main(String[] args) {

     String[] names = {"Hervaeus","Peter Auriol","Guiral","Felix","Lila","Lola","Yippy","Yiiiipppy","Acton","Pierce","Betty"};

     BTNode node = new BTNode(names[0]);

     for(int i = 1; i < names.length; i++) {

          node.insert(names[i]);

     }

     ArrayList<String> traversedNames = node.parse(BTNode.PARSE_IN);

     for(String s : traversedNames) {

          System.out.println(s);

     }

}

1.

What is the output for this method?

Peter Auriol

Hervaeus

Guiral

Acton

Betty

Felix

Lila

Lola

Yippy

Pierce

Yiiiipppy

There is an error in the recursion in BTNode.

Acton

Betty

Felix

Guiral

Hervaeus

Lila

Lola

Peter Auriol

Pierce

Yiiiipppy

Yippy

Betty

Acton

Felix

Guiral

Lola

Lila

Pierce

Yiiiipppy

Yippy

Peter Auriol

Hervaeus

Hervaeus

Guiral

Felix

Acton

Betty

Peter Auriol

Lila

Lola

Yippy

Yiiiipppy

Pierce

1/2 questions

0%

Access results and powerful study features!

Take 15 seconds to create an account.
Start now! Create your free account and get access to features like:
  • Full length diagnostic tests
  • Invite your friends
  • Access hundreds of practice tests
  • Monitor your progress over time
  • Manage your tests and results
  • Monitor the progress of your class & students
By clicking Create Account you agree that you are at least 13 years old and you agree to the Varsity Tutors LLC Terms of Use and Privacy Policy.
Learning Tools by Varsity Tutors