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))