# Basic Data Structures Keynotes

Small cheatsheet of data structures and the associated cost.

## List ADT

The list abstract data structure is one of the basic data structures around. Allows you to dynamically insert and remove and access elements.

### Linked List

Signature | Cost | Implementation |
---|---|---|

boolean add(E e) |
O(1) | add |

E get(int index) |
O(n) | get |

E set(int index, E element) |
O(n) | add |

E remove(int index) |
O(n) | remove |

### Array List

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The *add operation runs in amortized constant time*, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Signature | Cost | Implelementation |
---|---|---|

boolean add(E e) |
O(n) | add |

E get(int index) |
O(1) | get |

E set(int index, E element) |
O(1) | add |

E remove(int index) |
O(n) | remove |

## Queue ADT

### FIFO Queue

You can implement it efficiently with a Linked List or Array List.

Signature | Cost | Implelementation |
---|---|---|

boolean enqueue(E e) |
O(1) | add |

E dequeue() |
O(1) | remove |

E peek() |
O(1) | peek |

### LIFO Queue (Stack)

Can be implemented efficiently using an Array List or Linked List.

Signature | Cost | Implelementation |
---|---|---|

boolean push(E e) |
O(1) | push |

E dequeue() |
O(1) | pop |

E peek() |
O(1) | peek |

### Dequeue

Is a double ended queue, elements can be added to the front or the back. Can be implemented efficiently in both doubly linked list and Array List (with the circular array list implementation).

### Binary Heaps

They help to implement priority queues efficiently. The binary heap is a binary tree that needs to be completed. They contain a heap property, **Min Heap** where are the childrens have a priority less or equal than the parent or **Max heap** (viceversa).

Signature | Cost | Implelementation |
---|---|---|

boolean add(E e) |
O(lg(n)) | add |

E remove() |
O(lg(n)) | poll |

## Set and Map ADT

They both can reuse the same implementation (a map is a set with an extra value field). The main caracteristic is that the elements never duplicates.

### Binary Search Tree

Is important to note that this operations cost are only for self balance trees, like example AVL or Red-black trees. If they are regular trees, the operations can cost up to O(n).

BST allows you to manage the data in a sorted fashion (They produce Sorted Sets and Maps).

Signature | Cost | Implelementation |
---|---|---|

boolean add(E e) |
O(lg(n)) | add |

boolean contains(E e) |
O(lg(n)) | contains |

boolean remove(E e) |
O(lg(n)) | remove |

traversal |
O(n) | iterator |

### Hash Table

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. Iterating over this set requires time proportional to the sum of the HashSet instance’s size (the number of elements) plus the “capacity” of the backing HashMap instance (the number of buckets). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

Signature | Cost | Implelementation |
---|---|---|

boolean add(E e) |
O(1) | add |

boolean contains(E e) |
O(1) | contains |

boolean remove(E e) |
O(1) | remove |

traversal |
O(n)* | iterator |