Test: Computer Science

Consider the following code:

import java.util.ArrayList;

public class MethodClass5 {

     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 = {"Thomas Aquinas","Thomas Cajetan","Thomas Prufer","Thomas the Tank Engine","Thomas the Bread-Eater"};

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

          for(String s : traversedNames) {

               System.out.println(s);

          }

     }

}

1.

What is the output for the main method above?

Thomas the Tank Engine

Thomas Prufer

Thomas the Bread-Eater

Thomas Aquinas

Thomas Cajetan

 

Thomas the Bread-Eater

Thomas Aquinas

Thomas the Tank Engine

Thomas Prufer

Thomas Cajetan

 

Thomas the Bread-Eater

Thomas the Tank Engine

Thomas Prufer

Thomas Cajetan

Thomas Aquinas

Thomas Aquinas

Thomas the Tank Engine

Thomas Prufer

Thomas the Bread-Eater

Thomas Cajetan

 

Thomas Aquinas

Thomas Cajetan

Thomas Prufer

Thomas the Bread-Eater

Thomas the Tank Engine

 

 

1/3 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