use of java.util.Stack in project neo4j-mobile-android by neo4j-contrib.
the class FileUtils method deleteRecursively.
public static void deleteRecursively(File directory) throws IOException {
Stack<File> stack = new Stack<File>();
List<File> temp = new LinkedList<File>();
stack.push(directory.getAbsoluteFile());
while (!stack.isEmpty()) {
File top = stack.pop();
if (top.listFiles() != null) {
for (File child : top.listFiles()) {
if (child.isFile()) {
if (!deleteFile(child)) {
throw new IOException("Failed to delete " + child.getCanonicalPath());
}
} else {
temp.add(child);
}
}
}
if (top.listFiles() == null || top.listFiles().length == 0) {
if (!deleteFile(top)) {
throw new IOException("Failed to delete " + top.getCanonicalPath());
}
} else {
stack.push(top);
for (File f : temp) {
stack.push(f);
}
}
temp.clear();
}
}
use of java.util.Stack in project graphdb by neo4j-attic.
the class TreeGraphTest method testBreadthFirst.
@Test
public void testBreadthFirst() throws Exception {
Traverser traverser = Traversal.description().breadthFirst().traverse(referenceNode());
Stack<Set<String>> levels = new Stack<Set<String>>();
levels.push(new HashSet<String>(Arrays.asList("5", "6", "7", "8", "9", "A", "B", "C", "D")));
levels.push(new HashSet<String>(Arrays.asList("2", "3", "4")));
levels.push(new HashSet<String>(Arrays.asList("1")));
assertLevels(traverser, levels);
}
use of java.util.Stack in project Algorithms by pedrovgs.
the class BinaryTreeInOrder method getIterative.
/**
* Iterative implementation of this binary tree traversal. The complexity order in time terms of
* this algorithm is O(N) where N is the number of nodes in the tree. In space terms the
* complexity order of this algorithm is also O(N) where N is the number of nodes we have to
* store in the auxiliary data structure, the stack.
*/
public List<BinaryNode<Integer>> getIterative(BinaryNode<Integer> root) {
validateBinaryNode(root);
List<BinaryNode<Integer>> result = new LinkedList<BinaryNode<Integer>>();
Stack<BinaryNode> stack = new Stack<BinaryNode>();
//Define a pointer to track nodes
BinaryNode current = root;
while (!stack.empty() || current != null) {
if (current != null) {
//If it is not null, push to stack and go down the tree to left
stack.push(current);
current = current.getLeft();
} else {
//If no left child pop stack, process the node then let current point to the right
BinaryNode node = stack.pop();
result.add(node);
current = node.getRight();
}
}
return result;
}
use of java.util.Stack in project Algorithms by pedrovgs.
the class BinaryTreePostOrder method getIterative.
public List<BinaryNode> getIterative(BinaryNode root) {
validateTree(root);
List<BinaryNode> result = new LinkedList<BinaryNode>();
Stack<BinaryNode> stack = new Stack<BinaryNode>();
stack.push(root);
BinaryNode prev = null;
while (!stack.empty()) {
BinaryNode current = stack.peek();
//keep going down
if (prev == null || prev.getLeft() == current || prev.getRight() == current) {
//prev == null is the situation for the root node
if (current.getLeft() != null) {
stack.push(current.getLeft());
} else if (current.getRight() != null) {
stack.push(current.getRight());
} else {
stack.pop();
result.add(current);
}
//Go up the tree from left node need to check if there is a right child
//if yes, push it to stack otherwise, process parent and pop stack
} else if (current.getLeft() == prev) {
if (current.getRight() != null) {
stack.push(current.getRight());
} else {
stack.pop();
result.add(current);
}
//Go up the tree from right node after coming back from right node, process parent node
//and pop stack.
} else if (current.getRight() == prev) {
stack.pop();
result.add(current);
}
prev = current;
}
return result;
}
use of java.util.Stack in project android by owncloud.
the class AvailableOfflineObserver method startWatchingAll.
/**
* Scans and starts watching.
*
* Watch every file and folder below, going down through the full tree.
*
* Recursive mode will continue enabled until {@link #stopWatchingAll()} is called.
*/
public void startWatchingAll() {
if (!mRecursiveWatch) {
// TODO - should do this?
synchronized (mIncludedLock) {
super.stopWatching();
mIncludedChildren.clear();
Log_OC.d(TAG, "Stopped watching " + mPath + " for changes in selected children");
}
mRecursiveWatch = true;
} else {
// stop & clean to rescan folder tree below
stopWatchingAll();
}
mExcludedChildren.clear();
Stack<String> stack = new Stack<>();
stack.push(mPath);
// scan file tree and create subordinate observers
while (!stack.empty()) {
String parent = stack.pop();
Log_OC.d(TAG, "Adding observer for all files in " + parent);
mFolderTreeObservers.add(new SubfolderObserver(parent, UPDATE_MASK));
File path = new File(parent);
File[] files = path.listFiles();
if (files == null)
continue;
for (File file : files) {
if (file.isDirectory() && !".".equals(file.getName()) && !"..".equals(file.getName())) {
stack.push(file.getPath());
}
}
}
for (int i = 0; i < mFolderTreeObservers.size(); i++) {
mFolderTreeObservers.get(i).startWatching();
}
Log_OC.d(TAG, "Watching folder tree hanging from " + mPath);
}
Aggregations