From ac9c99e91e65ff01dce93eca1a7e40c3977a6fff Mon Sep 17 00:00:00 2001 From: cecarbs Date: Thu, 18 Feb 2021 17:41:01 -0600 Subject: [PATCH 1/2] BST/Hash/LL/Stacks/Queues Brian Jensen/Charles Carbonel --- python/bst.py | 89 ++++++++++++++++++++++++++++++++++++------- python/hash.py | 49 +++++++++++++++--------- python/linked_list.py | 68 +++++++++++++++++++++++++++------ python/queue.py | 27 ++++++++----- python/stack.py | 27 ++++++++----- 5 files changed, 195 insertions(+), 65 deletions(-) diff --git a/python/bst.py b/python/bst.py index 5cb2488..7597147 100644 --- a/python/bst.py +++ b/python/bst.py @@ -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)) diff --git a/python/hash.py b/python/hash.py index 6f3b20c..7fd6fba 100644 --- a/python/hash.py +++ b/python/hash.py @@ -1,24 +1,35 @@ +# 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 - pass + 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: + total += ord(char) + return total % self.number_of_buckets - 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 + 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 + h = self.hash(key) + 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 diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..97d310d 100644 --- a/python/linked_list.py +++ b/python/linked_list.py @@ -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) + ')') diff --git a/python/queue.py b/python/queue.py index f52c1a1..28e3804 100644 --- a/python/queue.py +++ b/python/queue.py @@ -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) diff --git a/python/stack.py b/python/stack.py index dd102b1..d38f10b 100644 --- a/python/stack.py +++ b/python/stack.py @@ -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) From 318e8b1a6df55a01ed76576c5da2da6e3d6e6a52 Mon Sep 17 00:00:00 2001 From: cecarbs Date: Thu, 18 Feb 2021 17:45:05 -0600 Subject: [PATCH 2/2] Updated comments --- python/hash.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/python/hash.py b/python/hash.py index 7fd6fba..aacdd57 100644 --- a/python/hash.py +++ b/python/hash.py @@ -12,13 +12,18 @@ def hash(self, 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 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 + + # 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, key):