spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Java implement tree search


You could navigate to the root of the tree first in a different method, then call your current recursive method.

private DefaultMutableTreeNode
search(int id, DefaultMutableTreeNode node){
    if(node == null){
        return null;
    }
    node = node.getRoot(); //Turns out that this
class has a getRoot() method.
    return searchInternal(id,node); //Then does a
depth-first search from the root node.
}

private DefaultMutableTreeNode searchInternal(int
id, DefaultMutableTreeNode node){
    if(node == null){ //I also switched this
around, for good practice.
        return null;
    }
    Employee emp = (Employee)
node.getUserObject();
    if(emp.getId() == id){ //Found it!
        return node;
    }
    DefaultMutableTreeNode foundNode =
searchInternal(id, node.getPreviousSibling());
    if(foundNode == null) {
        foundNode = search(id,
node.getNextSibling());
    }

    return foundNode;
}

In general, recursive methods tend to run a bit slower in Java, and they are prone to stack overflowing. It's usually better to use a custom stack for depth-first search, and this doesn't require you to make a different method. Recursive methods might make the code more readable in some cases.

What also works, in this case:

private DefaultMutableTreeNode
search(int id, DefaultMutableTreeNode node){
    if(node == null){
        return null;
    }
    Enumeration enumeration =
node.getRoot().depthFirstEnumeration();
//Enumerate over all nodes in the tree.
    while(enumeration.hasMoreElements()){
        DefaultMutableTreeNode next =
enumeration.next();
       
if(((Employee)next.getUserObject()).getId() ==
id){ //Found it!
            return next;
        }
    }
    return null;
}

Turns out that depth-first searching is already implemented by Swing. This is a post-order enumeration though. See the javadoc, there are also methods for the other types of traversals.


Categories : Java

Related to : Java implement tree search
Should I use multiple inheritance to implement search tree with linked list?
Multiple inheritance is a good candidate if your new class has an "is a" relationship with all the base classes your envisage. Is it a tree ? I want to create a class that is essentially an AVL tree with the added option of ... According to this statement, your ListTree definitively is a Tree and should inherit from it. Is it a list ? Well it depends of what's behind your List !

Categories : C++
Using CakeDC's search plugin to search data with multi-select field
This should work fine, assuming you are passing in the arg to parseCriteria() as a simple array of values. public $filterArgs = array( array( 'name' => 'city', 'type' => 'value', 'field' => 'Model.city' ) ); And you should be able to pass city:houston|dallas|austin in the URL It should parse that into an array of args => [city => [houston, dallas,

Categories : PHP
GTrendsR search Error in charToDate(x) when trying to for loop over search terms
This is because URLs/browsers become angry when there're spaces. Problem: you have search phrases with spaces. The error message is not that helpful here but @Richard asked a question that got me thinking on the right track. So you are passing terms with spaces but google wants + rather than spaces. gsub to the rescue. x <- "one two three" gsub("\s+", "\+", x) ## [1] "one+two+three" So

Categories : R
Turn Depth First Search into a Best-First (greedy) Search in prolog
The trick is to not allow prolog to choose the next move for you, but rather gather all of the possible moves so you can use your criteria to select the best one. Look into the find_all predicate.

Categories : Prolog
How Can I Search A Word in A Binary Search Tree?
You'll have to walk the whole tree since you're not searching by id, but you seem to realize that already. To walk the tree in order using recursion, recursively call your function on the left subtree, process the current node, then recursively call the function on the right subtree (checking for NULL appropriately). Example: void listWord(node *tree, char *word) { if (tree) { /* I

Categories : C
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.