als365
BAN USERHere is the general outline for a DFS
Build a tree with the supplied words
Make a stack s
s.push(root of the tree)
bool found = false;
while (s.isEmpty == false) {
node = s.pop();
if (node.data.startsWith("foo") || node.data.compareTo("foo") == 0) { // let foo be the string we are checking
found = true;
print(node.data);
s.push(node.left);
s.push(node.right);
}
else if (found == false && node.data.compareTo("foo") > 0) { // "foo" < node.data
s.push(node.left);
}
else if (found == false && node.data.compareTo("foo") < 0 { // "foo" > node.data
s.push(node.right);
}
}
Here is the general outline for a BFS
Build a tree with the supplied words
Make a queue q
q.enqueue(root of the tree)
bool found = false;
while (q.isEmpty == false) {
node = q.dequeue();
if (node.data.startsWith("foo") || node.data.compareTo("foo") == 0) { // let foo be the string we are checking
found = true;
print(node.data);
q.enqueue(node.left);
q.enqueue(node.right);
}
else if (found == false && node.data.compareTo("foo") > 0) { // "foo" < node.data
q.enqueue(node.left);
}
else if (found == false && node.data.compareTo("foo") < 0 { // "foo" > node.data
q.enqueue(node.right);
}
}
- als365 October 19, 2012