forked from cooervo/Algorithms-DataStructures-BigONotation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-structures.html
598 lines (513 loc) · 19.7 KB
/
data-structures.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
<link
href='http://fonts.googleapis.com/css?family=Open+Sans:800|Titillium+Web:400,600,700,900'
rel='stylesheet' type='text/css'>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1">
<title>Data Structures</title>
<link rel="icon" href="images/icons/favicon.ico">
</head>
<body>
<!-- ======== ASIDE ======== -->
<aside>
<a href="index.html"><img id="logo"
src="images/logo-algos.svg"
onerror="this.onerror=null; this.src='image.svg'"></a>
<a id="menu-button" href="#">Menu</a>
<nav id="responsive-nav">
<ul>
<li><a href="index.html"><img class="aside-icons"
src="images/icons/cheat-sheets.svg"
onerror="this.onerror=null; this.src='image.svg'">big O cheat sheets</a></li>
<li><a href="intro.html"><img class="aside-icons"
src="images/icons/intro-icon.svg"
onerror="this.onerror=null; this.src='image.svg'">intro</a></li>
<li><a href="big-O-notation.html"><img
class="aside-icons" src="images/icons/big-o-icon.svg"
onerror="this.onerror=null; this.src='image.svg'">big O notation</a></li>
<li><a href="data-structures.html"><img
class="aside-icons"
src="images/icons/data-structures-icon.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="nav-descriptions">data structures</span></a></li>
<li><a href="algorithms.html"><img
class="aside-icons" src="images/icons/algo-icon.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="responsnav-descriptionsive-hide">algorithms</span></a></li>
<li><a
href="https://github.com/cooervo/algortihms-datastructures"
target="_blank"><img class="aside-icons"
src="images/icons/github.svg"
onerror="this.onerror=null; this.src='image.svg'"><span
class="nav-descriptions">Github</span></a></li>
</ul>
</nav>
<div id="share-div">
<ul id="share-list">
<li><a
href="https://www.facebook.com/sharer/sharer.php?u=http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons" src="images/icons/FB-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
<li><a
href="https://twitter.com/home?status=Check%20this%20website%20with%20%20cheatsheets%20for%20data%20structures%20and%20algorithms%20http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons"
src="images/icons/twitter-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
<li><a
href="https://plus.google.com/share?url=http://cooervo.github.io/algortihms-datastructures/index.html"><img
class="share-buttons"
src="images/icons/google-plus-share.svg"
onerror="this.onerror=null; this.src='image.svg'"></a></li>
</ul>
</div>
</aside>
<!-- ======== =MAIN ======== -->
<div id="main">
<article>
<h1 class="titles">DATA STRUCTURES</h1>
<p>Data structures are fundamental constructs around which
you build your application. A data structure determines the
way data is stored, and organized in your computer. Whenever
data exists it must have some kind of data structure in
order to be stored in a computer.</p>
<h2 class="inner-titles">Contiguous or linked data
structures</h2>
<p>Data structures can be classified as either contiguous
or linked, depending whether they are based on arrays or
pointers(references):</p>
<p>
<b>Contiguous-allocated structures</b>, are made of single
slabs of memory, some of these data structures are arrays,
matrices, heaps, and hash tables.
</p>
<p>
<b>Linked data structures</b>, are composed as distinct
chunks of memory linked together by pointers (references).
Some of this data structures are lists, trees, and graph
adjacency lists.
</p>
<h2 class="inner-titles">Comparison</h2>
<p>Some advantages of linked lists over static arrays are:</p>
<ul>
<li>Overflow is more difficult to occur on a linked
structures than it is in an array. It only happens when
the memory is actually full.</li>
<li>Insertions and deletions are simpler than for
contiguous data structures such as arrays.</li>
<li>Linked list don’t need to know size on
initialization</li>
</ul>
<p>Advantages of arrays:</p>
<ul>
<li>Linked structures require allocating extra space
for storing pointers.</li>
<li>Arrays allow efficient access to any item.</li>
</ul>
<div class="division"></div>
</article>
<article>
<h1 class="titles">ARRAY</h1>
<p>Arrays are the fundamental contiguously allocated data
structure. They have a fixed size and each element can be
efficiently located by its index. Imagine an array is like a
street full of houses, one right next to each other, each
house can be easily located by its address (index).</p>
<p>The following is an example of usage of Java's
implementation of ArrayList, which is an Array that is
resized when needed</p>
<div class="code-div">
<pre>
<code class="Jcode">
<span class="code-comment">
/*
* We can determine at the moment of instantiation the capacity
* of the ArrayList this gives a little boost in performance,
* instead of making the array resize constantly
*/</span>
List<String> exampleList = new ArrayList<>(100);
<span class="code-comment">
/*
* Don't confuse capacity with size, the following
* statement outputs 0, as currently the ArrayList
* size is 0, but it's capacity is 100
*/</span>
System.out.println(exampleList.size());
exampleList.add("first");
exampleList.add("second");
exampleList.add("third");
System.out.println(exampleList.size());<span class="code-comment">//prints 3</span>
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">SET</h1>
<p>A Set is a Collection that cannot contain duplicate
elements.</p>
<p>In Java the Set interface contains methods inherited
from Collection and adds the restriction that duplicate
elements are prohibited. Java also adds a stronger contract
on the behavior of the equals() and hashCode() methods,
allowing Set instances to be compared meaningfully even if
their implementation types differ. Some methods declared by
Set are:
<ul>
<li>add( ) Adds an object to the collection</li>
<li>clear( ) Removes all objects from the collection</li>
<li>contains( ) Returns true if a specified object is
an element within the collection</li>
<li>isEmpty( ) Returns true if the collection has no
elements</li>
<li>iterator( ) Returns an Iterator object for the
collection which may be used to retrieve an object</li>
<li>remove( ) Removes a specified object from the
collection</li>
<li>size( ) Returns the number of elements in the
collection</li>
</ul>
<p>Let's use an example of Java's implementation of set to
understand how it works. First, we need to define the
DataType and override the equals() and hashCode() methods:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class DataType {
private String name;
private int number;
public DataType(String name, int number){
this.name = name;
this.number = number;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 :
name.hashCode());
return result;
}
<span class="code-comment">
/*
* We override method equals so that objects
* with same name can't be added to the set,
* but objects with same numbers can be added
*/</span>
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
DataType other = (DataType) obj;
if (other.name == null)
return false;
return this.name.equals(other.name);
}
@Override
public String toString() {
return "DataType: name=" + name + ", number=" + number;
}
}
</code>
</pre>
</div>
<p>Now we can proceed to use the class</p>
<div class="code-div">
<pre>
<code class="Jcode">
Set<DataType> example = new HashSet<>();
DataType data1 = new DataType("first", 1);
<span class="code-comment">
//notice name repeated it's not valid</span>
DataType data2 = new DataType("first", 1);
<span class="code-comment">
//notice different name but same number it's valid</span>
DataType data3 = new DataType("second", 1);
example.add(data1);
example.add(data2);
example.add(data3);
for (DataType x : example){
System.out.println(x);
}
</code>
</pre>
</div>
<p>the output is:</p>
<div class="code-div">
<pre>
<code class="Jcode">
DataType: name=first, number=1
DataType: name=second, number=1
</code>
</pre>
</div>
<h2 class="inner-titles">Multiset</h2>
<p>A multiset is similar to a set but allows repeated
values. For Java, third-party libraries provide multiset
functionality</p>
<ul>
<li><p>Apache Commons Collections provides the Bag
and SortedBag interfaces, with implementing classes
like HashBag and TreeBag.</p></li>
<li><p>Google Guava provides the Multiset
interface, with implementing classes like HashMultiset
and TreeMultiset.</p></li>
</ul>
<p>This data structure is perfect for when we need to
perform statistical data that needs no sorting, for example
calculating the average or Standard Deviation of a multiset.</p>
<div class="division"></div>
</article>
<article>
<h1 class="titles">STACKS AND QUEUES</h1>
<p>
Arrays, Linked list, Trees are best use to represent real
objects, Stacks & Queues are best to complete <em>tasks</em>,
they are like a tool to complete and then discard.
</p>
<p>They are useful to manage data in more a particular way
than arrays and lists.</p>
<ul>
<li>Queue are first in, first out (FIFO)</li>
<li>Stack are last in, first out (LIFO)</li>
</ul>
<p>When to use stacks and queues:</p>
<ul>
<li>Use a queue when you want to get things out in the
order that you put them in.</li>
<li>Use a stack when you want to get things out in the
reverse order than you put them in.</li>
<li>Use a list when you want to get anything out,
regardless of when you put them in (and when you don't
want them to automatically be removed).</li>
</ul>
<p>
The following is an implementation of Queues in Java, based
on the <a href="https://www.coursera.org/course/algs4partI">course
by R.Sedgewick in coursera</a>
</p>
<div class="code-div">
<pre>
<code class="Jcode">
<span class="code-comment">
/*This implementation uses a singly-linked list
* with a static nested class for linked-list nodes.
* All operations take constant O(1) time in the worst case.
*/</span>
public class Queue<Generic> implements Iterable<Generic> {
private Node<Generic> firstNode; <span class="code-comment">// beginning of queue</span>
private Node<Generic> lastNode; <span class="code-comment">// end of queue</span>
private int size; <span class="code-comment">// number of elements on queue</span>
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
public Queue() {
firstNode = null;
lastNode = null;
size = 0;
}
public boolean isEmpty() {
return firstNode == null;
}
public int size() {
return size;
}
public Generic peek() {
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
return firstNode.item;
}
public void enqueue(Generic item) {
Node<Generic> oldlast = lastNode;
lastNode = new Node<Generic>();
lastNode.item = item;
lastNode.next = null;
if (isEmpty())
firstNode = lastNode;
else
oldlast.next = lastNode;
size++;
}
public Generic dequeue() {
if (isEmpty())
throw new NoSuchElementException("Queue underflow");
Generic item = firstNode.item;
firstNode = firstNode.next;
if (isEmpty())
lastNode = null;<span class="code-comment">// to avoid loitering</span>
size--;
return item;
}
public String toString() {
StringBuilder s = new StringBuilder();
for (Generic item : this)
s.append(item + " ");
return s.toString();
}
public Iterator<Generic> iterator() {
return new ListIterator<Generic>(firstNode);
}
private class ListIterator<Item> implements Iterator <Item> {
private Node <Item> current;
public ListIterator(Node<Item> first) {
current = first;
}
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext())
throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
Queue<String> q = new Queue<>();
q.enqueue("FIRST IN");
q.enqueue(" 2nd ");
q.enqueue(" 3rd ");
System.out.println(q.dequeue() + " first out ==> FIFO");
}
}
</code>
</pre>
</div>
<div class="division"></div>
</article>
<article>
<h1 class="titles">DICTIONARIES</h1>
<p>
A Dictionary is a data structure that maps a <b>key</b> to a
<b>value</b>.This is useful in cases where you want to be
able to access data via a particular key rather than an
integer index.
<p>In Java, Dictionaries are implemented as a Map: The Map
interface maps unique keys to values. A key is an object
that you use to retrieve a value at a later date.</p>
<p>Given a key and a value, you can store the value in a
Map object. After the value is stored, you can retrieve it
by using its key.</p>
<p>Following is a simple Map Implementation as an array.
Firstly, we create a class to help store the key and it's
value in an object:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class Entry<K, V> {
private final K key;
private V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
</code>
</pre>
</div>
<p>Then the implementation of map:</p>
<div class="code-div">
<pre>
<code class="Jcode">
public class Map<K, V> {
private int size;
private int CAPACITY = 16;
private Entry<K, V>[] entriesArray = new Entry[CAPACITY];
public void put(K key, V value) {
boolean insert = true;
for (int i = 0; i < size; i++) {
if (entriesArray[i].getKey().equals(key)) {
entriesArray[i].setValue(value);
insert = false;
}
}
if (insert) {
growArray();
entriesArray[size++] = new Entry<K, V>(key, value);
}
}
private void growArray() {
if (size == entriesArray.length) {
int newSize = entriesArray.length * 2;
entriesArray = Arrays.copyOf(entriesArray, newSize);
}
}
public V get(K key) {
for (int i = 0; i < size; i++) {
if (entriesArray[i] != null) {
if (entriesArray[i].getKey().equals(key)) {
return entriesArray[i].getValue();
}
}
}
return null;
}
public void remove(K key) {
for (int i = 0; i < size; i++) {
if (entriesArray[i].getKey().equals(key)) {
entriesArray[i] = null;
size--;
condenseArrayElements(i);
}
}
}
<span class="code-comment">
//Moves backwards elements from start arg</span>
private void condenseArrayElements(int start){
for (int i = start; i < size; i++) {
entriesArray[i] = entriesArray[i+1];
}
}
public int size(){ return size; }
public Set<K> keySet(){
Set<K> set = new HashSet<K>();
for (int i = 0; i < size; i++) {
set.add(entriesArray[i].getKey());
}
return set;
}
public static void main(String[] args) {
Map<String, Integer> mapExample = new Map<>();
mapExample.put("Key 1", 100);
System.out.println(mapExample.get("Key 1"));
mapExample.put("Key 2", 200);
mapExample.put("Woaah", 100000);
System.out.println(mapExample.get("Key 2"));
System.out.println("keySet: " + mapExample.keySet());
System.out.print("Values: ");
for (String key : mapExample.keySet()){
System.out.print(mapExample.get(key) + " ");
}
mapExample.remove("Key 2");
System.out.println("\nkeySet: " + mapExample.keySet());
}
}
</code>
</pre>
</div>
<div class="division"></div>
</article>
</div>
<!-- ======== /MAIN ======== -->
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/script.js"></script>
</body>
</html>