LOGICAL TREES in JAVA

hi everyone.
i'm doing a project that requires me to use a tree structure. there is of course jtree but i dont need the user interface, i just need the structure.
may i ask a few minutes from your time to enlighten me regarding this problem.
please tell me the structure to use, and an example how to use it.
please help.(deadline's in a few days.)
thank you.

Here's a simple Node class - to use them as a tree, all you need is a reference any node, usually the root node.
If you run this example you get output like:
+ Root
   + Parent 0
      + Child 0.0
         - GrandChild 0.0.0
         - GrandChild 0.0.1
      + Child 0.1
         - GrandChild 0.1.0
         - GrandChild 0.1.1
   + Parent 1
      + Child 1.0
         - GrandChild 1.0.0
         - GrandChild 1.0.1
      + Child 1.1
         - GrandChild 1.1.0
         - GrandChild 1.1.1
import java.util.Vector;
public class Node
     private Vector mChildren= new Vector();
     private Node mParent;
     private Object mUserData;
     public Node(Object userData) {
          this(userData, null);
     public Node(Object userData, Node parent)
          mUserData= userData;
          mParent= parent;
          if (parent != null)
               parent.addChild(this);
     public Node getParent() {
          return mParent;
     public void addChild(Node child) {
          mChildren.addElement(child);
     public int getChildCount() {
          return mChildren.size();
     public Node getChildAt(int index)
          if (index > getChildCount())
               return null;
          return (Node) mChildren.elementAt(index);
     public boolean isLeaf() {
          return getChildCount() < 1;
     public String toString() {
          return mUserData != null ? mUserData.toString() : "null";
     public static void main(String[] argv)
          Node root= new Node("Root");
          for (int i= 0; i< 2; i++) {
               Node parent= new Node("Parent " +i, root);
               for (int j= 0; j< 2; j++) {
                    Node child= new Node("Child " +i +"." +j, parent);
                    for (int k= 0; k< 2; k++) {
                         Node grandChild= new Node("GrandChild " +i +"." +j +"." +k, child);
          print(root, "");
     private static void print(Node node, String indent)
          System.err.print(indent);
          System.err.print(node.isLeaf() ? "- " : "+ ");
          System.err.println(node);
          for (int i= 0; i< node.getChildCount(); i++)
               print(node.getChildAt(i), indent +"   ");

Similar Messages

  • Logical AND in Java Regular Expressions

    I'm trying to implement logical AND using Java Regular Expressions.
    I couldn't figure out how to do it after reading Java docs and textbooks. I can do something like "abc.*def", which means that I'm looking for strings which have "abc", then anything, then "def", but it is not "pure" logical AND - I will not find "def.*abc" this way.
    Any ideas, how to do it ?
    Baken

    First off, looks like you're really talking about an "OR", not an "AND" - you want it to match abc.*def OR def.*abc right? If you tried to match abc.*def AND def.*abc nothing would ever match that, as no string can begin with both "abc" and "def", just like no numeric value can be both 2 and 5.
    Anyway, maybe regex isn't the right tool for this job. Can you not simply programmatically match it yourself using String methods? You want it to match if the string "starts with" abc and "ends with" def, or vice-versa. Just write some simple code.

  • Populate Flex Tree from Java Code (Database)

    Hi
    Can someone please help me? I'm very new to using flex. I'm
    basically trying to create a tree that is populated from data in my
    database. I am using hibernate to fetch that data.
    The plan is to basically create the tree in Java code (since
    I don't want any processing done on the client PC) and then pass
    this to the flex tree (or an actionscript data structure that is
    compatible as the tree datasource)
    I am creating the java tree using DocumentImpl. Is this
    correct? What class would work?
    Element e = null;
    Node n = null;
    Document xmldoc= new DocumentImpl();
    Element root = xmldoc.createElement("USERS");

    quote:
    Originally posted by:
    ntsiii
    Stop running and get a map (read the documents)
    This is hardly a useful comment and must discourage new users
    from asking questions. If you think the documentation relating to
    the use of the tree control is straightforward then I beg to
    differ.
    If you are aware of a clear example of how to use the data
    tree with a php backend returning a remote data object please
    enlighten me. Meanwhile I continue my search but not within the
    livedocs.
    One site I have found that may be of use is
    http://flexdiary.blogspot.com/2009/01/lazy-loading-tree-example-file-posted.html
    Not everyone is an expert.

  • Creating non binary trees in Java

    Hi everybody actually i am used in creating binary trees in java,can u please tell me how do we create non binary -trees in java.

    Hi,
    Can u let us know the application area for Non binary tree.
    S Rudra

  • Non binary trees in java

    Hi guys i am used in creating binary trees ,tell me how do we create non binary trees in java.

    public class Node {
      private final Object payload;
      private final Set<Node> children;
      public Node(Object payload) {
        this.payload = payload;
        children = new HashSet<Node>();
      // now add methods to add/remove children, a method to do something with the payload (say,
      // a protected method that passes the payload to a method specified by some kind of interface),
      // and methods to recurse over children.
    }Actually rather than using Object probably should have generic-ized the class, but whatever.

  • Binary Tree in Java - ******URGENT********

    HI,
    i want to represent a binary tree in java. is there any way of doing that.
    thanx
    sraphson

    HI,
    i want to represent a binary tree in java. is there
    e any way of doing that.
    thanx
    sraphsonFirst, what is a binary tree? Do you know how the binary tree looks like on puesdo code? How about a representation in terms of numbers? What is a tree? What is binary? The reason I ask is, what do you know about programming?
    Asking to represent a binary tree in java seems like a question who doesn't know how it looks like in the first please. Believe me, I am one of them. I am not at this level yet. If you are taking a class that is teaching binary trees and you don't know how it looks like, go back to your notes.
    Sounds harsh, but it is better to hear it from a person that doesn't know either then a boss that hired you because Computer Science was what you degree said. Yet, you don't know how to program?
    Telling you will not help you learn. I can show you tutorials of trees would be start on where to learn.
    HOW TO USE TREES (oops this is too simple, but it is a good example)
    http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html
    CS312 Data Structures and Analysis of Algorithms
    (Here is a course about trees. Search and learn)
    http://www.calstatela.edu/faculty/jmiller6/cs312-winter2003/index.htm

  • Binary trees in Java

    Hi there
    Do you have any suggestions about how to implement trees or binary trees in Java?
    As far as I know there are already some libraries for that, but I don't know how to use them. Was trying to get some implementation examples but I'm still very confused.
    How can I use for example:
    removeChild(Tree t)
    addChild(Tree t)
    isLeaf()
    Thanks in advance

    Lulu wrote:
    Hi there
    I have several questions about binary trees
    Let's see, I use TreeMap to create them with the following code:
    TreeMap treeMap = new TreeMap<Integer, String>();
    treeMap.put("first", "Fruit");
    treeMap.put("second","Orange");
    treeMap.put("third", "Banana");
    treeMap.put("fourth", "Apple");You've defined the map to hold integer keys and strings as values, yet you're trying to add string keys and string values to it: that won't work.
    If this is a map how do I define if the data should go to the left or to the right of certain node?That is all done for you. In a TreeMap (using the no-args constructor), you can only store objects that are comparable to each other (so, they must implement the Comparable interface!). So the dirty work of deciding if the entry should be stored, or traversed, into the left or right subtree of a node, is all done behind the scenes.
    Also note that TreeMap is not backed up by a binary tree, or a binary search tree, but by a red-black tree. A red-black tree is a self balancing binary tree structure.
    Should I have dynamical keys so that they increase automatically when adding new data?
    According to a webpage I should use Comparator(), is that to find the data in the tree and retrieve the key?
    ThanksI am not sure what it is you want. I am under the impression that you have to write a binary tree (or a binary search tree) for a course for school/university. Is that correct? If so, then I don't think you're permitted to use a TreeMap, but you'll have to write your own classes.

  • How to connect to logical printers in Java

    Hey guys
    how do i connect logical printers in java
    the reason why i call them logical is bcaz they are not listed in printers like ordinary printers (is that grammatically correct ........ any how)
    More specificallly a printer in the Epson JPOS
    thanks much

    Are you saying you want to print something in Java?
    http://java.sun.com/developer/technicalArticles/Printing/SwingPrinting/index.html
    http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-print.html

  • Implementation of LISP trees in Java

    I have a problem with implementation of a something similar to a LISP tree in Java. That means that I want to be able to combine different if-else-conditions from a set of variables and operators and run it. For example, if (variable) (operator) (variable) (operator2) (variable) (operator) (variable)
    whereby :
    variable: means a variable
    operator: <, >, =, +, _,
    variable: variable
    operator2: &&, ||, etc
    I don't know how to do in a way that a variable can be picked up from a set, operator can be picked up from a set and operator2 can also be picked up from a set.

    Lisp doesn't use trees... it uses lists. In fact, Lisp stands for "List Processing".
    Take a look at the java.util.LinkedList class. If you make a LinkedList of Nodes, where each Node contains an Object car and LinkedList cdr, you've got yourself a Lisp list implementation!

  • Implementation of a parse tree in Java (Urgent)

    How best can a parse tree be implemented in java, i have read some online information that suggest use of data structures, but i am still not clear on how to do it.

    Well, you need a tree. A logical way to structure a tree is out of "node" objects. A node presumeably will hold some data, and may also have one or more subnodes.
    So the root of your effort ought to go into designing the node - data it will hold, and getters and setters, and operations to walk the tree.

  • How can I link to pages in a 2 level popup index/selection tree with java script?

    I can do a single level popup index using the java script below (#1) and link the selections to pages but to change to a  2 level popup index selection tree as below (#2) I can't seem to figure out how to link to pages. Any help greatly appreciated. Thanks
    1
    var itemIndex = app.popUpMenu("INTRODUCTION", "ALPHABET", "GRAMMAR", "NUMBERS", "DATES & TIME", "GETTING TO KNOW PEOPLE", "PHRASES FOR LEARNING", "DIRECTIONS & TRANSPORTATION", "DESCRIBING THE TRAIL", "DO'S & DON'TS", "EQUIPMENT", "FOOD", "INTRODUCING A VILLAGE", "CULTURE AND ETHNIC GROUPS", "HANDICRAFTS", "TEMPLES & MONKS", "AGRICULTURE", "ANIMAL HUSBANDRY", "SCHOOL & EDUCATION", "LEADING A TREK", "NATIONAL PROTECTED AREAS", "IDENTIFYING WILDLIFE", "PLANTS & FORESTS", "BOATS", "CAVES", "HEALTH AND SAFETY")
    switch (itemIndex) {
    case "INTRODUCTION":
    this.pageNum = 1
    break
    case "ALPHABET":
    this.pageNum = 2
    break
    case "GRAMMAR":
    this.pageNum = 5
    break
    case "NUMBERS":
    this.pageNum = 30
    break
    case "DATES & TIME":
    this.pageNum = 35
    break
    case "GETTING TO KNOW PEOPLE":
    this.pageNum = 42
    break
    case "PHRASES FOR LEARNING":
    this.pageNum = 56
    break
    case "DIRECTIONS & TRANSPORTATION":
    this.pageNum = 59
    break
    case "DESCRIBING THE TRAIL":
    this.pageNum = 63
    break
    case "DO'S & DON'TS":
    this.pageNum = 68
    break
    case "EQUIPMENT":
    this.pageNum = 74
    break
    case "FOOD":
    this.pageNum = 83
    break
    case "INTRODUCING A VILLAGE":
    this.pageNum = 96
    break
    case "CULTURE AND ETHNIC GROUPS":
    this.pageNum = 105
    break
    case "HANDICRAFTS":
    this.pageNum = 120
    break
    case "TEMPLES & MONKS":
    this.pageNum = 126
    break
    case "AGRICULTURE":
    this.pageNum = 131
    break
    case "ANIMAL HUSBANDRY":
    this.pageNum = 140
    break
    case "SCHOOL & EDUCATION":
    this.pageNum = 145
    break
    case "LEADING A TREK":
    this.pageNum = 151
    break
    case "NATIONAL PROTECTED AREAS":
    this.pageNum = 155
    break
    case "IDENTIFYING WILDLIFE":
    this.pageNum = 161
    break
    case "PLANTS & FORESTS":
    this.pageNum = 169
    break
    case "BOATS":
    this.pageNum = 175
    break
    case "CAVES":
    this.pageNum = 178
    break
    case "HEALTH AND SAFETY":
    this.pageNum = 182
    break
    2
    var aINTRODUCTION = ["INTRODUCTION", "How to Use This Book", "Format"];
    var aALPHABET = ["ALPHABET", "Vowel Sounds", "Consonant Sounds"];
    var itemIndex = app.popUpMenu(aINTRODUCTION, aALPHABET);
    Thanks.

    Thanks. I've got it now. I was mistakenly renaming itemIndex to each section...Introduction, Alphabet

  • Tree in java applet

    Please help
    i want to create a tree (the leaves label come from database ) in java applet... how can i start ?

    This is for a school assignment isn't it?
    The tutorial has how to read from a database, and about trees in it. There are even ready make classes for tree structures. So crack your book and give it a read, click on the tutorial link, and open the API... if it's not worth it to you, then why should it be to us?

  • How to fill a Tree in Java?

    Hello,
    I have a tree to realize the navigation on my site and now I want to build this tree in a dataProvider. I found the class SimpleTreeData which seems to link to the <nodes>-Tag but I can't get it work. Here are some code extracts:
    DataProvider:
    private SimpleTreeData createShopNode(String text, String description)
     SimpleTreeData data = new SimpleTreeData();
     data.setText( text );
     if ( description != null )
       data.setDescription( description);
     data.setDestination( "http://bali.us.oracle.com");
     data.setDestinationText( "More Information");
     return data;
    private DataObject createData(RenderingContext context, String ns, String name)
      SimpleTreeData treeRoot = new SimpleTreeData();
      SimpleTreeData shop = createShopNode("Shop", "Spend some money!");
      SimpleTreeData books = createShopNode("Books", null);
      SimpleTreeData umbrellas = createShopNode("Umbrellas", "Rain, rain go away");
      SimpleTreeData art = createShopNode("Art", "Picasso et al");
      books.addChild( art);
      shop.addChild(books);
      shop.addChild(umbrellas);
    DictionaryData lDictionaryData = new DictionaryData();
    lDictionaryData.put( "dynamicNode", shop);
    return lDictionaryData;
    This is my UIX Page:
    <provider>
    <data name="TreeProxy">
    <method class="test.DataTrees" method="getTreeProxy"/>
    </data>
    <data name="treeData">
    <method class="test.NavigationNodes"
    method="createData"/>
    </data>
    </provider>
    <tree data:proxy="proxy@TreeProxy">
    <include data:node="dynamicNode@treeData"/>
    </tree>
    Thanks,
    Christian

    Perhaps it is helpful, I get this Exception:
    java.lang.IllegalAccessException: de/oc/vera/navigation/NavigationNodes
    java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[])
    native code
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.data.provider.MethodDataProvider.getDataObject(oracle.cabo.ui.RenderingContext, java.lang.String, java.lang.String)
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.data.provider.TableDataProvider.getDataObject(oracle.cabo.ui.RenderingContext, java.lang.String, java.lang.String)
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.data.provider.CachingDataProvider.getDataObject(oracle.cabo.ui.RenderingContext, java.lang.String, java.lang.String)
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.data.provider.DataProviderStack.getDataObject(oracle.cabo.ui.RenderingContext, java.lang.String, java.lang.String)
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.laf.base.TryRenderer$CatchContext.getDataObject(oracle.cabo.ui.RenderingContext, java.lang.String, java.lang.String)
    oracle.cabo.ui.data.DataObject oracle.cabo.ui.RenderingContextProxy.getDataObject(java.lang.String, java.lang.String)
    java.lang.Object oracle.cabo.ui.data.DataBoundValue.getValue(oracle.cabo.ui.RenderingContext)
    java.lang.Object oracle.cabo.ui.BaseUINode.getAttributeValueImpl(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.AttributeKey, boolean)
    java.lang.Object oracle.cabo.ui.BaseUINode.getAttributeValue(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.AttributeKey)
    oracle.cabo.ui.data.DataObjectList oracle.cabo.ui.laf.base.BaseLafRenderer.getDataObjectListAttributeValue(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode, oracle.cabo.ui.AttributeKey)
    void oracle.cabo.ui.laf.browser.TreeRenderer.renderContent(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode)
    void oracle.cabo.ui.BaseRenderer.render(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode)
    void oracle.cabo.ui.laf.xhtml.XhtmlLafRenderer.render(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode)
    void oracle.cabo.ui.BaseUINode.render(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode)
    void oracle.cabo.ui.BaseUINode.render(oracle.cabo.ui.RenderingContext)
    void oracle.cabo.ui.BaseRenderer.renderChild(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode)
    void oracle.cabo.ui.BaseRenderer.renderIndexedChild(oracle.cabo.ui.RenderingContext, oracle.cabo.ui.UINode, int)

  • Help AVL Tree in JAVA code (remove) question-answer

    Hi,
    everyone. I have question.
    I want codes in Java for AVL Tree.
    I but must have remove methods or choose elements.
    It`s main for me.
    If you know, please write in code JAVA or contact e-mail.
    or link to page or if you have self code.
    thanks,
    regards
    [email protected]

    I have a better idea.
    Why don't you try to do it yourself, and when you get stuck, post your code and details of what difficulty you're having.
    When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.

  • Tree traversal Java Program

    Hi,
    I need the below implementation in JAVA :
    Scenario : Need to traverse once through all nodes of a tree
    Input :
    (i) Starting node of traversal
    (ii) List of nodes in the Tree
    (iii) Parent of all nodes, say, mapping like (each_node, list_of_its_parent) (OR) (each_node, list_of_its_child)
    Eg TREE :
    A___________________ F
    | | | |
    |__B | | |
    | C D_____|
    | |___
    H | |
    E G
    In the above example
    (node, child_list) :--
    =============
    (A, {B,C,D})
    (B, {H})
    (C, {E,G})
    (F, {D})
    In the above tree, if any node is given as the starting node, I need to traverse through the complete tree visiting all nodes once.
    Please help,
    Thanks
    Sam

    That is not a tree;Does it matter? You can always traverse a DAG as a
    tree can't you? The A in DAG means acyclic so a DAG
    is always also a tree.Check the definitions before you reply please: a DAG
    is not "aways also" a tree; no discussion about it. Don't confuse
    matters.Who's confusing matters? You introduced the DAG so
    you explain why and what consequences it has!No, I just gave a name to what the OP said: s/he didn't show a tree,
    s/he showed a DAG; that's all I did.
    So a DAG can not always be expanded to a tree? Is that what you're
    saying?There's no 'expanding' here; A tree is a very restricted DAG instead.
    kind regards,
    JosAnd zip up please. Your fake intimacy isn't always wellcome.Stay out of this discussion then; easy as that: a) you lack the knowledge
    to participate in this discussion and b) you don't like my friendly greetings,
    so the best thing for you, and for this discussion, is that you stay out of it.
    kind regards,
    Jos

Maybe you are looking for

  • Classified junk mail intermittently not moving to junk folder

    Hello, I am running Thunderbird 31.3.0 and all of a sudden the junk mail (which is classified and marked) is not being moved to the junk mail folder intermittently on some of the IMAP accounts setup on Thunderbird. Again, this problem only intermitte

  • Communication problem between NetBeans and Tomcat

    hi! i got a quite mysterious problem. here is what happens: - i start NetBeans 5.5.1 (the first time) - i want to debug my JSF-Project, the Debugger starts - After a few seconds the debugger waits for tomcat (it sais: "Waiting for Tomcat...") and tom

  • 802.1x authentication fail with MS IAS

    dear friends, i want to configre .1x authentication for my wireless user and trying to integrat our controllers with MS WINDOWS Server 2008 to configure as a Radius server i followed this documentation here is the link http://www.cisco.com/en/US/prod

  • Date fields in XML

    Does anyone know if you can have date fields in XML documents so you can sort fields in date order etc. Thanks

  • Itunes IS truncating idtags. Why?

    Yes. Itunes 8.1.1.10 IS responsible for truncating idtags. Here's when, and how this problem comes up for myself. If an album's title is named like the following examples: Moog and Mooger [IMPORT] Moog Mooger and Moogerer (The Moog) Moog Mooger [CD1]