0

I'm solving a Java problem which needs a java implementation of a tree with N leaves. For that, i decided to use XML Dom tree to represent the problem. Is that possible with Dom4j ?

The problem is basically a tree of game which represents all moves to calculate the minimum number of moves required to win the game. Is that any useful sample for Dom4j ? Thanks.

Katy
  • 1,023
  • 7
  • 19

1 Answers1

3

I will attempt to answer but with some considerations in mind:

First of all to directly answer your question about whether it is possible to use a XML Dom tree to represent the problem? - Yes, it would be possible but in my opinion using an XML DOM tree to represent a data structure is not so natural (in fact I could also call it an overkill) unless you want to serialise that data structure to the disk or across the network as a result of an API call.

Examples are present here : http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html

A cleaner and more simpler alternative would be to define a Node class like below:

class Node
{
   int value; //assuming the node holds an integer value
   List<Node> childNodes; // these are the N child nodes.
}

Your game algorithm can then iterate through the list of child nodes to perform it's computation logic.

One limitation that may appear with the above definition is that in case you want to search for a particular child node you need to iterate over the list - while in case you use DOM4j you could use XPath. But using DOM and XPath have their own limitations in terms of memory consumption - refer to this for details.

The simplistic data structure mentioned however above would not have the same memory implications - also it would be easier to manipulate the data structure during the computation process.

Hope this helps.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22
  • Thanks for your answer. But with Dom Tree we can perform Breadth-first search ? – Katy Aug 15 '15 at 20:15
  • I am not so well-versed with DOM tree but then BFS is an algorithm and with DOM tree it would mean enuerating the immediate children of a node and processing them – Prahalad Deshpande Aug 15 '15 at 20:33