bst, trees, tries, graphs, bfs, dfs Flashcards

1
Q

How do we find the left child of any parent element in a bst?

A

multiply the parent element’s index by 2 and add 1.

let left = 2indexOfParent + 1

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

How do we find the right child of any parent element?

A

multiply the parent element’s index by 2 and add 2

let right = 2indexOfParent + 2

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

What is the runtime of an unbalanced bst? What is the runtime of a balanced bst?

A

h = height of tree

unbalanced: O(h)
balanced: O(h)

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

How do you calculate the height of a node in a bst?

A

max(heighRightCild, heightLeftChild) + 1

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

What is the runtime of min(), max(), insert(), deletion() of a unbalanced bst? Balanced bst?

A

O(h)

h = length of longest path root to leaf
if balanced: h = O(lan)

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

Implement breath first search in a binary search tree

A

bfs(root) {
let result = [];
let queue = [];

queue.push(root);

  while(queue.length) {
    let curr = queue.shift();
    result.push(curr.value)
    if (curr.left) {
      queue.push(curr.left)
    }
    if (curr.right) {
      queue.push(curr.right)
    }
  }

return result;
}

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

What is a cycle?

A

When a path goes from a particular vertex back to itself

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

What is a directed acyclic graph, or dag?

A

Directed graph with no cycles

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

What is the in-degree?

A

The number of edges entering a vertex

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

What is the out-degree?

A

The number of edges leaving a vertex

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

What is a complete graph?

A

When all nodes are connected to all other nodes

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

What is a connected graph?

A

If all nodes have at least one edge

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

What is an Adjacency List?

A

A list of all the nodes connected to a node

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

What are the 2 main ways graphs can be represented and what are the pros and cons of each

A
  1. Adjacency lists
    - list of key value pairs
    - takes up less memory than matrix
    - not as easy to visualize
  2. Adjacency matrix
    - 2d array filled with 1’s and 0’s
    - 1 means connection and 0 means no connection
    - takes up more space but easier to visualize and represent the data
    - example: if there are not a lot of edges then the matrix will be filled with lots of 0’s which are not used

Generally speaking, graphs with lots of edges fare better as matrices and graphs with fewer edges fare better as lists.

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

How do you represent an ordered pair and how do you represent an unordered pair in mathmatics

A

order pair: (a,b) // order matters

unordered pair: {a,b} // order does not matter

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

What what is a leaf?

A

node that does not have a child node in the tree

17
Q

What is the height of a tree?

A

the length of the longest path to a leaf

18
Q

What is the depth of a node?

A

the length of the path to its root

19
Q

What is a path?

A

Path refers to the sequence of nodes along the edges of a tree.

20
Q

What is the root node of a tree?

A

The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node.

21
Q

Write an algorithm that checks if tree t is a subtree of tree s

A
public class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if (s == null) return false;
        if (isSame(s, t)) return true;
        return isSubtree(s.left, t) || isSubtree(s.right, t);
    }
private boolean isSame(TreeNode s, TreeNode t) {
    if (s == null && t == null) return true;
    if (s == null || t == null) return false;

    if (s.val != t.val) return false;
        return isSame(s.left, t.left) && isSame(s.right, t.right);
    }
}
22
Q

What type of traversal is good for shortest paths?

A

breath first search

23
Q

What type of traversal is good for solving mazes?

A

depth first search

24
Q

What is the runtime of breath first search?

A

O(n)

o(m+n) m = edges n = vertices (linear time)

25
Q

what is undirected connectivity

A

See if a vertex is included in a particular graph

26
Q

How do you find all connected components of an undirected graph given a list of node (all nodes are not necessarily connected to each other)? what is the runtime?

A
bfs strat
- given list of n nodes to check
- all nodes set to unexplored
- for i = a to n
  if i not yet explored do
  bfs(graph, i)
- in bfs we set each node's explored to true

runtime:
- look through all nodes -> m + n time -> n time
- O(1) timer per node in the bfs
O(n) runtime

27
Q

What is an arc in graph theory

A

a directed edge

28
Q

What it topological ordering?

A

it’s an ordering of the vertices of a graph so that all of the arcs, the directed edges of the graph, only go forward in the ordering.

a -> b -> c >d
a——–> c
b——–> d

  • goes in one direction
  • no cycle
29
Q

Minimal Tree: Given a sorted (increasing order) array with unique integer elements, write an
algorithm to create a binary search tree with minimal height

A
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}
const createMinimalBST = (a) => {
  const midIndex = Math.floor(a.length / 2);
  const midElement = a[midIndex];
  const left = a.slice(0, midIndex);
  const right = a.slice(midIndex+1);
  const root = new Node(midElement);
  if(left.length) root.left = createMinimalBST(left);
  if(right.length) root.right = createMinimalBST(right);
  return root;
}