370 midterm notes

This commit is contained in:
Medium Fries
2019-04-15 20:06:49 -07:00
parent 787e8bed86
commit 505c09500c
9 changed files with 266 additions and 7 deletions

53
370/past-midterms/2.md Normal file
View File

@@ -0,0 +1,53 @@
# Time/space complexity
For basically everthing, just list it out somewhere on sheet
# AVL Tree's
Get some functions for them written out
Insert/Deletion/Lookup Functions
# No pathfinding code
_mainly cuz we never did any for homework/class_
Time/space complexities should be straightforward memorization
# Tries
# Huffman
Decoding:
: Time - O(n) {n = number of input bits}
Encoding
: Time O(nlog(n))
: Where n is the number of unique bits in the string
# Pathfinding - Conceptual things
Admissability: allowed to underestimate but not overestimate.
> Heuristic?
* A-star
* Best-first
> Multiple sources
* Floyd's Algorithm
> Multiple destination
* Floyd's
* Bellman-Ford
> Negative edges
* Floyd
* Bellman-Ford
> Negative Cycles
None

96
370/past-midterms/2.py Normal file
View File

@@ -0,0 +1,96 @@
class HuffmanNode:
# $ will represent a dummy node
# anything else is a regular node
def __init__(self, data, freq):
self.data = data
self.freq = freq
class HuffmanTree:
def __init__(self, node):
self.root = node
def frequencyMap(self, string):
ret = {}
for i in string:
if i not in ret:
ret[i] = Node(i, 1)
else:
ret[i].freq += 1
return ret
def encode(self, string):
f = self.frequencyMap(string)
class TrieNode:
def __init__(self):
# 1 ref for each character in the alphabet
self.isleaf = False
self.edges = [0] * 26
class TrieTree:
def __init__(self, root):
self.root = root
# just not going to bother with capitals
def _charIndex(self, char):
return ord(char) - ord('a')
def insert(self, string):
curr = self.root
for i in string:
alphabetIndex = _charIndex(i)
if curr[alphabetIndex] is None:
curr.edges[alphabetIndex] = Node()
# go to the nexxt thing
curr = curr.edges[alphabetIndex]
else:
# Finally mark the last node as a leaf
curr.isleaf = True
def lookup(self, string):
curr = self.root
# First we should go straight to the end of the string
# That's why we don't do any checks
for i in string:
alphaIndex = _charIndex(i)
curr = curr.edges[alphaIndex]
return curr.isleaf
def remove(self, string):
pass
def traverse(self, node, preFix):
if node:
if n.isleaf:
print(prefix)
# recursively go through the tree
for i in prefix:
traverse(node.edges[_i], prefix+_charIndex(i))
class AVLNode:
def __init__(self, data):
self.data = data
self.height = 0
self.left = None
self.Right = None
class AVLTree:
def __init__(self,node):
self.root = node
def rotateLeft(self, node):
tmp = node.right
node.left = tmp.right
tmp.right = node
# Set the heights on each node
node.height = 1 + max(node.left.height, node.right.height)
tmp.height = 1 + max(tmp.left.height, tmp.right.height)
return tmp
def rotateRight(self, node):
pass

Binary file not shown.

27
370/past-midterms/tmp.py Normal file
View File

@@ -0,0 +1,27 @@
class Trie:
def __init__(self, *words):
self.root = {}
for word in words:
self.insert(word)
def insert(self, word):
curr = self.root
for c in word:
if c not in curr:
curr[c] = {}
else:
curr = curr[c]
# add a marker to show that we are a leaf
curr['.'] = '.'
self.root = curr
def remove(self, word):
pass
def printWord(self, word):
pass
def all(self):
pass

59
370/past-midterms/trie.py Normal file
View File

@@ -0,0 +1,59 @@
class Node:
def __init__(self, leaf=False):
# refs list of next valid characters
self.letters = [None] * 26
self.isLeaf = leaf
class Trie:
def __init__(self):
self.root = Node()
def _charIndex(self, char):
return ord(char) - ord('a')
def insert(self, string):
curr = self.root
for i in string:
refIndex = self._charIndex()
# create nodes if we have to
if curr.letters[refIndex] is None:
curr.letters[refIndex] = Node()
curr = curr.letters[refIndex]
# Set the last letter to be a leaf
curr.isLeaf = True
def remove(self, string):
# Lazy removal
curr = self.root
for i in string:
refIndex = self._charIndex(i)
if curr.letters[refIndex] is None:
break
curr = curr.letters[refIndex]
curr.isLeaf = False
# We're also going to be printing things as we go along the thing
def lookup(self, string):
curr = self.root
for i in string:
refIndex = self._charIndex(i)
# Make sure we don't walk into nothing
if curr.letters[refIndex] is None:
return False
curr = curr.letters[refIndex]
print(i, end='')
return curr.isLeaf
def all(self, node, word):
if node is None:
return
if node.isLeaf:
# Print out the word we have so far
self.lookup(word)