Hi, I need a bit of help with the java app I have. I am trying to have a
tree structure that contains 2 different types of nodes..]

a simple node called 'SimpleNode' - to hold a name and url strings
and another node called TreeNode that act as folders and subfolders in which the simplenodes can be added as children.

So i can have a tree with root, a list of simplenodes just holding a name and a url and then maybe a treenode that acts as a subfolder, which in turn could contain simplenodes

I have the program creating a root node and then adding a subnode to the root node(ie adding a 'TreeNode' child to the 'TreeNode' root) but I do not know how to add the simple nodes as children of the root, or for that matter as children of the treenodes

below is the code

public class TreeNode<Bookmark> {
    private Bookmark bookmark;
    private TreeNode<Bookmark> next;
    private TreeNode<Bookmark> subnodes;

    public TreeNode(Bookmark bookmark, TreeNode<Bookmark> subnodes, TreeNode<Bookmark> next) {
        this.bookmark = bookmark;
        this.subnodes = subnodes;
        this.next = next;
    }

    public TreeNode findSubNode(Bookmark itemToMatch) {
        for (TreeNode sub = subnodes; sub != null; sub = sub.next) {
            if (sub.bookmark.equals(itemToMatch)) {
                return sub;
            }
        }
        return null;
    }

    public void addSubNode(TreeNode<Bookmark> child) {
        child.next = subnodes;
        subnodes = child;
    }

    public void setSubnodes(TreeNode<Bookmark> subnodes) {
        this.subnodes = subnodes;
    }

    public TreeNode<Bookmark> getSubNodes() {
        return this.subnodes;
    }

    public Bookmark getBookmark() {
        return this.bookmark;
    }

    public String toString() {
        String str = recursiveToString("", 0);
        return str;
    }

    public TreeNode<Bookmark> getNext() {
        return this.next;
    }

    private String recursiveToString(String str, int indent) {
        for (int i = 0; i < indent; i++) {
            str += " ";
        }
        str += bookmark.toString();
        str += "\n";
        indent += 3;
        TreeNode<Bookmark> sub = subnodes;
        while (sub != null) {
            str = sub.recursiveToString(str, indent);
            sub = sub.getNext();
        }
        return str;
    }

    public void displaySubNodes() {
        System.out.println("Contents of " + bookmark);
        for (TreeNode<Bookmark> sub = subnodes; sub != null; sub = sub.next) {
            System.out.println(sub.getBookmark());
        }
    }
};
public final class SimpleNode<Bookmark> {
    private String name;
    private String url;

    public SimpleNode(String name, String url) {
        this.name = name;
        this.url = url;
    }

    public void addSimpleNode(SimpleNode<Bookmark> child) {
        //insert code
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
public static void main(String[] args) {
        //Initial Root Node
        TreeNode<String> root = new TreeNode("Firefox Bookmarks", null, null);
        //Initial Sub Nodes
        root.addSubNode(new TreeNode<String>("Favourite Bookmarks", null, null));
        TreeNode<String> tnode = new TreeNode<String>("College Bookmarks", null, null);
        root.addSubNode(tnode);
        tnode.addSubNode(new TreeNode<String>("Data Structures", null, null));
        //Initial Bookmark
//Doesnt Work
        //SimpleNode<String> snode = new SimpleNode<String>("Google", "www.google.com");
        //root.addSimpleNode(snode);

        System.out.println(root);
        
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader reader = new BufferedReader(input); 
        System.out.println("Add Bookmarks...");
        System.out.print("Enter Bookmark: ");
        
        String bookmark;
        try {
            bookmark = reader.readLine();
            System.out.println("Searching... " + bookmark);
            TreeNode treenode = root.findSubNode(bookmark);
            if (treenode == null)
                System.out.println("No Such Bookmark");
            else {
                System.out.println(treenode);
            }
            /*System.out.println("Add a Bookmark Folder...");
            System.out.print("Enter Name: ");
            String bookmarkFolder = reader.readLine();
            treenode.addSubNode(new TreeNode(bookmarkFolder, null, null));
            System.out.println(treenode);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

It looks to me like your problem comes from the class structure itself.
The normal way to do this would be to declare an abstract class (eg "AbstractNode") and make SimpleNode and TreeNode subclasses of it. Your subnodes collection should be a collection of AbstractNodes; that way you can add both TreeNodes and SimpleNodes as sub-nodes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.