Computer Science › Traversals
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
return parse(this,parseOrder);
}
private static ArrayList
ArrayList
if(node == null) {
return(retVal);
}
ArrayList
ArrayList
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
for(String s : traversedNames) {
System.out.println(s);
}
}
What is the output for this method?
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:
What's the best way to traverse this list in Swift (iOS)?
var list: \[Int\] = \[0, 1, 2, 3, 4, 5\]
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);
Traverse and print out this list
List
integers.add(1);
integers.add(2);
integers.add(3);
Given the following tree structure:
What is the pre-order traversal of the tree?
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.
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
return parse(this,parseOrder);
}
private static ArrayList
ArrayList
if(node == null) {
return(retVal);
}
ArrayList
ArrayList
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
for(String s : traversedNames) {
System.out.println(s);
}
}
}
What is the output for the_main_ method above?