What is the actual use of Tree in Java - java

I am using the Java Tree from collection but what will be the actual use of Tree.
As in CS language, a B-tree can have a max of 2 childs , leaf , height of the tree and other.
But in java how these interpretation can be observed and what is the implementation of Tree in java.
I am talking about TreeMap and TreeSet only

If you are talking about the TreeSet and TreeMap classes, they are implementing the Set and Map interfaces respectively with an internal representation of a Tree structure. So the tree inside is not accessible to users (you cannot access the children directly)
Note: there is no standard Tree interface in java

There isn't a Tree - are you sure you don't mean either TreeSet or TreeMap?
Either way, both of these use Red-Black Trees as the underlying implementation.
Apart from having the performance characteristics of Red-Black trees ( O(log(n)) time for most common operations), they otherwise behave in pretty much the same way as HashSet and HashMap - in most cases you can use them interchangeably.

Related

Is it possible to have an AVL tree sorted by two properties

I am implementing my own data structure for storing objects, these objects have an ID and also a date attached. The operations I must implement require me to sometimes return say an array in date order, or find an object by its ID.
How can I minimise the time complexity for both date and ID in this scenario, would the best approach be to have two separate trees and accept the storage complexity cost?
Any guidance and help is appreciated, thanks!
That's possible, but not useful, and very confusing (since — for example — you'll need separate methods for rebalancing a node with respect to one tree vs. the other, so you'll basically have to write two copies of your AVL tree implementation). Instead, you should have two separate trees, but their nodes can (and should) contain pointers (references) to the same objects. You can (and should) still wrap this up in a single object, so that client code doesn't have to worry about the existence of two underlying trees.
Note, by the way, that a pair of trees has the same asymptotic complexity as a single tree, because 2 is just a constant factor (and there are no more-than-polynomial complexities involved).

Does this tree class allow fastest execution (memory not important) in Java?

In Java, is the following Tree class with List<Tree> children in each node the fastest way for further navigation of the tree?
public class Tree {
private int data;
private List<Tree> children = new ArrayList<Tree>();
// operations to insert and navigate Tree nodes
}
I just add new Tree objects to the appropriate array of children and so the entire tree expands. Don't use deletion of elements because I use them to navigate the tree all the time through the algorithm.
And how should this Tree data structure change if I do insertion and navigation as above plus deletion of nodes? Then maybe change ArrayList to LinkedList?
There lots of examples elsewhere but there is no comparison and so without enough experience I can not trust to choose any Tree implementation. That's why I'm asking some advice to find the best data structure (for fastest execution, memory is irrelevant) for two described situations (1. insertion/navigation; 2. insertion/navigation/removal).
EDIT: Can you say what you are not using the built in TreeMap or TreeSet? Because I'm studying Algorithms in general and it is hard to read builtin Java classes as they contain lots of code (checks, conversion, useless functions) that is not needed in understanding the basic operations of the algorithms.
What is the usecase? If you want add-remove-traverse and not any fancy tree functionality, just use a ArrayList! Why Tree?

What kind of tree is used in Java's TreeSet and TreeMap?

Are they AVL trees, red-black trees, or something else?
Red-black trees as described in the first line of the javadoc.
Tree Map
Tree Set
From the java.util.TreeMap<K,V> documentation:
A Red-Black tree based NavigableMap implementation.
For questions like these, you should always first consult the documentation. The API shouldn't describe ALL of the inner-workings of a class, but elementary informations such as general data structures and algorithms used are usually documented.
Other Java Collections Framework trivias
These are all little trivias that are also clearly documented:
TreeSet is implemented with a TreeMap
HashSet is implemented with a HashMap
Collections.sort uses modified mergesort
Map<K,V> is not a Collection<?>
ArrayList doesn't specify exact growth policy (unlike, say, Vector)
Related questions
Why does java.util.Arrays.sort(Object[]) use 2 kinds of sorting algorithms?
Why does the Java Collections Framework offer two different ways to sort?
Why doesn't Java Map extends Collection?
The first sentence of the TreeMap Javadoc states:
A Red-Black tree based NavigableMap implementation.
It is a red-black tree in the Oracle desktop Java implementation, but an AVL-tree in Android.
TreeSet is based on TreeMap.
And they uses red-black tree, red-black tree is a kind of AVL.

Data Structures for hashMap, List and Set

Can any one please guide me to look in depth about the Data Structures used and how is it implemented in the List, Set and Maps of Util Collection page.
In Interviews most of the questions will be on the Algorithms, but I never saw anywhere the implementation details, Can any one please share the information.
To learn how Java implements collections, the definitive place to go is the source code itself, freely available. Generally, Lists are implemented as either arrays (ArrayList) or linked lists (LinkedList); sets are either hashtables (HashSet) or trees (TreeSet); and maps are hashtables (HashMap).
Algorithms for manipulating arrays, linked lists, hashtables, and binary or n-ary trees (add, remove, search, sort) are complex enough in themselves that an entire course is necessary to cover them all. Anyone doing their own program design typically needs to understand these algorithms and their performance tradeoffs by heart. There's no substitute here for textbook study and/or practice.
The source code of the API is available, get a JDK and open up the src.zip file from the installation folder.
ArrayList: array
LinkedList: doubly linked list (Entry objects)
HashMap: array of Entry objects each Entry pointing to singly linked list
HashSet: internally uses HashMap, stores data as Key and dummy Object (of class Object) as Value in the map.
TreeMap: Red-Black tree implementation of Entry objects.
TreeSet: internally uses TreeMap. Key as data and dummy object as value.
*Entry: is an internal class in these collections and generally has Key, Value, references for other Entry objects etc.
You can always open the source files, it's all there, however, I wouldn't recommend it as usually they are quite hard to understand. Instead, I'd try finding the underlying data structure, and looking it up. Wikipedia contains most of the information you want to know on these subjects, and google contains the absolute rest.
List is just a dynamic array,
Set is a... set,
And maps are usually hash tables keyed by the key's hash, and stored as key-value pair.
If you're going to dive into the source code, I'd recommend familiarizing yourself with "how-it-probably-works", cause otherwise it will be hard to understand, especially the hash table.

Existing implementations of Trees in Java?

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 :-).

Categories