You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Right_Data_Structure.md
+74-18Lines changed: 74 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,91 +3,134 @@
3
3
Data structure is a way how data is stored in the computer. We should choose data structure very wisely else system will not perform well. Below are the bunch of data structures and reasons when to use a specific one.
4
4
5
5
**1. Arrays**
6
-
- Used to store similar kind of elements in _contiguous memory_. Index based access which normally starts from 0 but can be from 1. We can use Arrays in below cases,
6
+
```
7
+
- Used to store similar kind of elements in contiguous memory. Index based access which normally starts from 0 but can be from 1.
8
+
- We can use Arrays in below cases,
7
9
- When fast element access is needed (Can be done using the index).
8
10
- When number of elements i.e size of array is known before hand.
9
11
- When iterating through all the elements in sequence is needed.
10
12
- When less memory has to be used. (Arrays take less memory as compared to LinkedList).
13
+
```
11
14
12
15
**2. Singly Linked List**
13
-
- Used to store data in nodes, which are connected to each other and points in one direction. There is no index based access here, so to find an element, we have to traverse the whole linked list. Also, they are not contigous block of memory. Here each node knows the location of next node, which can be located anywhere in the memory. It can be used in below cases,
16
+
```
17
+
- Used to store data in nodes, which are connected to each other and points in one direction. There is no index based access here, so to find an element, we have to traverse the whole linked list. Also, they are not contigous block of memory. Here each node knows the location of next node, which can be located anywhere in the memory.
18
+
- It can be used in below cases,
14
19
- When constant time of insertion and deletion is needed (On head). In middle or on tail, traversing is needed.
15
20
- When data dynamically grows, these are best.
16
21
- When random elements are not needed to be accessed.
17
22
- When insertion is needed at any point in the list.
23
+
```
18
24
19
25
**3. Doubly Linked List**
20
-
- Same as singly linked list but pointers at each node points in both the directions. Can be used in below cases,
26
+
```
27
+
- Same as singly linked list but pointers at each node points in both the directions.
28
+
- It Can be used in below cases,
21
29
- It's easier to delete the node from doubly linked list.
22
30
- Can be iterated in reverse order without recursion.
23
31
- Insertion or removal from doubly linked list is faster when compared to singly linked list.
32
+
```
24
33
25
34
**4. Circular Linked List**
26
-
- Same as singly linked list except the fact that last node again points to first node. Can be used in below cases,
35
+
```
36
+
- Same as singly linked list except the fact that last node again points to first node.
37
+
- It can be used in below cases,
27
38
- Develop the buffer memory.
28
39
- Represent a deck of cards in a game.
29
40
- Browser cache which allows to hit the back button.
30
41
- MRU list (Most recently used list).
31
42
- Undo functionality in photoshop or word.
43
+
```
32
44
33
45
**5. Stack**
34
-
- It is used to store data in LIFO form i.e LAST IN FIRST OUT. Think of a stack of plates. Can be used in below cases,
46
+
```
47
+
- It is used to store data in LIFO form i.e LAST IN FIRST OUT. Think of a stack of plates.
48
+
- It can be used in below cases,
35
49
- Expression evaluation and syntax parsing.
36
50
- Finding the correct path in maze using back tracking.
37
51
- Runtime memory management.
38
52
- Recursion function.
53
+
```
39
54
40
55
**6. Queue**
41
-
- It is used to store data in FIFO form i.e FIRST IN FIRST OUT. Think of a queue outside movie theatre. Can be used in below cases,
56
+
```
57
+
- It is used to store data in FIFO form i.e FIRST IN FIRST OUT. Think of a queue outside movie theatre.
58
+
- It can be used in below cases,
42
59
- When order is needed.
43
60
- When processing is needed in FIFO order.
44
61
- If we want to add or remove, queue can be used. We can also go with double ended queue in case if operations are needed on both sides.
62
+
```
45
63
46
-
**7. Binary Tree** - Data Structure where each node has atmost 2 childs. Can be used in below cases,
64
+
**7. Binary Tree**
65
+
```
66
+
- Data Structure where each node has atmost 2 childs.
67
+
- It can be used in below cases,
47
68
- Find name in the phone book.
48
69
- Sorted traversal of the tree.
49
70
- Find the next closest element.
50
71
- Find all elements less then or greater then certain value.
72
+
```
51
73
52
74
**8. Binary Search Tree (BST)**
53
-
- These are similar to binary tree, except of the fact that root node is equal to or greater then the root node of left subtree and less then the root node of right sub tree. Can be used in following cases,
75
+
```
76
+
- These are similar to binary tree, except of the fact that root node is equal to or greater then the root node of left subtree and less then the root node of right sub tree.
77
+
- It can be used in following cases,
54
78
- They are memory efficient.
55
79
- To be used when data needs to be sorted.
56
80
- Search can be done on range of values.
57
81
- Height balancing helps in reducing the run time.
82
+
```
58
83
59
84
**9. B Tree**
60
-
- This keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. We can use B-Tree in the following use cases,
85
+
```
86
+
- This keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time.
87
+
- We can use B-Tree in the following use cases,
61
88
- File systems.
62
89
- Database operations.
90
+
```
63
91
64
92
**10. Red Black Tree**
65
-
- This is again a kind of binary tree with an extra bit of data per node i.e its color, which can be either red or black. We can use Red-Black Tree in the following use cases,
93
+
```
94
+
- This is again a kind of binary tree with an extra bit of data per node i.e its color, which can be either red or black.
95
+
- We can use Red-Black Tree in the following use cases,
66
96
- Java Tree Map and C++ map are implemented using Red Black Tree.
67
97
- Computational Geometry Data structures.
68
98
- Scheduler applications.
99
+
```
69
100
70
101
**11. Splay Tree**
71
-
- It is a self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again. We can use Splay Tree in the following use cases,
102
+
```
103
+
- It is a self-adjusting binary search tree with the additional property that recently accessed elements are quick to access again.
104
+
- We can use Splay Tree in the following use cases,
72
105
- When we want to access the recent data easily.
73
106
- Allow duplicate items.
74
107
- Simple implementation and take less memory.
75
108
- When the application deals with a lot of data, use the splay tree.
109
+
```
76
110
77
111
**12. AVL Tree**
78
-
- Shape of this tree is constrained at all times such that the tree remains balanced. The height of the tree never exceeds O(log n). We can use AVL Tree in the following use cases,
112
+
```
113
+
- Shape of this tree is constrained at all times such that the tree remains balanced. The height of the tree never exceeds O(log n).
114
+
- We can use AVL Tree in the following use cases,
79
115
- When we want to control the tree height outside -1 to 1 range.
80
116
- Fast looking element.
117
+
```
81
118
82
119
**13. Minimum Spanning Tree**
83
-
- A spanning tree of a graph is a subgraph that is a tree and connects all the vertices together. A minimum spanning tree (MST) or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree. We can use Minimum Spanning Tree in the following use cases,
120
+
```
121
+
- A spanning tree of a graph is a subgraph that is a tree and connects all the vertices together.
122
+
- A minimum spanning tree (MST) or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree.
123
+
- We can use Minimum Spanning Tree in the following use cases,
84
124
- Describe financial markets.
85
125
- Handwriting recognition of mathematical expressions.
86
126
- Image registration and segmentation.
87
127
- Constructing trees for broadcasting in computer networks.
128
+
```
88
129
89
130
**14. Trie**
90
-
- A Trie (digital tree and sometimes radix tree or prefix tree), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings. We can use Trie in the following use cases,
131
+
```
132
+
- A Trie (digital tree and sometimes radix tree or prefix tree), is an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings.
133
+
- We can use Trie in the following use cases,
91
134
- Fixed dictionary and want to look up quickly.
92
135
- Require less storage for a large dictionary.
93
136
- Matching sentences during string matching.
@@ -96,34 +139,47 @@ Data structure is a way how data is stored in the computer. We should choose dat
96
139
- Supports ordered traversal.
97
140
- No need for a hash function.
98
141
- Deletion is straightforward.
142
+
```
99
143
100
144
**15. Heap**
101
-
- It is a specialized tree-based abstract data type that satisfies the heap property. We can use Heap in the following use cases,
145
+
```
146
+
- It is a specialized tree-based abstract data type that satisfies the heap property.
147
+
- We can use Heap in the following use cases,
102
148
- Implement Priority Queue.
103
149
- Whenever we want quick access to the largest (or smallest) item.
104
150
- Good for selection algorithms (finding the min or max).
105
151
- Operations tend to be faster than for a binary tree.
106
152
- Heap sort methods being in-place and with no quadratic worst-case scenarios.
107
153
- Graph algorithms uses heap as internal traversal data structures, run time will be reduced by polynomial order.
154
+
```
108
155
109
156
**16. Hashing**
110
-
- It is used to implement an associative array, a structure that can map keys to values. We can use Hash table in the following use cases,
157
+
```
158
+
- It is used to implement an associative array (Hash Map), a structure that can map keys to values.
159
+
- We can use Hash table in the following use cases,
111
160
- Constant time operation.
112
161
- Inserts are generally slow, reads are faster than trees.
113
162
- Hashing is used so that searching a database can be done more efficiently.
114
163
- Internet routers use hash tables to route the data from one computer to another.
115
164
- Internet search engine uses hash function effectively.
165
+
```
116
166
117
167
**17. Graph**
118
-
- It is an abstract data type that is meant to implement the graph and directed graph concepts from mathematics. We can use Graph in the following use cases,
168
+
```
169
+
- It is an abstract data type that is meant to implement the graph and directed graph concepts from mathematics.
170
+
- We can use Graph in the following use cases,
119
171
- Networks have many uses in the practical side of graph theory.
120
172
- Finding the shortest path between the cities.
121
173
- Solve maze game.
122
174
- Find the optimized route between the cities.
123
175
- Social Media applications.
176
+
```
124
177
125
178
**18. Matrix**
126
-
- It is used to store the data using rows and columns. We can use Matrix in the following use cases,
179
+
```
180
+
- It is used to store the data using rows and columns.
181
+
- We can use Matrix in the following use cases,
127
182
- Graphic processing algorithms.
128
183
- Represent the graph.
129
184
- Represent quadratic forms and linear algebra solution.
0 commit comments