1

I have been looking at this for awhile now but I cant really seem to find much about it online. I have a JTree and a list of strings. I want to search just the lowest levels of the JTree(so not every node in the tree is searched, just the lowest nodes) for each of the strings and add the string to a list if the string i am searching for is present in the path of the lowest node

something like this

public List<String> searchLowestNodes(List<String> wordsToSearchFor){
    List<String> matches = new ArrayList<>;
    for(String word: wordsToSearchFor){
        // i do not know how to get the lowest node for each path
        if(path.contains(word)){
            matches.add(word);
        }
        //keep looping for all paths
     }
     return matches;
}

Does anybody know how this can be done?

EDIT: Tree example

Root
-assignment1
--paul
---example.java
--john
---example.java
-assignment2
--a2
---sean
----assignment.java
---mark
----assignment.java

so given the following tree it should only search the following paths for the words

root>assignment1>paul>example.java
root>assignment1>john>example.java
root>assignment2>a2>sean>assignment.java
root>assignment2>a2>mark>assignment.java

i do not want to add

root>assignment1
root>assignment1>paul
...
newSpringer
  • 1,018
  • 10
  • 28
  • 44
  • If a tree has one branch node with two leaf nodes, which of the two leaf nodes is 'lowest'? – Andrew Thompson Sep 11 '12 at 10:18
  • if it has two leafs, I want it to search both the leafs for the word – newSpringer Sep 11 '12 at 10:19
  • So by 'lowest node' you mean *every* leaf node? Some leaf nodes might be at a much more 'shallow' or 'less deep' level in the tree than other leaves. – Andrew Thompson Sep 11 '12 at 10:23
  • what exactly is path and how to you get it? – kleopatra Sep 11 '12 at 10:25
  • yes that is correct(was not too sure how to explain ti as have never worked with JTree's before this), I will be keeping the same folder structure throughout so leafs should not be more shallow than others but it might happen that they are yes – newSpringer Sep 11 '12 at 10:26
  • @kleopatra - path is the path the each leaf node of the tree, and i do not know how to get these leaf paths, that is the problem i am having – newSpringer Sep 11 '12 at 10:28
  • There is nothing to get a path to a leaf specifically: you'll have to walk the tree, for each: check if the node is a leaf, if so process the compare – kleopatra Sep 11 '12 at 10:31
  • so what you are saying is i have to walk the whole tree, and if the current node has no child node i add that as a path as it has no more leafs is it? – newSpringer Sep 11 '12 at 10:55
  • So you simply want to find all the leafs (from your example) – vainolo Sep 11 '12 at 10:58
  • i want to find the paths of the deepest leafs, in the case above i want the paths for assignment.java(both instances) and example.java(both instances) as they are the end leafs – newSpringer Sep 11 '12 at 11:04

1 Answers1

4

First, fetch the root of the tree model:

rootNode = (DefaultMutableTreeNode)tree.getModel().getRoot()

Now having this node, traverse the whole tree and store the leaves in your own data structure. You can traverse the tree using rootNode.depthFirstEnumeration(), running through the enumerator and checking for each element element.isLeaf().

After you have all the leaves, fetch their paths: element.getPath(). This gives you an array of nodes from the root to the leaf node.

Now you can do whatever you want with them.

vainolo
  • 6,907
  • 4
  • 24
  • 47
  • but child is an int, how do you compare angaint an int? – newSpringer Sep 11 '12 at 10:30
  • Seeing the comments, maybe I didn't understand your question. My answer gives you the second level of the tree for checking... Is that what you mean by "lowest? Regardless, could you post how you populate your tree? maybe this will help. – vainolo Sep 11 '12 at 10:34
  • i have added an example above – newSpringer Sep 11 '12 at 10:56