Search in sources :

Example 46 with Stack

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();
    }
}
Also used : IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 47 with Stack

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Traverser(org.neo4j.graphdb.traversal.Traverser) Stack(java.util.Stack) Test(org.junit.Test)

Example 48 with Stack

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;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 49 with Stack

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;
}
Also used : BinaryNode(com.github.pedrovgs.binarytree.BinaryNode) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

Example 50 with Stack

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);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File) Stack(java.util.Stack)

Aggregations

Stack (java.util.Stack)245 HashSet (java.util.HashSet)42 ArrayList (java.util.ArrayList)37 File (java.io.File)23 IOException (java.io.IOException)22 View (android.view.View)18 Test (org.junit.Test)18 ViewGroup (android.view.ViewGroup)14 HashMap (java.util.HashMap)14 Set (java.util.Set)12 LinkedList (java.util.LinkedList)11 List (java.util.List)11 ImageView (android.widget.ImageView)10 Map (java.util.Map)10 Document (org.w3c.dom.Document)10 TestInputHandler (org.apache.maven.plugins.repository.testutil.TestInputHandler)9 PostfixMathCommandI (org.nfunk.jep.function.PostfixMathCommandI)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)7 TreeSet (java.util.TreeSet)7