A quick glance on language support for different data structures.
Arrays
Support for N-th dimensional arrays is generally built directly into the core language itself. They exist in most of languages if not all of them.
Some languages index from zero while others index from one.
Dynamic Arrays
Java
Java offers a couple of classes, the best known being the
ArrayList
class.Python
In Python,
list
is a data type, just like strings and numbers. It’s an implementation of dynamic arrays.C++
C++’s
std::vector
is an implementation of dynamic arrays.C#
C# also offers
ArrayList
class as in Java.Linked List
Java
In Java,
List
is an interface, where LinkedList
class implements the List
interface. It’s implemented as doubly-linked list.Python
Python does not have a built-in linked list implementation. Python lists are not linked lists, they are dynamic arrays.
C++
The
list
container in the standard temple library is a doubly-linked list implementation.C#
In C#, there is a
LinkedList
class as in Java. It’s also implemented as doubly-linked list.Stacks
Java
In Java, it has a
Stack
class.Python
Python doesn’t have an explicit stack class, but there is a section in the Python documentation of using lists as stacks.
C++
In C++,
stack
is part of the standard template library.C#
C# also offers
Stack
class as in Java.Queues
Java
Queue
is an interface just as List
was. There are multiple concrete classes that do support queue behavior. The LinkedList
class actually supports a simple queue behavior.Python
The
queue
module offers a queue
class. It is especially useful in when working with threading which is very common with queues. It’s also possible to use list as queues, however, lists are not efficient for this purpose.collections.deque
is an alternative implementation of unbounded queues with fast atomicappend()
andpopleft()
operations that do not require locking.
C++
In C++, there is a
queue
container in the standard template library.C#
In C#, there is a
Queue
class.Deque
Java
Java also has a
Deque
interface just as Queue
was. The LinkedList
class also implements that Deque
interface.
Notice that LinkedList
is turning out to be a very flexible class in Java. It can behave like a linked list and it can behave like a queue, and it can behave like a double ended queue; deque.
Python
Python also has a
deque
class for this. Even though you could use a Python list
, the deque
is optimized for this kind of usage; appends and pops on either end.C++
In C++, there is a
deque
container.C#
It doesn’t have a built in explicit deque, but, we can provide equivalent behavior with a linked list or even a dynamic array. Just be conscious as shifting elements around in the array is not an efficient task.
Priority Queues
Just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods.
Java
Java has a
PriorityQueue
class, which is based on a priority heap.Python
The
queue
module offers a PriorityQueue
class, which uses heapq
module under the hood to prioritize the queue entries. The heapq
module uses the heap data structure.Queue.PriorityQueue
is a thread-safe class, while theheapq
module makes no thread-safety guarantees.
C++
C++ has
priority_queue
container, which is also implemented as a heap data structure.C#
C# doesn’t contain a priority queue class. But, there are several implementations for priority queues available on the internet.
Associative Arrays
Most associate arrays, whether they are called dictionaries or maps or hash, are implemented using something called a hash table, and a hash table itself is a very important and useful data structure.
Other languages use binary search tree (probably a red black tree; a type of self balancing binary search tree) where each node has a key and associated value. So, if you want to keep your keys in a sorted order, hash tables aren’t really good at that, but, binary search trees are.
Java
In Java, It has
Map
interface just as List
was. It defines the operations for associative arrays, while HashMap
and HashTable
classes are implementations of these operations based on the hash table data structure.
HashTable
is better when working with multi-threaded applications, where you have different threads accessing and changing this hash table, but it will add a performance cost.
ConcurrentHashMap
class is a replacement for the olderHashTable
.
There is also
LinkedHashMap
, which use linked list to iterate over the items in the same way they were inserted.
While
TreeMap
class also implements the Map
interface, it’s actually a RedBlackTree, which is a self-balancing binary search tree.Python
In Python, they are called dictionaries. They are implemented as a data type called
dict
, just like strings and numbers.C++
C++ offers
std::unordered_map
container in the standard template library, while std::map
container is the sorted version which is typically implemented as binary search tree.C#
In C#, they’re available in
Dictionary<TKey,TValue>
, Hashtable
and StringDictionary
classes, whileSortedDictionary
class sorts the key-value pairs that’s implemented as a binary search tree.Sets
The idea of a set; having a big container where you can put a bunch of items into it, is usually implemented using hash tables, or binary search trees.
Java
In Java, It has
Set
interface just as List
and Queue
, whileHashSet
and TreeSet
classes implement the Set
interface.HashSet
is internally implemented using an HashMap
, while TreeSet
is internally implemented using TreeMap
.Python
Python supports
set
and frozenset
data types. frozenset is a way of making the set immutable after it has been created.C++
As with maps, C++ also offers
std::unordered_set
container in the standard template library, while std::set
container is the sorted version which is typically implemented as binary search tree.C#
C# also offers
HashSet
class which uses the hash table as in Java, while SortedSet
is the sorted version.Graphs
There is no direct support for graph data structure in languages(same as trees). Because the implementation of any graph is always going to be more specific.
For example, A linked list is a kind of graph, a tree is a kind of graph, a heap is a kind of graph. A single linked list would be considered a directed graph, whereas a doubly linked list is a kind of undirected graph. They are all graphs with intentional constraints.
No comments:
Post a Comment