Data Structures & Algorithms Flashcards

1
Q

Find Height of Binary Tree

A

Recursive: Check to see if the root node is null, if so return 0. Otherwise, set an int left equal to a recursive call with the root’s left node, and the same for the right, which will recurse until it’s found the last node for each left and right. The function returns the max of left and right plus one for the root node, which happens for every branch off of left and right until the tree is fully traversed.

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

Tree

A

Data structure comprised of recursive nodes

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

Binary Tree

A

Tree where each node has up to 2 children

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

Binary Search Tree

A

Ordered tree where all left descendants are less than or equal to their right descendants.

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

Balanced Tree

A

Balanced enough, not perfectly balanced, so that we can ensure O(log n) insert and lookup.

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

Complete Binary Tree

A

Every node has either 0 or 2 children.

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

Perfect Binary Tree

A

Full and complete.

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

In Order Tree Traversal

A

“Visit” left, current node, right. In a Binary Search Tree, ascends in order.

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

Pre-Order Tree Traversal

A

“Visit” current node, left, right. Root will be first visited.

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

Post-Order Tree Traversal

A

“Visit” left, right, current node. Root will be last visited.

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

Min Heap

A

Complete Binary Tree where each node is smaller than its children.

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

Min Heap insertion

A

Insert at bottom rightmost location, fix by moving up into right spot and swapping nodes as you sort.

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

Min Heap extract

A

Remove top (min node) and replace with bottom rightmost node. Swap now-top node until min is inserted at top and tree is in correct order.

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

Tries

A

A prefix tree. Variant of n-ary tree with characters at each node. Each path down could be a word, with the end of the word represented by a null node.

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

Uses for Tries

A

Autocomplete. A trie could be used to store the English language and do O(n) prefix lookup, where n is the string prefix being searched.

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

Graph definition

A

A collection of nodes with edges between (some of) them.

17
Q

Directed Graph

A

Edges point the way, like a one way street.

18
Q

Undirected Graph

A

Edges go both ways, like a two way street.

19
Q

Connected Graph

A

Graph with an edge between every pair of vertices.

20
Q

Cyclic

A

Graph that cycles, end node touching root node

21
Q

Acyclic

A

Graph that does not have a cycle

22
Q

Adjacency List

A

Way of representing a graph where every vertex (node) stores a list of adjacent vertices (nodes).

23
Q

Adjacency Matrix

A

A NxN boolean matrix where a true value at matrix[i][j] indicates an edge between i and j. In an undirected graph, the matrix will be symmetric, and in a directed graph, it will not necessarily be symmetric.

24
Q

Depth First Search

A

Way of searching graphs that starts at the root (or another arbitrary node) and explores each branch completely before going to the next branch.

25
Q

Breadth First Search

A

Way of searching graphs that starts at the root (or other arbitrary node) and explores each neighbor before going to any of their children.

26
Q

BFS Use Case

A

Best for finding shortest path between two nodes, as it stays as close to the source node for as long as possible.

27
Q

DFS Use Case

A

Best if you want to visit every node, as all children are explored before moving on to a neighbor node.

28
Q

DFS Algorithm

A
void search(Node root) {
   if (root == null) return;
   visit(root);
   root.visited = true;
   for (Node n : root.adjacent) {
      if (n.visited == false) {
         search(n);
      }
   }
}
29
Q

BFS Algorithm

A
void search(Node root) {
   Queue queue = new LinkedList();
   root.marked = true;
   queue.add(root);
   while (!queue.isEmpty()) {
      Node r = queue.remove();
      visit(r);
      for (Node n : r.adjacent) {
         if (n.marked == false) {
            n.marked = true;
            queue.add(n);
         }
      }
   }
}
30
Q

Bidirectional Search

A

Used to find the shortest path between source and destination nodes by running two BFS searches from each node. The path is found when the searches collide.

31
Q

Bubble Sort

A

Swapping elements in pairs by order, then moving to next pair until sorted (nested for loops). O(n^2) time and O(1) space

32
Q

Selection Sort

A

Find the smallest element, move to front (nested for loops). O(n^2) time and O(1) space

33
Q

Merge Sort

A

Divide in half, repeat, sort, merge. O(n log n) time, memory depends.

34
Q

Quick Sort

A

Pick an element, move all less than to the left, greater to the right. O(n log n) time, O(log n) space.

35
Q

Radix Sort

A

A sorting algorithm that takes advantage of the fact that integers have a finite number of bits. We iterate through each digit of the number, grouping numbers by each digit. Then we sort these groupings by the next digit, and so on until the list is sorted. O(n*k) time, where n is the number of elements and k is the required passes through to sort.

36
Q

Binary Search

A

Look for element x by comparing to the midpoint of the list we’re searching. If it’s less than x, we search only the right half, and if it’s greater we search the left half, repeating this until we either find the element x or each sub array is length 0. O(log n) time, O(1) space.