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