Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Java Trees

  1. #1
    dazzyg is offline Newbie
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Java Trees

    Hi,
    I am doing a project using a java tree structure. Is there any good websites or tutorials that could help me out to get me going? I tried google but couldnt find what Im looking for.

    Or could someone give me a few pointers to get going?

    Basically, I need to create a class for representing artists and their tracks, but I have to address artist nodes inside the tree structure using an array of ints referring to the subnodes by their index?
    Im familiar with the different lists like linked lists, queues and stacks but not too up on trees.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Java Trees

    YouTube Classes !!!

    YouTube - Broadcast Yourself.

    YouTube - Broadcast Yourself.

    Both are those are recorded playlists at universities, I watched the second one. I loved it.

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Java Trees

    Those videos will help you A LOT!!! If you just want a brief introduction my web site above is rather decent. It gives a brief introduction to the different structures to get you up and going.

    Where as, the youtube classes will give you more details.

  6. #5
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Java Trees

    Yea mine are def not brief haha

    I got full semesters.. each are like an hour long.. and about 36~ videos....

  7. #6
    dazzyg is offline Newbie
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Re: Java Trees

    thanks guys. I looked at the youtube vids but I kinda already have a grasp of the architecture and how it works, I was looking for some code to see how its implemented. there is nothing really in the youtube videos about the actual implementation but they were a helpful mind refresh and a great lecture too.
    Chilli's link is something like what im after. But i would like to get a vey basic, simple working app to tinker with the code and see how I could implement my own version.

  8. #7
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Java Trees

    I believe a budget program I posted has a Java Tree in it.

    Budget Program

  9. #8
    dazzyg is offline Newbie
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Re: Java Trees

    OK, here is one of the apps Ive come up with.


    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());
            }
        }
    };
    Code:
    public class Main {
    
        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>("Java", null, null));
            //Initial Bookmark
            //SimpleNode<String> snode = new SimpleNode<String>("Google", "googlecom");
            //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();
            }
        }
    }
    I also have this node,
    Code:
    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;
        }
    }
    I basically want to have a root node, then the treenode to hold bookmark folders(which can have subfolders) with simplenodes attached to them to hold the url name and address. But I cant figure out how to add the simplenode as a subnode of the root?

    I want to be able to have a list of bookmarks and a list of bookmarks in folders(which in turn can have subfolders)
    I dont know how clear I am being - i cannot post a picture to explain it a bit better

    Anyone got an answer?
    thanks

  10. #9
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Java Trees

    To add a "next" you create a new node, then link "root.next = new" or something. All objects are pointers so do not worry about using space when copying them.

  11. #10
    dazzyg is offline Newbie
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Re: Java Trees

    Quote Originally Posted by BlaineSch View Post
    To add a "next" you create a new node, then link "root.next = new" or something. All objects are pointers so do not worry about using space when copying them.
    I tried this but its saying: incompatible types - its looking for a 'TreeNode' node and wont accept the 'SimpleNode' node as a child.

    So can you not have 2 different types of nodes in the same tree structure?

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Help] MFC maze (Binary Trees)
    By Beeko in forum C and C++
    Replies: 12
    Last Post: 01-04-2011, 10:21 PM
  2. Intermediate Trees 101
    By WingedPanther in forum C Tutorials
    Replies: 2
    Last Post: 01-31-2009, 07:54 AM
  3. Christmas Trees
    By chili5 in forum The Lounge
    Replies: 22
    Last Post: 12-19-2008, 07:02 PM
  4. Need help with binary trees.
    By Daskill in forum General Programming
    Replies: 9
    Last Post: 12-08-2008, 08:13 AM
  5. Binary Trees in C
    By Salrandin in forum C and C++
    Replies: 4
    Last Post: 12-02-2007, 02:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts