Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Trees
- --
- Binary tree: two nodes
- BST: Two nodes, s.t. left <= n < right for all nodes
- balanced: O(logn) insert/find, red-black, AVL
- Complete BST: All children filled, leaves filled left to right
- Full BST: All nodes have 2 or 0 children
- Perfect BST: All levels filled, all nodes have 2 children
- Ordering: Imagine a tree with nodes like 4 -> 2/6. Preorder (node first): "4 2 6", In order (node middle): "2 4 6", Post order is (node last) "2 6 4"
- Heaps
- --
- Min-heap is asc, max-heap is desc
- Heaps are always complete binary trees (not BSTs)
- Always insert at right-most location, then swap up that element until it's parent is small (in min-heap) (O(logn))
- Re-heapify: Take bottom-right value, move it to the root, swap it down until it's parents are smaller
- Tries
- --
- Full words end in a special node that denotes end-of-word.
- Each node stores a character and the child nodes.
- Graphs
- --
- Adjacency list: obvious way to represent with nodes
- Adjacency matrix: NxN boolean matrix, true represents an edge (or weight)
- Find closest node: BFS it, since you're minimizing search space (DFS will generally take more iterations)
- DFS: Pre-order! Mark node as visited. Recursive.
- BFS: NOT RECURSIVE. Use a queue dawg. Still need to mark nodes. Add root to queue, then while queue not empty, dequeue node. For all adjacent nodes, enqueue. 4-> 2(-> 1/3) /6. Enqueue 4, Dequeue 4, enqueue 2 and 6. Dequeue 2, enqueue 1/3. Dequeue 6. Dequeue 1. Dequeue 3. Done.
- Bidirectional Search (Path from node N to node M)
- --
- If you know both nodes to start, run two BFS's from each node. Check if they have a node in common. If they do, bam, done. Check after each level.
- Takes a factor of 2 less time.
- Dynamic Programming
- --
- DRAW THE CALL TREE YOU DUMMY! It's your runtime for a given input size!
- DRAW THE CALL TREE WITH MEMOIZATION! It's STILL your runtime for a given input size!
- Consider having more than 1 base case and starting there, i.e. fib(0) / fib(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement