Data Structures Flashcards

1
Q

What is a graph

A

Any collection of nodes and their pointers to other nodes. Linked lists and trees are both types of graphs

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

What is a binary tree

A

A binary tree is a type of tree. It is a collection of nodes. Every node has between 0 to 2 children, and every node except the root has exactly one parent.

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

In a tree, what is a root node

A

The root node is the node at the “top” of the tree. Every node in the tree is accessible starting from the root node. The root node has no parents

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

In a tree, what is a parent and what is a child

A

If you have a node A with an edge to a node B, so A -> B, we call A the parent of node B, and node B a child of node A.

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

In a tree, what is a leaf node

A

A leaf node is a node that has no children

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

In a tree, what is the depth of a node

A

The depth of a node is how far it is from the root node. The root has a depth of 0. Every child has a depth of parentsDepth + 1, so the root’s children have a depth of 1, their children have a depth of 2, and so on.

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

in a tree, what is a subtree

A

A subtree of a tree is a node and all of it’s descendants. A subtree is a valid tree where the chose node is the root of the subtree

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

What are the advantages/disadvantages of DFS vs BFS

A

DFS can waste a lot of time if the value you are searching for is on the side which you are searching second

BFS can waste a lot of time if your values are in leaf nodes

Space complexity for a perfect binary tree in DFS is O(log n) while, for a BFS it is O(n)

For a lopsided tree, like a line, DFS space complexity will be O(n) and BFS will be O(1)

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

Define a Binary Search Tree (BST)

A

A BST is a binary tree, for each node the value of the node is greater than all of the values of the left children and less than all of the values of the right children

Operations like searching, adding, and removing can all be done in O(log n)

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

What traversal will handle the nodes of a binary search tree in sorted order

A

in-order left -> right

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

In a graph, what does it mean to have directed edges

A

Directed edges mean you can only traverse in one direction

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

In a graph, what does it mean to have undirected edges

A

Undirected edges mean you can traverse in both directions

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

In a graph, what is a connected component

A

A connected component is a group of nodes that are connected by edges

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

In a graph, what is a node’s indegree

A

A nodes indegree is the number of edges that can be used to reach a node

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

In a graph, what is a node’s outdegree

A

A nodes outdegree is the number of edges that can be used to leave a node

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

In a graph, what is a neighbor

A

Neighbors are nodes connected by an edge. In the graph A <-> B <-> C, A is neighbors with B, B is neighbors with A and C, and C is neighbors with B

17
Q

In the context of a graph, what is an adjacency list

A
  • A list of vertices where each vertex has a list of neighboring vertices
  • In python, you can use the defaultdict(list) to hold and adjacency list
0->[1,2]
1->[2,3]
2->[3]
18
Q

In the context of a graph, what is an adjacency matrix

A
  • A matrix that is used to represent adjacent vertices
  • A row represents a vertex and it’s columns represents if the vertex has an edge between the corresponding vertex
19
Q

In the context of a graph, what is an array of edges

A
  • An array of pairs of vertices that are connected by an edge
[[0,1], [1,2], [0,3]]
20
Q

In the context of a graph, what is a matrix

A
  • A mxn coordinate system
  • Generally this is represented as a 2-D array
21
Q

What is a greedy algorithm

A
  • An algorithm that makes a locally optimal decision at every step
  • Most greedy problems are looking for a maximum or minimum
  • Sorting is the most common algorithm associated with greedy algorithms
22
Q

What is a trie or prefix trie

A
  • A trie is graph that stores characters at each node
  • It can be used to efficiently implement string searching/matching patterns