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
89 changes: 75 additions & 14 deletions python/bst.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,84 @@
class Bst:
def __init__(self):
self.parent = None
pass
def __init__(self):
self.root = None

def insert(self, value):
#This is where you will insert a value into the Binary Search Tree
pass
def insert(self, value):
# This is where you will insert a value into the Binary Search Tree
if not self.root:
self.root = Node(value)

def contains(self, value):
# this is where you'll search the BST and return TRUE or FALSE if the value exists in the BST
pass
else:
self._insert(value, self.root)
# Private helper function that's recursive

def remove(self, value):
# this is where you will remove a value from the BST
pass
def _insert(self, value, current_node):
# Edge case: if value is already in the tree
if value == current_node.value:
print("This value is already in the BST!")
return

# If value we're about to insert is less than the node we're on...
if value < current_node.value:
# If current node does not have a left child, create a new node and insert it as the left child
if current_node.left is None:
current_node.left = Node(value)
else:
# If the current node does have a left child, then we recurse and pass in the value and current node that we're on
self._insert(value, current_node.left)
# Same as the left subtree, but now we're applying it to the right subtree
elif value > current_node.value:
if current_node.right is None:
current_node.right = Node(value)
else:
self._insert(value, current_node.right)

def contains(self, value):
# this is where you'll search the BST and return TRUE or FALSE if the value exists in the BST

# In order traversal
self.in_order_array = self.in_order_traversal(self.root)
if value in self.in_order_array:
return True
else:
return False

def in_order_traversal(self, current_node):
elements = []
# If the current node does not equal None
if current_node:
# visit left subtree
elements += self.in_order_traversal(current_node.left)

elements.append(current_node.value)

elements += self.in_order_traversal(current_node.right)

return elements

def remove(self, value):
# this is where you will remove a value from the BST
pass


# ----- Node ------
class Node:
# store your DATA and LEFT and RIGHT values here
pass
# store your DATA and LEFT and RIGHT values here
def __init__(self, value):
self.value = value
self.left = None
self.right = None


b = Bst()

b.insert(8)
b.insert(3)
b.insert(1)
b.insert(6)
b.insert(4)
b.insert(7)
b.insert(10)
b.insert(14)
b.insert(13)

print(b.contains(1))
54 changes: 35 additions & 19 deletions python/hash.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
# Does not account for collisions!!!!

class HashTable:
def __init__(self, number_of_buckets):
# self.number_of_buckets = number_of_buckets
# self.table = [None] * self.number_of_buckets
pass
def __init__(self, number_of_buckets):
# self.number_of_buckets = number_of_buckets
# self.table = [None] * self.number_of_buckets
self.number_of_buckets = number_of_buckets
self.arr = [None for i in range(self.number_of_buckets)]

def hash(self, value):
# here is where you'll turn your 'value' into a hash value that will return the index of your table to insert value
# IMPORTANT: Think about how you'd store values into the same index
total = 0
for char in value:
# Uses ASCII value from char
total += ord(char)
# uses modulo operator to get an index
return total % self.number_of_buckets

def hash(self, value):
# here is where you'll turn your 'value' into a hash value that will return the index of your table to insert value
# IMPORTANT: Think about how you'd store values into the same index
pass
def set(self, key, value):
# here is where you'll perform your logic to insert the value into your table
# you'll also call your hash method here to get the index where you'll store the value

def set(self, value):
# here is where you'll perform your logic to insert the value into your table
# you'll also call your hash method here to get the index where you'll store the value
pass
# use hash function and get index value
h = self.hash(key)
# use index value to insert into "array"
self.arr[h] = value

def get(self, value):
# here is where you'll retreive your value from the hash table
# IMPORTANT: Think about how you'd retreive a value that from an index that has multiple values
pass
def get(self, key):
# here is where you'll retreive your value from the hash table
# IMPORTANT: Think about how you'd retreive a value that from an index that has multiple values
h = self.hash(key)
return self.arr[h]

def has_key(self, value):
# here is where you'll return a True or False value if there is a value stored at a specific index in your HashTable
pass
def has_key(self, value):
# here is where you'll return a True or False value if there is a value stored at a specific index in your HashTable
for num in self.arr:
if value == num:
return True
return False
68 changes: 56 additions & 12 deletions python/linked_list.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,63 @@
class LinkList:
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
# write your __init__ method here that should store a 'head' value which the first Node in the LinkedList and a 'length' value which is the total number of Nodes in the LinkedList
def __init__(self):
self.head = None

def add(self, data):
# write your code to ADD an element to the Linked List
pass
def add(self, data):
# write your code to ADD an element to the Linked List
new_node = Node(data)
if not self.head:
self.head = new_node
else:
old_node = self.head
self.head = new_node
self.head.next_node = old_node

def remove(self, data):
# write your code to REMOVE an element from the Linked List
pass
def remove(self, target):
# write your code to REMOVE an element from the Linked List
previous = None
current = self.head
if self.head is None:
return False
else:
while current:
if current.data == target:
if previous:
previous.next_node = current.next_node
else:
self.head = current.next_node
previous = current
current = current.next_node
return False

def get(self, element_to_get):
# write you code to GET and return an element from the Linked List
pass
def get(self, target):
if self.head is None:
return False
else:
current = self.head
if current.data == target:
return True
while current.next_node:
current = current.next_node
if current.data == target:
return True
return False

def print_list(self):
this_node = self.head
while this_node is not None:
print(this_node, end="->")
this_node = this_node.next_node
print('None')
# ----- Node ------


class Node:
# store your DATA and NEXT values here
pass
# store your DATA and NEXT values here
def __init__(self, data):
self.data = data
self.next_node = None
self.previous_node = None

def __str__(self):
return ('(' + str(self.data) + ')')
27 changes: 17 additions & 10 deletions python/queue.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
class Queue:
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Queue and a 'queue' value which is an array of stored values in the Queue
def __init__(self):
self.queue = []

def enqueue(self):
# write your code to add data to the Queue following FIFO and return the Queue
pass
def enqueue(self, data):
# write your code to add data to the Queue following FIFO and return the Queue
self.queue.insert(0, data)

def dequeue(self, data):
# write your code to removes the data to the Queue following FIFO and return the Queue
pass
def dequeue(self):
# write your code to removes the data to the Queue following FIFO and return the Queue
if len(self.queue) > 0:
self.queue.pop()
else:
print("Queue is empty!")
return "Queue is empty!"

def size(self):
# write your code that returns the size of the Queue
pass
def size(self):
# write your code that returns the size of the Queue
print(len(self.queue))
return len(self.queue)
27 changes: 17 additions & 10 deletions python/stack.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
class Stack:
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack
# write your __init__ method here that should store a 'total' value which is the total number of elements in the Stack and a 'stack' value which is an array of stored values in the Stack
def __init__(self):
self.stack = []

def push(self):
# write your code to add data following LIFO and return the Stack
pass
def push(self, item):
# write your code to add data following LIFO and return the Stack
self.stack.append(item)

def pop(self, data):
# write your code to removes the data following LIFO and return the Stack
pass
def pop(self):
# write your code to removes the data following LIFO and return the Stack
if len(self.stack) > 0:
self.stack.pop()
else:
print("Your stack is empty!")
return "Your stack is empty!"

def size(self):
# write your code that returns the size of the Stack
pass
def size(self):
# write your code that returns the size of the Stack
print(len(self.stack))
return len(self.stack)