Trees and Graphs Flashcards
Tree
a data structure composed of nodes. It’s actually a special kind of graph.
The best way to understand a tree is with a recursive explanation
- In the data structure, each tree has a root note
- Root node has 0+ child nodes
- Each child node has 0+ child nodes
- etc.
Provides fun stuff like log(n) search & insertion. Also able to store many combinations (like a dictionary) in less space and with some benefits than just storing the whole thing (see Tries and prefix matches)
Basic Tree Node Definition
TreeNode:
val (any type)
children (list of TreeNode)
Binary Tree
A tree with up to 2 children
Binary Tree vs Binary Search Tree
BST has an ordering property: all left decedents <= n. All right decedents > n
Balanced vs Unbalanced
For practical uses, this usually means “just balanced enough to ensure log(n) insert and find.
Common balanced trees are red-black trees and AVL trees
Complete Binary Tree
All levels are filled in except for the last level, which must be filled in from left-to-right with no gaps.
Full Binary Tree
Every node has zero or two children
Perfect Binary Tree
Both Full and Complete. It has 2^k - 1 nodes where k is the number of levels
Common Traversals
in-order: current in between children, visits nodes in ascending order (left, node, right)
pre-order: current before children (node, left, right)
post-order: current after children (left, right, node)
Binary Heap
Most commonly min-heaps and max-heaps. These are COMPLETE trees where the root element is either the min or max element.
MinHeap supports log(n) insert, remove, deleteMin and constant time getMin without additional memory overhead. Each node’s children are larger than it.
Key Operations: insert
and extractMin
There are ways to implement min element-type structures using queues, for example, but some of the operations either take a performance hit (e.g. linear), or require more space to store.
Insert to MinHeap
Insert to bottom of tree then percolate up. O(log n) time where n is the number of nodes in the heap.
Extract Minimum Element from MinHeap
Remove the minimum element and swap in the very last element in the heap (bottom, right-most element). Then percolate down.
Trie (Prefix Trees)
Commonly used to represent the entire (English) language for quick prefix lookups.
A Trie is a special application of an n-ary tree used to represent prefixes and complete words.
A node in a trie can have 0 to ALPHABET_SIZE + 1 children
The root of the tree does not have any value. The children represent letters or may have either a boolean or node value like ‘ * ‘ indicating that the previous path represents a complete word.
A trie can check if a string is a valid prefix in O(K) time, where K is the length of the string.
Trie vs Hash Map for prefix lookup
Both require O(K) time, but hash map requires more space than a Trie.
TODO: exact space comparison between Trie and Hash Map for prefix lookup
Graphs
Collection of nodes with edges between some of them