I want to implement a tree structure. Each node in the tree contains an object of my choosing, and a link to each of each children, and possibly parents. I am wondering if something like this exists in Java, so for example:
T randomObject = new randomObject();
Node<T> root;
root.setObject(randomObject);
root.addChild( ...
....
root.getFirstChild().getObject().getObjectProperty();
I have looked into Node structure (org.w3c.dom.Node), but it does not seem to be able to store objects and seems more for parsing a document.
I also looked into things like DefaultMutableTreeNode, MutableTreeNode, and TreeNode, but I have not been able to find clear examples of usage. I encounter a lot of problems with these. For example, when passing a DefaultMutableTreeNode as a parameter (does not seem to work), or when getting the child of a DefaultMutableTreeNode, which seems to return simply a TreeNode, when I clearly have added DefaultMutableTreeNode as a child.
I am new to Java so any information would be very helpful. The Oracle documentation is not that helpful. I have worked with the Vector structure, which is very easy to work with, can store objects and is easily accesible, and am wondering if something similar may exist. Thanks.
TreeMapis a Map backed by a Red-Black Tree. Exactly what kind of Tree are you trying to build?