Trees and Graphs Flashcards

1
Q

Tree

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Basic Tree Node Definition

A

TreeNode:
val (any type)
children (list of TreeNode)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Binary Tree

A

A tree with up to 2 children

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Binary Tree vs Binary Search Tree

A

BST has an ordering property: all left decedents <= n. All right decedents > n

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Balanced vs Unbalanced

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Complete Binary Tree

A

All levels are filled in except for the last level, which must be filled in from left-to-right with no gaps.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Full Binary Tree

A

Every node has zero or two children

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Perfect Binary Tree

A

Both Full and Complete. It has 2^k - 1 nodes where k is the number of levels

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Common Traversals

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Binary Heap

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Insert to MinHeap

A

Insert to bottom of tree then percolate up. O(log n) time where n is the number of nodes in the heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Extract Minimum Element from MinHeap

A

Remove the minimum element and swap in the very last element in the heap (bottom, right-most element). Then percolate down.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Trie (Prefix Trees)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Trie vs Hash Map for prefix lookup

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Graphs

A

Collection of nodes with edges between some of them

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Directed Graph

A

Nodes are connected by unidirectional edges

17
Q

Undirected Graph

A

Nodes are connected by bidirectional edges

18
Q

Connected Graph

A

There exists a path from any vertex to any other vertex

19
Q

Cycle (in a graph)

A

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”

20
Q

Complete Graph

A

a graph in which each pair of graph vertices is connected by an edge

Sometimes called “universal graph” in older literature

21
Q

Representing a graph in programming

A

There are 2 common ways:

  1. Adjacency List
  2. Adjacency Matrix
22
Q

Adjacency List

A

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.

23
Q

Why is a Graph Unlike a Tree?

A

You can’t necessarily reach all the nodes from a single node.

24
Q

Common Graph Search

A

DFS & BFS

25
Q

Common scenarios for graph DFS

A

Often preferred for visiting every node in the graph (simpler)

26
Q

Common scenarios for graph BFS

A

Often preferred for (shortest) path

27
Q

Graph DFS Implementation

A

Either have a visited set or visited attribute on the node itself.

For a connected graph, start with a root. then search each of its adjacent nodes.

If the graph is not connected, we’ll need a mechanism to pick a new root. Perhaps use a hash set of all nodes. If one is visited, remove from the hash set of “not-visited”

28
Q

Graph BFS Implementation

A

NOT recursive. Uses a queue. (usually best)

Mark the root, and enqueue. Do not enqueue an already marked node. It hasn’t yet been visited, but it’s been “marked” because it’s been inserted into the queue.

29
Q

Graph Bidirectional Search

A

Find shortest path between a source and destination node. Essentially 2 simultaneous BFS searches. We’ve found a path once the searches collide.

This is because, if we have a path of length (depth) d, each of our searches only need to check d/2 levels. Since exploring each depth is exponential, we would have had to search k^d nodes, but instead we only need to search 2k^(d/2) nodes

Faster than normal BFS by a factor of K^(d/2) where k is the maximum number of adjacent nodes from any node.

30
Q

Topological Sort

A

todo

31
Q

Dijkstra’s Algorithm

A

todo

32
Q

AVL Trees

A

todo

33
Q

Red-Black Trees

A

wishlist?

34
Q

Adjacency Matrices

A

A common way to represent a graph.

  • NxN boolean matrix (N is the number of nodes)
  • matrix[i][j] == true => ∃ edge from node i to node j (i.e. from ROW to COL)
  • matrix is symmetric in undirected graphs

Can perform the same algorithms used on adjacency lists (e.g. BFS), but they’re somewhat less efficient since at each node, you don’t have direct access to all neighbors. In adjacency matrix, iteration will be O(NxN) as opposed to O(N) (yes?)

TODO: when are adjacency matrices used then?