Algorithmics Flashcards
Stack Operations
push
peak
pop
isEmpty
Queue Operations
enqueue
peek
dequeue
isEmpty
Priority Queues Additional Operations
insert (giving a priority)
findMin
deleteMin/removeMin - returns the lowest element
List Operation
add - shifts elements to make space
remove
set - replaces element at position
get
Set Operations
add
remove
contains
size
isEmpty
Map Operations
put
get
remove
size
Skip List
Data Structure
Hierarchies of linked lists which allow for binary search, allowing Θ(log(n)) search. Good for concurrent access compared to binary trees
Binary Tree
A tree (a graph with no cycles and no direction) where each node has at most 2 children
Binary Search Tree
A binary tree where every element in the left subtree is smaller and each element in the right subtree is larger for every parent
Successor
Binary Search Tree
The next smallest element. Found by:
1. If has right child move right once then left as far as possible
2. Otherwise go up left as far as possible then up right once
Deletion
Binary Search Tree with 2 children
Replace the element with it’s successor. Then remove the successor
Single rotation
Binary Search Tree
Move top element of larger subtree up and change middle subtree to old top element
Double Rotation
Binary Search Tree
Needed if large subtree is in the middle. First move large subtree to outside. Then do normal rotation
AVL trees
Binary search tree where all parents left and right subtrees differ by at most 1
AVL tree implemenation
Each node has a balance factor. If > |1| then do rotations on deepest section.
AVL additions
Iterate up tree changing balance factor (increase if on left). Stop when balancefactor > |1| or is 0.
AVL deletion
Requires updating of balance factors and rotations but may need repeated rotations. Worst case O(log(n))
Red-Black Trees
- Black Root
- Children of red nodes are black
- Number of blacks on route from root to any exposed node (<= 2 children) are the same for all routes
Complete Tree
Lowest level is all on the left and other levels are full.
Heap
A complete tree with every child being >= its parent (so root is smallest)
Adding to Heap
Add element to next space in tree and swap with parent until corrrect (percolate up)
removeMin
heap priority queue
- Pop root
- Replace it with last node in heap
- Swap with smallest child until fixed
Heap Array Implementation
Root stored at 0. Children of node at k are at 2k + 1 and 2k + 2
Heap Sort
Add all elements to a heap then take them off again. Worst case O(nlog(n)) as we need to do add/remove 2n times and add/remove are O(log(n))
B-tree
Balanced trees for keeping related data close together. Good for disk access time
M-way tree
Allow for nodes to have many children. Reduces the depth of trees greatly, reducing access time
B+ Tree
All data stored in leaves. Non leaf nodes store M/2 - M-1 keys with a key corresponding to a child (Can have an extra child if on left or right). All leaves have L/2 - L data entries. Each key corresponds to a subtree with the smallest value in that subtree being that key value. Leftmost subtree has no key for it.
Trie
Multiway tree for storing sets of words. All words end with $. Use lots of memory
Internal one-way branching
Trie
Edges between non-leaf nodes, can be removed by condensing
External one-way branchiong
Trie
Edges that reach the leaf nodes, can be removed by condensing
Suffix Tree
Trie of all suffixes of string, so all parts of string included. Good for finding if string in text
Content-addressable memory
Where we want to access data based on a linked objects contents (use hash table)
Turn Hash into address
Hash % tableSize
Hash Collision
Two objects map to same address
Seperate Chaining
Hashing
Make a linked list to store any hash collisions
Open Addressing
Finding a new space in the hash table to put hash collisions
Linear Probing
Hash collisions are moved to next available space. Can cause clustering making loading data slow
Loading Factor
hashing
Proportion of full entries in the table
Quadratic Probing
hashing
hash collisions are placed in next available space 1, 4, 9, … spaces away (i^2). Prevents clustering, may cause failure to place element when table not full
Double Hashing
hash collision are placed in next available space multiples of new hash address away (from a different hashing function). Table size should be prime to allow all spaces to be checked.