My class is currently doing Binary Trees as part of our data structures unit. I understand that the Node class must have 3 parameters (value, left node, right node) in its constructor. As part of a requirement, we must have a Tree class. What is the purpose of a tree class? Is it for managing the entire set of Nodes? Does it simply contain the functions necessary to insert, remove and search for specific Nodes?
Thank you in advance.
My Node class:
class Node {
protected int data;
protected leftNode;
protected rightNode;
Node (int data, Node leftNode, Node rightNode){
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
}
Yes, it is supposed to give the functional interface of a Tree by encapsulating all the behavior and algorithms related to the internal structure.
Why this is good?
Because you will define something that just provide some functionality and that works in a stand-alone way so that everyone should be able to use your tree class without caring about nodes, algorithms and whatever.
Ideally the class should be parametric so that you'll have Tree<T> and you'll be able to have generic methods for example
T getRoot()
Basically you'll have to project it so that it will allow you to
insert values
delete values
search values
visit the whole tree
whatever
As I see it, a Tree class should hold a reference to the root node of the tree, and to methods and attributes that operate on the tree as a whole, as opposed to methods that belong to each node, like getLeft(), getValue().
For example, I'd define a size attribute in the Tree class, and the methods that add or remove nodes to the tree (which also happen to be in this class) would be responsible for keeping the size up to date.
The purpose of any data structure is to give you a way to hold onto collections of related values and manipulate them in meaningful ways.
A tree is a special kind of data structure. It's a good choice for data that is hierarchical: those with natural parent-child relationships. A binary tree has, at most, two children for every parent node.
One other feature of trees that deserves special mention is the fact that it's self-similar: every Node in a tree is itself the root of a sub-tree. Recursion exploits this fact.
Yes, those are good methods to start with. You might want to have a look at the java.util.Map interface for others that might be useful.
The assumption is, as you suspect, a little bit flawed. The Node type that you have defined is a perfectly fine instance of a binary tree.
Jack mentions that you want to have a Tree type around the whole set of nodes in order to provide operations like getRoot, inserts, deletes and so on. This is of course not a bad idea, but it is by no means a requirement. Many of these operations could go on Node itself, and you don't necessarily have to have all of these operations; for example, there are arguments both for and against having a getRoot operation. An argument against it is that if you have an algorithm that at one point only needs a subtree, then the Tree object that holds on to the root Node prevents garbage collection of Nodes that your algorithm no longer needs.
But more generally, the real question that needs to be asked here is this: which of the things you're dealing are interface, and which are implementation? Because it's one thing if you want to expose binary trees as an interface to callers, and another if you want to provide some sort of finite map as the interface, and the binary tree is just an implementation detail. In the former case, the client would "know" that a tree is either null or a Node with a value and branches, and would be allowed to, for example, recurse on the structure of the tree; in the latter case, all that the client would "know" is that your class supports some form of put, get and delete operations, but it's not allowed to rely on the fact that this is stored as a search tree. In many variants of the latter case you indeed would use a Tree type as a front end to hide the nodes from the clients. (For example, Java's built-in java.util.TreeMap class does this.)
So the shortest answer is, really, it depends. The slightly longer answer is it depends on what the contract is between the binary tree implementation and its users; what things are the callers allowed to assume about the trees, what details of the binary tree are things that the author expects to be able to change in the future, etc.
"Is it for managing the entire set of Nodes?"
Yes.
"Does it simply contain the functions necessary to insert, remove and search for specific Nodes?"
Basically, yes. For that reason, it should contain at least a reference to the root node.
Related
I am trying to parse a java file using Eclipse JDT's AST. ASTVisitor provides a nice API to traverse all the nodes and work with the node which we want. Now what I want is, can we go to a target node, let say of type MethodDeclaration or all the nodes of that type, instead of traversing all the nodes? Because this reduces time if I have to get all the nodes of a particular type in a whole package. Thanks in advance.
Finding all nodes of a given type inherently is traversing. ASTVisitor is suitable for this exact task.
If you are concerned about unnecessary traversal below the node you are interested in, just return false from the corresponding visit() method, and the visitor will not descend into children of the current node.
I'd be surprised, though, if traversing actually were a performance bottleneck. Creating the AST in the first place is more expensive than that.
If you only want to address few nodes (identified, e.g., by a name pattern), then performing a search (which relies on an index) could perhaps be faster, but this probably pays off only if a significant number of files can be skipped entirely.
Finally, as you mention MethodDeclaration: perhaps you don't even need AST but the Java Model (which is much more light weight) is sufficient for your task?
I know this is a really specific question, but I wasn't quite sure how to phrase it more broadly, so if you recognize the broader question here, please answer it. I have implemented most of a BinarySearchTree class, but I'm not sure where to put the checks to make sure that it is a valid BinarySearchTree. Currently I have no checks, but I am considering checking before every relevant operation on that tree (i.e. check before searching, check before doing a tree walk, check before minimum, etc) as well as when the tree is constructed. If the tree isn't valid then I will throw an IllegalArgumentException. However, before I implemented this I considered instead making a new BinarySearchTreeNode class which extends BinaryTreeNode (which is the class I am currently using) and instead making the checks in the BinarySearchTreeNode class (on construction and whenever the children are set). So, I don't need an implementation of any of this (I already have an isValidBinaryTree method), but which is best practice: never checking if the tree is valid, checking if the tree is valid during the construction of the tree and before each method call on the tree, or making a BinarySearchTreeNode class which checks during construction and whenever the children are set?
Just for reference you don't really need to know what a binary search tree is. It is just a type of binary tree (each node has 0-2 children) where the value of the left child is <= the value of the parent and the value of the right child is >= the value of the parent for all nodes in the tree (this is what I refer to as a valid tree).
Your class will have contracts. A reasonable expectation for a binary search tree would be the contract that every such tree is indeed a binary search tree. This is called an invariant.
Every operation that manipulates such a tree should do it in such a way that this invariant is never broken.
This means that for every manipulation, you need to make sure that when the method finishes, the object still represents a binary search tree with all invariants intact.
Of course, you can make this easy or hard for you by choice of the API. If your tree exposes internal methods that allow for the invariants to be broken to the public, then your design is broken.
So, you should design your public API in such a way that you can indeed keep the invariants for every public method.
For example, the empty tree is indeed a binary search tree. If your add and remove methods make sure that the invariants hold (and no other methods that manipulate the state of the tree exist), then there is no reason at all to check if these invariants are true before searching. You can write a proof that they must be true at that point, if you did it right.
In my opinion it's most logical to check if your tree (or some other data structure) is valid is when it changes.
In the case of a binary search tree check for validity each time a node is inserted or removed.
If you construct the tree simply by inserting nodes then you get validity check during construction for free.
If construction is done some other way (e.g blindly copying some data) then you'd want to check the validity right after construction as well.
It wouldn't make much sense to do a validity check each time a search tree is queried since querying doesn't change it
(but you can be sure that the tree structure is good if you do the checks when changeing it).
Also, if possible, check if a modification would result in an invalid structure before actually doing the modification (and throw an exception instead).
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.
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.
I am trying to write an AST interpreter / REPL. ANTLRv4 provides two very similar interfaces (ParseTreeVisitor and ParseTreeListener) to walk the parse tree. I cannot seem to find any major differences between them, and the documentation is rather sparse. Is one interface preferable to the other?
The interfaces are used for different purposes. The primary differences are as follows:
ParseTreeListener
Provides separate enter/exit methods for before/after the children of a parse tree node are examined.
All methods return void. Any values collected for "return" by the listener must be held in fields or elsewhere.
Control of which tree nodes are examined is external (via ParseTreeWalker or a derived class).
ParseTreeVisitor
Provides one method which is responsible for all analysis/behavior for each parse tree node.
Each method returns generic type parameter T, which may be Void if the visitor methods do not return a value.
Control of which tree nodes are examined is internal (via visitChildren and/or calls to visit for specific children).
I'm looking for any implementation of a pure Tree data structure for Java (that aren't graphical ones in java.awt), preferably generic.
With a generic tree I'd like to add elements that are not supposed to be sorted and do something like this:
TreeNode anotherNode = new TreeNode();
node.add(anotherNode);
…and then I'd like to traverse the nodes (so that I can save and preserve the structure in a file when I load the tree from the same file again).
Anyone knows what implementations exist or have any other idea to achieve this?
You can use the DefaultMutableTreeNode defined in the javax.swing.tree package. It contains methods getUserObject() and setUserObject(Object) allowing you to attach data to each tree node. It allows an arbitrary number of child nodes for each parent and provides methods for iterating over the tree in a breadth-first or depth-first fashion (breadthFirstEnumeration() / depthFirstEnumeration()).
Also note that despite being resident in the javax.swing.tree package this class does not contain any UI code; It is merely the underlying model of JTree.
Scala has a nice Tree data structure. It's a "General Balanced Tree." It's not exactly Java, but it's close, and can serve as a good model.
It is hard to believe, given how much is in the base Java libraries, but there is no good generic Tree structure.
For a start, the TreeSet and TreeMap in the runtime are red-black-tree implementations.
Assuming you don't want to store arbitrary Java objects on the nodes, you could use the W3C DOM. It even comes with its own serialization format (I forget what it's called :-).