Does the standard Java SDK have a class that represents a Self Balancing Binary Search Trees out of the box?
TreeMap and TreeSet (suppose it just uses TreeMap under the hood)
TreeMap implements NavigableMap interface and bases it’s internal working on the principles of red-black trees
Related
What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.
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.
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.
What is equivalent to [TreeBidiMap][1] in c# from Commons Collections in Java?
BidiMap is an interface which defines map which allows mapping from key to value as well as from value to key (thus "bidirectional"). TreeBidiMap is the implementation of this interfaces which uses red-black tree.
None of this classes exist in .NET BCL per se, but I think those can be composed out of simpler primitives. Specifically, C5 Generic Collection Library provides an implementation of a red-black tree, and bidirectional map can be implemented on top of a standard Dictionary<TKey, TValue>.
What is the difference between a Hash Map and dictionary ADT. And when to prefer one over another. For my programming assignment my instructor has asked to use one of them but I don't see any difference in between both. The program is supposed to work with a huge no. of strings. Any suggestions?
In terms of Java, both the class HashMap and the class Dictionary are implementations of the "Map" abstract data type. Abstract data types are not specific to any one programming language, and the Map ADT can also be known as a Hash, or a Dictionary, or an Associative Array (others at http://en.wikipedia.org/wiki/Associative_array). (Notice we're making a distinction between the Dictionary class and the Dictionary ADT.)
The Dictionary class has been marked as obsolete, so it's best not to use it.
This Stack Overflow post does a good job explaining the key differences:
Java hashmap vs hashtable
Note that Hashtable is simply an implementation of the Dictionary ADT. Also note that Java considers Dictionary "obsolete".
The fact that Hashtable is synchronized doesn't buy you much for most uses. Use HashMap.
In Java the HashMap implements the Map interface while the Dictionary does not. That makes the Dictionary obsolete (according to the API docs). That is, they both do a similar function so you are right that they seem very similar...a HashMap is a type of dictionary.
You are advised to use the HashMap though.
Map is an interface for an ADT in Java, the same general language-independent data structure for maintaining <key, value> pairs, and is introduced in Java 1.2.
Dictionary (not an implementation of Map) is an Abstract class for the same purpose introduced earlier in JDK 1.0. The only subclass it has is Hashtable which itself is implementing Map. Nevertheless, Dictionary class is obsolete now and you may forget it.
There are differences between the function members of Map and Dictionary, however you may find the difference between HashMap and Hashtable more useful. here you can find the differences.