I have a TreeModel in java and I'm given a path to find to check if the path exists. For example /dir1/dir2/dir3/ is an existing path in my tree. My tree is non binary. How would I approach this? My idea was to have the function take a DefaultMutableTreeNode then check if node has the same name as the first directory in my path and so on for the rest of the directories. My problem is how to change to the next string and the next node recursively. Should my function be recursive, iterative etc.. Any help would be great! Thanks in advance.
How to find a node in a tree given a path
471 views Asked by user1995933 At
2
There are 2 answers
2
Bohemian
On
Assuming you have a typical tree, where each element of your path is the key to the next node, something like this should work when called on the root node:
public boolean hasPath(String path) {
Node node = this;
for (String key : path.split("/")) {
node = node.get(key);
if (node == null)
return false;
}
return true;
}
It's iterative and therefore easier to understand than going down the rabbit hole of recursion. It's also more efficient.
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in RECURSION
- What is the problem in my "sumAtBis" code?
- Leetcode 1255-recursion and backtracking
- Unexpected Recursive Call
- Clang possibly skipping line(s) of code while compiling
- Return an arraylist without passing an argument
- Solving Maze using Backtracking C++
- I can't get the specific node of BST using recursion . i.e. every stack it erase
- Python Quadtree won't insert values
- Top View Of Binary Tree Depth First Search Using TreeMap
- Select/filter tree structure in postgres
- Python global variables in recursion get different result
- Trying to recursively find the area of a polygon
- *Dynamically* decorate a recursive function in Python
- What structure can be made to avoid having to use RefCell?
- Why is the output of the two given cout statements different in the given cpp code
Related Questions in TREE
- Python - how to make tree without any library
- how to get the full path of antd tree
- Python Quadtree won't insert values
- Top View Of Binary Tree Depth First Search Using TreeMap
- Select/filter tree structure in postgres
- PySimpleGUI tree doesn't Insert data into tree
- Is it possible to create a node-link diagram with ggplot?
- Represent a full, but not complete, binary tree with an array structure
- Redirecting stdout with execvp
- Prevent selected node to be unselect primevue Tree component
- Binary Search Tree (BST) - array representations
- Debugging AVL Tree Deletion: Unbalanced Node Not on Deletion Path
- How to shorten line length in react-d3-tree
- installed dm-tree vs imported tree
- Why the height of segment tree is O(logn)
Related Questions in DEFAULTMUTABLETREENODE
- Java - JTree, DefaultModelTree won't update
- I already have a checkbox in a Java Swing TreeNode. But how do I make it checkable?
- How would one convert a JSONObject into a DefaultMutableTreeNode in java?
- JTree changes lineStyle when changing node's name
- "AWT-EventQueue-0" java.lang.StackOverflowError - While trying to create JTree folder structure
- Hardcoding values in Java
- Add same DefaultMutableTreeNode to 2 different DefaultMutableTreeNode
- xml to jtree parsing in java
- JTree displaying drive does not show up at all
- Java Swing > Calling specific powershell script by selecting DefaultMutableTreeNode from Jtree model
- Dynamically add nodes in an JTree
- TreeView nodes names bugging
- DefaultMutableTreeNode value set to be default when use it to Spark mapToPair
- Filter jtree - keeping all nodes and children of nodes that match criteria
- JTree node editing path comparison always true
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
You can do this Save and process all child nodes from root into stack Then start poping each child node and doing same with it recursively