public void toString(NodeImpl<E> node) {
if (node == root) {
treeToString.append(root.key + "(");
}
if (node.left == nil && node.right == nil) {
if (node == node.parent.right) {
}
}
if (node.left != nil) {
if (node.left.left != nil || node.left.right != nil) {
treeToString.append(node.left.key + "(");
toString(node.left);
} else {
if (node.right != nil) {
treeToString.append(node.left.key);
} else {
treeToString.append(node.left.key + ")");
}
toString(node.left);
}
}
if (node.right != nil) {
if (node.right.left != nil || node.right.right != nil) {
if (node.left != nil) {
treeToString.append("," + node.right.key + "(");
} else {
treeToString.append(node.right.key + "(");
}
toString(node.right);
} else {
if (node.left != nil) {
treeToString.append("," + node.right.key);
} else {
treeToString.append(node.right.key);
}
toString(node.right);
}
treeToString.append(")");
}
}
DFS(G,v) ( v is the vertex where the search starts )
Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;
if (not visited[u]) then
// Print '('
// Print node name
visited[u] := true;
for each unvisited neighbour w of u
push S, w;
// Print ')'
end if
end while
END DFS()
private List<Node<E>> breadthFirst(Node<E> n) {
Queue<Node<E>> queue = new LinkedList();
LinkedList<Node<E>> output = new LinkedList();
if (n == null) {
return output;
}
queue.add(n);
while (!queue.isEmpty()) {
Node<E> node = queue.remove();
output.add(node);
if (left(node) != null) queue.add(left(node));
if (right(node) != null) queue.add(right(node));
}
return output;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
eclipse package explorer vs project explorer view | 2 | 135 | |
maven project error | 5 | 56 | |
windows explorer path to command prompt | 5 | 45 | |
table example | 4 | 29 |
Join the community of 500,000 technology professionals and ask your questions.