Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 92 additions & 89 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import sys
sys.path.append('../queue_and_stack')
from dll_stack import Stack
from dll_queue import Queue


class BinarySearchTree:
def __init__(self, value):
self.value = value
Expand All @@ -12,103 +6,112 @@ def __init__(self, value):

# Insert the given value into the tree
def insert(self, value):
if value < self.value and self.left is None:
self.left = BinarySearchTree(value)
elif value >= self.value and self.right is None:
self.right = BinarySearchTree(value)
elif value < self.value and self.left is not None:
self.left.insert(value)
elif value >= self.value and self.right is not None:
self.right.insert(value)
# compare root node
if value < self.value:
# if lesser go left
if not self.left:
self.left = BinarySearchTree(value)
else:
# if something is already there, recursive
self.left.insert(value)
# value is greater go right of root
else:
if not self.right:
self.right = BinarySearchTree(value)
else:
self.right.insert(value)

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
if target == self.value:
if self.value == target:
return True
elif target < self.value and self.left is None:
return False
elif target >= self.value and self.right is None:
return False
elif target < self.value and self.left is not None:
self = self.left
return self.contains(target)
elif target >= self.value and self.right is not None:
self = self.right
return self.contains(target)
if target < self.value:
# look left, no child there
if not self.left:
return False
# if there is something there, recurse return
else:
return self.left.contains(target)
# look right, same logic as left
else:
if not self.right:
return False
else:
return self.right.contains(target)

# Return the maximum value found in the tree
def get_max(self):
while self.right is not None:
self = self.right

return self.value
def get_max(self):
# if there is no right node, you've gone far right as possible, return max
if not self.right:
return self.value
else:
# recurse till you get furthest right, return
return self.right.get_max()

# Call the function `cb` on the value of each node
# You may use a recursive or iterative approach
def for_each(self, cb):

cb(self.value)

if self.left:
self.left.for_each(cb)
if self.right:
self.right.for_each(cb)

# DAY 2 Project -----------------------

# Print all the values in order from low to high
# Hint: Use a recursive, depth first traversal
def in_order_print(self, node):
if node.left:
node.in_order_print(node.left)
print(node.value)
if node.right:
node.in_order_print(node.right)

# Print the value of every node, starting with the given node,
# in an iterative breadth first traversal
def bft_print(self, node):
self.queue = Queue()
self.queue.enqueue(node)

while self.queue.len() > 0:
node = self.queue.dequeue()
if node.left:
self.queue.enqueue(node.left)
if node.right:
self.queue.enqueue(node.right)
print(node.value)

# Print the value of every node, starting with the given node,
# in an iterative depth first traversal
def dft_print(self, node):
self.stack = Stack()
self.stack.push(node)
while self.stack.len() > 0:
node = self.stack.pop()
if node.left:
self.stack.push(node.left)
if node.right:
self.stack.push(node.right)
print(node.value)

# STRETCH Goals -------------------------
# Note: Research may be required

# Print In-order recursive DFT
def pre_order_dft(self, node):
print(node.value)
if node.left:
node.pre_order_dft(node.left)
if node.right:
node.pre_order_dft(node.right)

# Print Post-order recursive DFT

def post_order_dft(self, node):
if node.left:
node.post_order_dft(node.left)
if node.right:
node.post_order_dft(node.right)
print(node.value)
# # DAY 2 Project -----------------------

# # Print all the values in order from low to high
# # Hint: Use a recursive, depth first traversal
# def in_order_print(self, node):
# if node.left:
# node.in_order_print(node.left)
# print(node.value)
# if node.right:
# node.in_order_print(node.right)

# # Print the value of every node, starting with the given node,
# # in an iterative breadth first traversal
# def bft_print(self, node):
# self.queue = Queue()
# self.queue.enqueue(node)

# while self.queue.len() > 0:
# node = self.queue.dequeue()
# if node.left:
# self.queue.enqueue(node.left)
# if node.right:
# self.queue.enqueue(node.right)
# print(node.value)

# # Print the value of every node, starting with the given node,
# # in an iterative depth first traversal
# def dft_print(self, node):
# self.stack = Stack()
# self.stack.push(node)
# while self.stack.len() > 0:
# node = self.stack.pop()
# if node.left:
# self.stack.push(node.left)
# if node.right:
# self.stack.push(node.right)
# print(node.value)

# # STRETCH Goals -------------------------
# # Note: Research may be required

# # Print In-order recursive DFT
# def pre_order_dft(self, node):
# print(node.value)
# if node.left:
# node.pre_order_dft(node.left)
# if node.right:
# node.pre_order_dft(node.right)

# # Print Post-order recursive DFT

# def post_order_dft(self, node):
# if node.left:
# node.post_order_dft(node.left)
# if node.right:
# node.post_order_dft(node.right)
# print(node.value)
25 changes: 0 additions & 25 deletions names/dll_queue.py

This file was deleted.

21 changes: 0 additions & 21 deletions names/dll_stack.py

This file was deleted.

20 changes: 7 additions & 13 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from binary_search_tree import BinarySearchTree
import time
import sys
sys.path.append('../binary_search_tree')


start_time = time.time()

Expand All @@ -13,22 +12,17 @@
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

# duplicates = []
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

# using BST to improve speed
# I think the runtime complexity is O(n^2) because each for loop is O(n), multiplying each other since we are comparing

duplicates = []
bst = BinarySearchTree("names")

for name_1 in names_1:
bst.insert(name_1)
for name_2 in names_2:
if bst.contains(name_2):
duplicates.append(name_2)
for names in names_1:
bst.insert(names)
for names in names_2:
if bst.contains(names):
duplicates.append(names)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand Down
3 changes: 3 additions & 0 deletions reverse/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ def contains(self, value):
return False

def reverse_list(self):
# start at the head, make sure it's not empty
current = self.head
if current == None:
return self.head
# assign var to next node
next = current.next_node
# while next exists, reverse nodes, return
while next != None:
previous = current
current = next
Expand Down
50 changes: 30 additions & 20 deletions ring_buffer/ring_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,46 @@ def __init__(self, capacity):
self.storage = DoublyLinkedList()

def append(self, item):
if self.storage.length < self.capacity:
self.storage.add_to_head(item)
self.current = self.storage.tail
elif self.current is self.storage.tail:
self.storage.remove_from_tail()
# if ring isn't full add to ring set to tail
if self.capacity > self.storage.length:
self.storage.add_to_tail(item)
self.current = self.current.prev
elif self.current is self.storage.head:
# also set to head
self.current = self.storage.head
# if ring is full delete head
elif self.capacity == self.storage.length:
# head is oldest item in ring
oldest_item = self.storage.head
# delete head
self.storage.remove_from_head()
self.storage.add_to_head(item)
self.current = self.storage.tail
else:
self.current.insert_after(item)
self.current.delete()
self.current = self.current.prev
# add item to tail
self.storage.add_to_tail(item)
# if only 1 item in ring, set to head and tail
if oldest_item == self.current:
self.current = self.storage.tail

def get(self):
# Note: This is the only [] allowed
list_buffer_contents = []
# add first item
first_item = self.current
list_buffer_contents.append(first_item.value)

current = self.storage.head
while current != None:
list_buffer_contents.insert(0, current.value)
if current.next == None:
break
else:
current = current.next
# check to see if there is next item
if first_item.next:
next_item = first_item.next
else:
next_item = self.storage.head

# loop through dll, appending items till done
while next_item is not first_item:
list_buffer_contents.append(next_item.value)
if next_item.next:
next_item = next_item.next
else:
next_item = self.storage.head
return list_buffer_contents


# ----------------Stretch Goal-------------------


Expand Down
Loading