In this post, we will see about various data structures in java.
Data structure is a way of storing and organizing data. Data structure provide a way to process and store data efficiently.
For example:
Imagine you have pile of books on the table and you are going to read these books one by one from the top. Can you think of any data structure which can simulate with this?
Yes, you can simply use stack to store the books, so it can be accessed in Last in first out fashion.
Table of Contents
In this post, I am going to cover list of all important data structures in java which you can easily implement.
Array
Array is data structure which stores fixed number of similar elements.Array can store primitive data types as well as object bu it should be of same kind. This is one of most used data structures in java.
Practice Programs
- Find missing number in an array
- search an element in a sorted and rotated array in java
- Find second largest number in array
- Find number occurring odd number of times in array
- Minimum numbers of platforms required for railway station in java
- Find pair whose sum is closest to zero in array in java
- Find pair whose sum is closest to X in array in java
- Find all pairs of elements whose sum is equal to given number
- Search element in row wise and column wise sorted matrix
- Stock buy and sell to maximize profit.
Stack
Stack is abstract data type which depicts Last in first out (LIFO) behavior.
Implementation
- Java Program to implement stack using array.
- Java Program to implement stack using Linked List
- Java Program to implement stack using two queues
Practice Programs
Sort a stack using another stack.
Queue
Queue is abstract data type which depicts First in first out (FIFO) behavior.
Implementation
LinkedList
Singly LinkedList
In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate over linked list by using this condition.
Doubly LinkedList
In doubly linked list, Node has data and pointers to next node and previous node.You can iterate over linkedlist either in forward or backward direction as it has pointers to prev node and next node. This is one of most used data structures in java.
Implementation
Practice Programs
- How to reverse a linked list in java
- How to reverse a linked list in pairs
- How to find middle element of linked list in java
- How to detect a loop in linked list in java
- Find start node of loop in linkedlist
- How to find nth element from end of linked list
- How to check if linked list is palindrome in java
- Add two numbers represented by linked list in java
Binary tree
A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child
Example of binary tree:
Implementation
Practice Programs
- Binary tree preorder traversal
- Binary tree postorder traversal
- Binary tree inorder traversal
- Binary tree level order traversal
- Binary tree spiral order traversal
- Binary tree reverse level order traversal
- Binary tree boundary traversal
- Print leaf nodes of binary tree
- Count leaf nodes in binary tree
- get maximum element in binary tree
- Print all paths from root to leaf in binary tree
- Print vertical sum of binary tree in java
- Get level of node in binary tree in java
- Lowest common ancestor(LCA) in binary tree in java
Binary Search tree
Binary search tree is a special type of binary tree which have following properties.
- Nodes which are smaller than root will be in left subtree.
- Nodes which are greater than root will be right subtree.
- It should not have duplicate nodes
- Both left and right subtree also should be binary search tree.
Implementation
Practice Programs
- Delete node from binary search tree
- Check if tree is binary search tree or not
- Find minimum and maximum elements in binary search tree
- Convert sorted array to binary search tree
- Convert sorted linked list to binary search tree
Trie
Trie is data structure which is used to store in such a way that data can be retrieved faster.
Some real time examples:
Trie can be used to implement Dictionary.
Implementation
That’s all about Data structures in Java.