Results 1 to 4 of 4

Thread: Tree structure

  1. #1
    toofast is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Tree structure

    Hi everybody, I have to create a tree structure, and after looking at certain Java classes as TreeNode, DefaultMutableTreeNode or MutableTreeNode I'm not so sure If I could use them.

    Basically I want to build an n-ary tree in which I'll be able to set what elements are parents / children from each other because later I'll want to trasverse it in a certain way (preorder, postorder or inorder, I have not decided yet). For this, I want to be able to do something like "myTree.getElement(x).InsertChild(y)", which is what I have not seen clear on the classes mentioned above. I can easily add children to a single node, but I don't know how to add them to a particular one in the middle of the tree...

    If you know structures or data types that I could use, I'd really appreciate it. Regards!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Tree structure

    I can easily add children to a single node, but I don't know how to add them to a particular one in the middle of the tree...
    You need to traverse the tree appropriately. Start at the root, and traverse through the tree to search for the Node (although a more efficient search to simply get the Node containing a user object/element would be to create a Map keyed with the element and valued with the node). To traverse, a) take a node (start with the root), b) get all the children, and recursively go to step (a) for all the children.

  3. #3
    toofast is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default Re: Tree structure

    I see, it doesn't sound too difficult. But I have doubts about its performance ... Couldn't it be a bit inefficient when inserting elements?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Tree structure

    Quote Originally Posted by toofast View Post
    Couldn't it be a bit inefficient when inserting elements?
    Anything can be inefficient in the incorrect context (and inefficiencies are relative - so when you say inefficient you should compare that to something). The main point of a tree is not about efficiencies/inefficiencies when creating or modifying the data structure, its about traversing and/or accessing the data in such a way that takes advantage of how its structured.

Similar Threads

  1. Jsp Tree Structure with Checkbox
    By umadatt in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-17-2012, 05:31 PM
  2. How to create a tree structure
    By deemu in forum New To Java
    Replies: 7
    Last Post: 03-07-2012, 04:50 PM
  3. Tree data structure
    By Nacao in forum New To Java
    Replies: 18
    Last Post: 08-23-2011, 07:26 PM
  4. Tree Structure
    By chiku in forum New To Java
    Replies: 2
    Last Post: 01-27-2011, 09:31 PM
  5. Tree structure using JAVA
    By trill in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 12:02 PM

Posting Permissions

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