CS106B Notes
CS106B Notes
CS106B Notes
5/29 Section
- Recursion
base case
recursive call(s)
- Recursive backtracking
base case. return true
optional base case, return false
for each possible path in this current function
choose a path // often means removing the option from a set of workable paths
recurse on this path
if (true) return true
else un-choose the path
return false
- Memory
pointer = memory address that stores something in heap
int x = 3;
int* xp = &x; // xp points to x
int y = *xp; // y = 3, the value xp points to
- Struct
a piece of memory that contains multiple fields
- Hash Map
values into the hash() function, which returns an index for the hash buckets array
an array of pointers to linked lists
if multiple values have the same index, chain together nodes
if load factor is too high, increase size of array and rehash everything
- Graph
set of vertices and edges
directed edge: only goes one way
cycle: mark every node checked on path, check every possible path
recursive backtracking, use set
Detecting cycles
while (!toBeVisited.isEmpty()) {
Vertex* v = toBeVisited.front(); // grabs an element from the front of the set
Set<Vertex*> beingLookedFor;
if (isReachable(v, graph, beingLookedFor, prevVisited)) return true;
toBeVisited -= prevVisited;
}
return false;
}
void myHashMap::add(string key, int val) { // :: means to be part of the hashmap class
int index = hash(key);
node * curr = buckets[index];
if (curr == NULL) {
node* newNode = new node;
newNode -> key = key;
newNode -> value = val;
curr = newNode;
} else {
while (curr != NULL) {
if (curr -> key == key) {
curr -> value = val;
return;
}
curr = curr -> next;
}
// now that curr == NULL, you dont know where you are, so append new node in
the beginning instead of at the end
node* newNode = new node;
newNode -> key = key;
newNode -> value = val;
curr = buckets[index]; // keeps a handle on the linked list
buckets[index] = newNode;
newNode -> next = curr;
}
size++;
if (((double)size) / numBuckets > MAX_LOAD) { // check load factor
resize();
}
}
void myHashMap::resize() { // resize the array to be 2*original size + 1 and then go through
every single node in the array and rehash them
int oldBuckets = numBuckets;
numBuckets = numBuckets*2 + 1; // want numBuckets to stay odd
node** newBuckets = new node*[numBuckets];
for (int i = 0; i < numBuckets; i++) {
newBuckets[i] == NULL; // makes sure everything is null, no garbage memory
}
for (int i = 0; i < oldBuckets; i++) {
while (buckets[i] != NULL) { // pull node from front of linked list, change pointer
node * curr = buckets[i];
if (buckets[i] -> next != NULL) buckets[i] = buckets [i] -> next;
int index = hash(curr -> key);
curr -> next = newBuckets[index];
newBuckets[index] = curr;
}
}
delete buckets[];
buckets = newBuckets;
}
5/9
- Heapsort
1. Insert unsorted elements one at a time into a heap until all are added
2. Remove them from the heap one at a time (remove the next biggest item for max-heap; next
smallest item for min-heap)
5/5
--
void IntVector::clear() {
count = 0;
delete [] data;
capacity = 10;
data = new int[capacity];
}
--
- structure of a .h file
// foobar.h
#ifndef __foobar_h // protection in case multiple .cpp files include foobar.h
#define _foobar_h // so that contents wont get declared twice
#endif
- operator overloading
5/2
- to copy over a new array, have to iterate through
- delete [] data // deletes an array called data on the heap
5/1
--
linked lists: a struct containing an integer and a pointer to the next one
4/25
4/7
4/4
- string.find
string name = Donald Knuth;
if (name.find(Knu) != string:npos) {
name.erase(7.5) // Donald
}
- grid: 2D vector
Ex. Queen Safety
#include grid.h
int main() {
Grid<bool> board(8,8);
clearBoard(board);
return 0;
}
4/2
- declare functions first / above main(), sometimes unable to order functions in a way such that it
is implemented before it is used (mutual recursion; functions call each other)
- header
#include <iostream>
#include console.h // Stanford library
using namespace std;
- cout << // console output
- cin >>
- endl; // make a new line
- recursion: when a function calls itself
- int main() {
cout << stuff << endl;
getLin(); // allows for us to see stuff before main exits
return 0;
}
- int& //int reference, affects originally copy of int