Data Structure/Algos Flashcards

1
Q

BST Insert/Add

A

// if insert/add at head need node** head
if val < head.val;
if left is null then add, else go left;
elif val > head.val;
if right is null then add, else go right;
else
error // already exists

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

BST remove/delete

A

// algorithm must stay at parent and check ahead for value

  1. hang right child on parents child node
  2. hang left child on right childs far left node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

BST find_min

A

recursively left until left is null, return head->val;

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

BST find

A
if (val == head->val)
  return head;
elif (val < head->val)
  go left
elif (val > head->val)
  go right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

BST preorder visit

A

preorder()
visit();
preorder(left);
preorder(right);

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

BST inorder visit

A

inorder()
inorder(left);
visit();
inorder(right);

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

BST postorder visit

A

postorder()
postorder(left);
postorder(right);
visit();

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

BST find_next

A

// if right child of node is NOT null then its far left child is the next. Simple.
// otherwise if the node is a right child there is NO next.
// otherwise if the node is a left child its parent is next.
if (node->right != NULL)
return FindMin(node->right);
else
FindNext_();

void FindNext_(T val, struct BstNode** next, struct BstNode* head)
        {
                if (head == NULL || head->val == val)
                        return;
                if (head->val > val)
                {
                        *next = head;
                        FindNext_(val, next, head->left);
                }
                else if (head->val < val)
                {
                        //could still be a left child and parent is the next, but may have to traverse right
                        //to eventually get there
                        FindNext_(val, next, head->right);
                }
        }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

quicksort()

A
quicksort(*array, low, high)
  if (low < high)
    pivot = partition(array,low,high)
    quicksort(array,low,pivot-1)
    quicksort(array,pivot+1,high)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

quicksort::partition()

A
partition(*array, low, high)
  pivot = array[low]
  small = low
  for(i,low+1:high)
    if (array[i] <= pivot)
      small++
      swap(array[i],array[small])
  swap(array[low],array[small]) //move pivot to pivot idx
  return small // index of pivot
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

quicksort description

A
  1. choose a pivot
  2. “partition” array around pivot (move small below and high above)
  3. recurse 1. with array below pivot and above pivot (excluding pivot)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

mergesort description

A
  1. divide the list into two halfs by pointing 2 pointers to the head and iteratoring over the list incrementing 1 pointer twice as fast
  2. recurse 1. with first half
  3. recurse 1. with second half
  4. merge the two halves by iterating over them both taking and incrementing only the lowest value (so next iteration the higher value is again compare)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

mergesort()

A
mergesort(**head)
  *a = *head
  *b
  divide(a, &amp;b)
  mergesort(a)
  mergesort(&amp;b)
  merge(a, b, head)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

mergesort::divide()

A
divide(*a, **b)
  //iterator over the list incrementing one pointer twice as fast - so when it reaches the end the other pointer will point to the middle
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

mergesort::merge()

A
//new head will be smallest of two heads
//iterate over the lists together taking and moving past the smallest only each time
// ex:
while a!null and b!null
if a.val < b.val
  tail.next = a
  a = a.next
else
  tail.next = b
  b = b.next
tail = tail.next
//one list finishes first, so special case to grab last value
if a is null
  tail.next = b
else
  tail.next = a
How well did you know this?
1
Not at all
2
3
4
5
Perfectly