2

I am working on my thesis work that is relevant to the artificial intelligent domain.

I want to make a real time recommendation system so I need to represent the decisions with a tree.

My main question is how can I represent this tree in an efficient way? I want to mention that the tree will be traversing both bottom-up and up to bottom. Also the tree is not binary and the node that it has are quite many (over 100).

Right now I have create a node class like the above:

public class node {

    private String nodeName;
    private expectedValue ev;
    private boolean isLeaf;
    private boolean isRoot;
    private List<node> listOfChildren = new ArrayList<node>();
    private node parent;


    public node(String nodeName) {
        super();
        this.nodeName = nodeName;
    }


    public node(String nodeName, boolean isLeaf, boolean isRoot, List<node> listOfChildren, node parent) {
        super();
        this.nodeName = nodeName;
        this.isLeaf = isLeaf;
        this.isRoot = isRoot;
        this.listOfChildren = listOfChildren;
        this.parent = parent;
    }


    public void initializeNode(boolean isLeaf, boolean isRoot, List<node> listOfChildren, node parent) {
        this.isLeaf = isLeaf;
        this.isRoot = isRoot;
        this.listOfChildren = listOfChildren;
        this.parent = parent;
    }


    //getter and setter here......  
}

But I believe that it is not the most efficient way to represent a tree....

So, what is an efficient way to represent a tree in Java and is there a way to create the tree dynamically or do I have to initialise it one by one node?

Thank you!

7
  • I flagged it as 'too broad'/'opinion based' as there is no correct answer to your request. It also depends a lot on the exact requirements of the tree. Btw, you don't need "isLeaf" (listOfChildern.size() == 0) or "IsRoot" (parent == null). Commented Jan 7, 2016 at 11:49
  • Why do you "believe it is not the most efficient way to represent a tree"? What are some of the problems you see with it? Commented Jan 7, 2016 at 11:49
  • Possible duplicate of Tree implementation in Java (root, parents and children) Commented Jan 7, 2016 at 12:04
  • @Dima : Because of the fact that this system is going to run real time and also the tree has a lot of nodes, i believe that the overhead of the lists (for example list Of Children) is too big and it could be done in a more efficient and "elegant" way. Commented Jan 7, 2016 at 12:13
  • In order to elicit a most helpful answer, can you provide data / estimates on the relative frequency of operations (add, update, delete; possibly shuffle, reattach, etc.) on the nodes, possibly taking into account typical sequences of operations? Since the target is a recommendation system, optimizing for minimized query times might be the best way to go. Commented Jan 7, 2016 at 12:15

1 Answer 1

2

I think you don't need so many variables to be passed to your constructor. It could be much simpler. I would recommend to have a separate static inner class (nested class) for the Node. Also take advantage of java generics (<T>) Something like this:

public class MyTree<T> {

    private Node<T> root;

    public MyTree(T rootData) {
        root = new Node<T>();
        root.data = rootData;
        root.children = new ArrayList<Node<T>>();
    }

    public static class Node<T> {
        private T data;
        private Node<T> parent;
        private List<Node<T>> children;
    }
}

As a side note, there is a tree structure implemented in the JDK which you may use even without a swing interface. (javax.swing.tree TreeModel and TreeNode )

Sign up to request clarification or add additional context in comments.

4 Comments

(This comment is irrelevant to the content of this reply.)The world is full of strangers. Why making an enemy rather than a friend. I am a bit over reacted to your comment today. Enjoy your journey in S.O. (Strange, I cannot @ you).
@smwikipedia you can delete the comments here :)
@ldos They're educative. Let's keep them if it's not against the rules.
@idos thank you for your answer. i was't aware of java generics (<T>) so i have to look further to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.