Traversals

Practice Questions

Computer Science › Traversals

Questions
8
1

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 parse(final int parseOrder) {

return parse(this,parseOrder);

}

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

ArrayList retVal = new ArrayList();

if(node == null) {

return(retVal);

}

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

ArrayList 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 traversedNames = node.parse(BTNode.PARSE_IN);

for(String s : traversedNames) {

System.out.println(s);

}

}

What is the output for this method?

2

Suppose you are given an array of integers:

int array\[\] = {1,2,3,4,5};

and the following method:

public static void printArray(int\[\] arr)

{

for (int a = 0; r < arr.length-1; a++)

{

if (a%2==0)

{

System.out.println(arr\[a\]);

}

}

}

After the method cacll printArray(array) is called, the output would be:

3

What's the best way to traverse this list in Swift (iOS)?

var list: \[Int\] = \[0, 1, 2, 3, 4, 5\]

4

The function recur is defined as follows:

public int recur(int x)

{

if (x <= 1)

{

return 1;

}

else

{

return x + recur(x/2);

}

}

How many times is recur called in the following declaration?

int num = recur(6);

5

Traverse and print out this list

List integers = new ArrayList();

integers.add(1);

integers.add(2);

integers.add(3);

6

TREE TRAVERSALS

Given the following tree structure:

Blank flowchart   new page

What is the pre-order traversal of the tree?

7

Which of the following code performs a multiplication by 5 of the elements of the array defined as:

int\[\]\[\] vals = new int\[50\]\[100\];

Presume that the array has been properly initialized and filled with values.

8

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 parse(final int parseOrder) {

return parse(this,parseOrder);

}

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

ArrayList retVal = new ArrayList();

if(node == null) {

return(retVal);

}

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

ArrayList 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 traversedNames = node.parse(BTNode.PARSE_POST);

for(String s : traversedNames) {

System.out.println(s);

}

}

}

What is the output for the_main_ method above?

Return to subject