Author: cyrus2281

Data Structure

Content

Array

top

lookup: O(1)
push: O(1)
insert: O(n)
delete: O(n)

static: they have specific size of elements
dynamic: their increases as new elements are added

Array > 2-D Array

top

An array that holds other arrays.

All the second dimension arrays must be identical in shape



UP : row - 1

RIGHT : col + 1

DOWN : row + 1

LEFT : col - 1

Hash

top

lookup: O(1) / O(n)
search: O(1)
insert: O(1)
delete: O(1)

Collision can slow down the hash table, multiple hash keys are stored in the same row
O(n/k) -> O(n) (k is the number of rows in hash table)

Linked List

top

prepend O(1)
append O(1)
lookup O(n)
insert O(n)
delete O(n)

Linked List > Singly Linked List

top

Linked List > Doubly Linked List

top

Stack

top

lookup O(n)
pop O(1)
push O(1)
peek O(1)

LIFO
with either array or LinkedList
array: faster but predefined size
linked list: slower but with unlimited size

Queue

top

lookup O(n)
enqueue O(1)
dequeue O(1)
peek O(1)

FIFO
best to use with linked list (beacuse array has to shift all elements to left to remove the first element)

Queue > Priority Queue

top

Priority Queues are like normal queue except instead of first in first out, it is the value with highest prioriy is out first

lookup: O(n)
insert; O(log N)
delete: O(log N)

Priority Queues are based on heaps (check Heaps)

Tree

top

Tree > Binary Tree

top

Perfect Binary Tree: All branches have 2 leaf
Full Binary Tree: Each branch has either 2 leaves or no leaves

Balanced:
lookup: O(log N)
insert; O(log N)
delete: O(log N)

Unbalanced / Degenerate:
lookup: O(n)
insert; O(n)
delete: O(n)

Binary Search Tree

AVL and Red/Black trees perform auto balancing

Tree > Heap

top

lookup: O(n)
insert; O(log N)
delete: O(log N)

Max Heap: every child node has a lower value than the parent node
Min Heap: the root node is the smallest

heap is different from binary search tree as in BST nodes where in order (left for smaller, right for bigger),
but in heap both childern are either smaller or bigger. This is maintaned using up bubbling that each time the balanced is destroyed, the nodes swape with the parent and bubble up till the balance is restored

heaps are useful when we want a group of values, for example for values higher than 20, we would just pick all nodes above 20

Priority Queue is one of the uses of the heap

A heap can be written as an array as follows:

Max Heap: [100,40,50,10,15,50,40]

parent: floor((idx - 1) / 2)
=> parent of 15: ((4-1) / 2) = 1

left : (idx 2) + 1
=> left of 40 : (1
2) + 1 = 3

right : (idx 2) + 2
=> right of 40 : (1
2) + 2 = 4



Adding to heap:

Add the value to the last index, then Bubble up. The value should be smaller than all of its parent, if not, swap

Removing from heap:

remove the value, and swap it with its first child. The child should be bigger than its left and rigth children, if not swap.

Tree > Trie

top

Trie allows you to know if a word or part of a word exists in a body of text
AKA Prefix Tree

O (lenght of the word)

It is mainly used with strings. A use if this tree can be for auto-completion

Graph

top

image-3.png

A set of values that are related in a pair wise passion
Node (Vertex) are connected with Edges

Types of Graphs

Directed/Undirected

Directed Graphs: A kind of system where the movement is not bio directional, eg traffic flow, Twitter
Undirected Graphs: A system where the movement is bio directional, eg Highways, FaceBook

image.png

Wieghted/ Unwieghted

Wieghted Graphs: only the nodes have values
Unwieghted Graphs: Information can be also added to the edges

image-2.png

Cyclic/ Acyclic

Cyclic Graphs: A system where there is a cycle and you can reach the first node by only moving forward. System with loops.
Acyclic Graphs: A system where there is no cycle. You can not reach the fist node by just moving forward. System without loops.

Implementing Graphs

Edge List

An Edge list graph can be create using an array of pair holding the values of the edges

Adjacent List

An Adjacent list graph can be create using an array where the index is the node and the value is the nodes neighbors

Adjacent Matrix

An Adjacent Matrix graph can be create using a matrix, each index has an array representing all the other value.
1 for has a conection and 0 for no connection. if weighted, the weights instead of 0 or 1.

References

top

Instructors

Aditional Resources