bst, trees, tries, graphs, bfs, dfs Flashcards
How do we find the left child of any parent element in a bst?
multiply the parent element’s index by 2 and add 1.
let left = 2indexOfParent + 1
How do we find the right child of any parent element?
multiply the parent element’s index by 2 and add 2
let right = 2indexOfParent + 2
What is the runtime of an unbalanced bst? What is the runtime of a balanced bst?
h = height of tree
unbalanced: O(h)
balanced: O(h)
How do you calculate the height of a node in a bst?
max(heighRightCild, heightLeftChild) + 1
What is the runtime of min(), max(), insert(), deletion() of a unbalanced bst? Balanced bst?
O(h)
h = length of longest path root to leaf
if balanced: h = O(lan)
Implement breath first search in a binary search tree
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;
}
What is a cycle?
When a path goes from a particular vertex back to itself
What is a directed acyclic graph, or dag?
Directed graph with no cycles
What is the in-degree?
The number of edges entering a vertex
What is the out-degree?
The number of edges leaving a vertex
What is a complete graph?
When all nodes are connected to all other nodes
What is a connected graph?
If all nodes have at least one edge
What is an Adjacency List?
A list of all the nodes connected to a node
What are the 2 main ways graphs can be represented and what are the pros and cons of each
- Adjacency lists
- list of key value pairs
- takes up less memory than matrix
- not as easy to visualize - 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 do you represent an ordered pair and how do you represent an unordered pair in mathmatics
order pair: (a,b) // order matters
unordered pair: {a,b} // order does not matter
What what is a leaf?
node that does not have a child node in the tree
What is the height of a tree?
the length of the longest path to a leaf
What is the depth of a node?
the length of the path to its root
What is a path?
Path refers to the sequence of nodes along the edges of a tree.
What is the root node of a tree?
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.
Write an algorithm that checks if tree t is a subtree of tree s
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); } }
What type of traversal is good for shortest paths?
breath first search
What type of traversal is good for solving mazes?
depth first search
What is the runtime of breath first search?
O(n)
o(m+n) m = edges n = vertices (linear time)
what is undirected connectivity
See if a vertex is included in a particular graph
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?
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
What is an arc in graph theory
a directed edge
What it topological ordering?
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
Minimal Tree: Given a sorted (increasing order) array with unique integer elements, write an
algorithm to create a binary search tree with minimal height
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; }