Jump to content

Java Trees

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1
dazzyg

dazzyg

    Newbie

  • Members
  • Pip
  • 8 posts
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
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Have a look here: Java Data Structures (2nd edition) - End of the World Production, LLC..

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
YouTube Classes !!!

YouTube - Broadcast Yourself.

YouTube - Broadcast Yourself.

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

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
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.

#5
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yea mine are def not brief haha

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

#6
dazzyg

dazzyg

    Newbie

  • Members
  • Pip
  • 8 posts
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.

#7
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I believe a budget program I posted has a Java Tree in it.

http://forum.codecal...et-program.html

#8
dazzyg

dazzyg

    Newbie

  • Members
  • Pip
  • 8 posts
OK, here is one of the apps Ive come up with.


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 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,
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 :cursing:

#9
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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.

#10
dazzyg

dazzyg

    Newbie

  • Members
  • Pip
  • 8 posts

BlaineSch said:

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?

#11
glen19

glen19

    Newbie

  • Members
  • Pip
  • 1 posts
can u create an avl tree code for insertion and deletion?
kindly create one for me if you can.

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

dazzyg said:

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?

You probably can. If SimpleNode is an extension of TreeNode than you probably can.

glen19 said:

can u create an avl tree code for insertion and deletion?
kindly create one for me if you can.


No! We don't do homework for people. What do you have so far?