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
Directed Graph
Nodes are connected by unidirectional edges
Undirected Graph
Nodes are connected by bidirectional edges
Connected Graph
There exists a path from any vertex to any other vertex
Cycle (in a graph)
Starting at node A, there exists a path through other nodes back to A.
In an undirected graph, you may not visit the node you just visited to detect a cycle. In other words, the minimum number of nodes in an undirected cycle is 3. The minimum number of nodes in a directed cycle is 1 (self-loop).
Terminology: “cyclic graph” vs “acyclic graph”
Complete Graph
a graph in which each pair of graph vertices is connected by an edge
Sometimes called “universal graph” in older literature
Representing a graph in programming
There are 2 common ways:
- Adjacency List
- Adjacency Matrix
Adjacency List
Every vertex stores a list of adjacent vertices.
In an undirected graph, an edge like (a, b) would be stored twice: once in a’s adjacent vertices and once in b’s adjacent vertices.
Most common way to represent a graph.
class Graph: list(nodes) [type: Node]
class Node: name [type: Str] list(children) [type: Node]
Additional classes are not necessary to represent a graph. An array (or hash table) of lists can store the adjacency list. More compact but not as clean as using node classes.
Why is a Graph Unlike a Tree?
You can’t necessarily reach all the nodes from a single node.
Common Graph Search
DFS & BFS