diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index d80d9f6282..4d47660635 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -17,20 +17,58 @@ def __init__(self, value): # Insert the given value into the tree def insert(self, value): - pass + node = BSTNode(value) + while True: + if value < self.value: + if self.left is None: + self.left = node + return + else: + self.left.insert(value) + elif value > self.value: + if self.right is None: + self.right = node + return + else: + self.right.insert(value) # Return True if the tree contains the value # False if it does not def contains(self, target): - pass + + while self is not None: + if self.value is target: + return True + if target < self.value: + if self.left: + return self.left.contains(target) + else: + False + elif target > self.value: + if self.right: + return self.right.contains(target) + else: + return False + + return False # Return the maximum value found in the tree def get_max(self): - pass - + + curr_node = self.value + while self is not None: + if self.value > curr_node: + curr_node = self.value + self = self.right + + return curr_node # Call the function `fn` on the value of each node def for_each(self, fn): - pass + fn(self.value) + if self.left: + self.left.for_each(fn) + if self.right: + self.right.for_each(fn) # Part 2 ----------------------- @@ -64,7 +102,6 @@ def post_order_dft(self): This code is necessary for testing the `print` methods """ bst = BSTNode(1) - bst.insert(8) bst.insert(5) bst.insert(7) @@ -72,14 +109,12 @@ def post_order_dft(self): bst.insert(3) bst.insert(4) bst.insert(2) - bst.bft_print() bst.dft_print() - print("elegant methods") print("pre order") bst.pre_order_dft() print("in order") -bst.in_order_dft() +bst.in_order_print() print("post order") bst.post_order_dft() diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 6f91b43a9b..975f163704 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -27,7 +27,16 @@ def __len__(self): the old head node's previous pointer accordingly. """ def add_to_head(self, value): - pass + node = ListNode(value) + if self.head is None: + self.head = node + self.tail = node + else: + node.next = self.head + self.head.prev = node + self.head = node + self.length +=1 + """ Removes the List's current head node, making the @@ -35,7 +44,16 @@ def add_to_head(self, value): Returns the value of the removed Node. """ def remove_from_head(self): - pass + if self.head is None: + self.head = None + self.tail = None + else: + node.next = None + self.head = self.head.prev + self.length -=1 + + + """ Wraps the given value in a ListNode and inserts it @@ -43,7 +61,17 @@ def remove_from_head(self): the old tail node's next pointer accordingly. """ def add_to_tail(self, value): - pass + node = ListNode(value) + if self.head is None: + self.head = self.node + self.tail = self.node + else: + self.tail.next = node + node.prev = self.tail + self.tail = node + self.length +=1 + + """ Removes the List's current tail node, making the @@ -51,28 +79,51 @@ def add_to_tail(self, value): Returns the value of the removed Node. """ def remove_from_tail(self): - pass + if self.head is None: + self.head = None + self.tail = None + else: + self.tail = self.tail.prev + self.tail.next = None + self.length -=1 """ Removes the input node from its current spot in the List and inserts it as the new head node of the List. """ def move_to_front(self, node): - pass + if self.tail is node: + del node + self.add_to_head = node """ Removes the input node from its current spot in the List and inserts it as the new tail node of the List. """ def move_to_end(self, node): - pass + if self.head is node: + del node + self.add_to_tail = node """ Deletes the input node from the List, preserving the order of the other elements of the List. """ def delete(self, node): - pass + if self.head is None: + self.node = None + elif self.head is self.tail: + self.head = None + self.tail = None + elif self.head is node: + self.head = node.next + del node + elif self.tail is node: + self.tail = node.prev + del node + else: + del node + self.length -= 1 """ Finds and returns the maximum value of all the nodes diff --git a/doubly_linked_list/test_doubly_linked_list.py b/doubly_linked_list/test_doubly_linked_list.py index 3a4ace4d85..2b7b3bb21d 100644 --- a/doubly_linked_list/test_doubly_linked_list.py +++ b/doubly_linked_list/test_doubly_linked_list.py @@ -1,8 +1,10 @@ import unittest from doubly_linked_list import ListNode from doubly_linked_list import DoublyLinkedList - +import os +os.system("cls") class DoublyLinkedListTests(unittest.TestCase): + def setUp(self): self.node = ListNode(1) self.dll = DoublyLinkedList(self.node) diff --git a/queue/queue.py b/queue/queue.py index 0d2599ded7..eef9f1f638 100644 --- a/queue/queue.py +++ b/queue/queue.py @@ -13,16 +13,46 @@ Stretch: What if you could only use instances of your Stack class to implement the Queue? What would that look like? How many Stacks would you need? Try it! """ +''' +1. Implement the Queue class using an array as the underlying storage structure. + Make sure the Queue tests pass. +class Queue: + def __init__(self): + self.size = 0 + self.storage = [] + + def __len__(self): + return self.size + + def enqueue(self, value): + self.storage.insert(0,value) + self.size += 1 + + + def dequeue(self): + if self.size > 0: + value = self.storage[self.size - 1] + self.storage.pop + self.size -= 1 + return value +''' +from singly_linked_list import LinkedList class Queue: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = LinkedList() def __len__(self): - pass + value = self.size + return value def enqueue(self, value): - pass + self.storage.add_to_tail(value) + self.size += 1 + return value def dequeue(self): - pass + if self.size > 0: + value = self.storage.remove_head() + self.size -= 1 + return value \ No newline at end of file diff --git a/queue/singly_linked_list.py b/queue/singly_linked_list.py new file mode 100644 index 0000000000..de96315100 --- /dev/null +++ b/queue/singly_linked_list.py @@ -0,0 +1,84 @@ +class Node: + def __init__(self, value = None, next_node = None): + self.value = value + self.next_node = next_node + + def get_value(self): + return self.value + + def get_next_node(self): + return self.next_node + + def set_next_node(self,new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail= None + + def add_to_head(self, value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail= new_node + + else: + new_node.set_next_node(self.head) + self.head = new_node + + def add_to_tail(self,value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail = new_node + + else: + self.tail.set_next_node(new_node) + self.tail = new_node + + def remove_head(self): + if self.head is None: + return None + else: + ret_value = self.head.get_value() + if self.head == self.tail: + self.head = None + self.tail = None + + else: + self.head = self.head.get_next_node() + + return ret_value + + def remove_tail(self): + if self.tail is None: + return None + else: + ret_value = self.tail.get_value() + if self.tail == self.head: + self.head = None + self.tail = None + else: + curr_node = self.head + while curr_node is not None: + if curr_node.get_next_node() == self.tail: + curr_node.set_next_node(None) + self.tail = curr_node + else: + curr_node = curr_node.get_next_node() + + return ret_value + + def contain(self,value): + cur_node =self.head + while cur_node is not None: + if cur_node.get_value() == value: + return True + + return False + + + + + diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index e69de29bb2..de96315100 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -0,0 +1,84 @@ +class Node: + def __init__(self, value = None, next_node = None): + self.value = value + self.next_node = next_node + + def get_value(self): + return self.value + + def get_next_node(self): + return self.next_node + + def set_next_node(self,new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail= None + + def add_to_head(self, value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail= new_node + + else: + new_node.set_next_node(self.head) + self.head = new_node + + def add_to_tail(self,value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail = new_node + + else: + self.tail.set_next_node(new_node) + self.tail = new_node + + def remove_head(self): + if self.head is None: + return None + else: + ret_value = self.head.get_value() + if self.head == self.tail: + self.head = None + self.tail = None + + else: + self.head = self.head.get_next_node() + + return ret_value + + def remove_tail(self): + if self.tail is None: + return None + else: + ret_value = self.tail.get_value() + if self.tail == self.head: + self.head = None + self.tail = None + else: + curr_node = self.head + while curr_node is not None: + if curr_node.get_next_node() == self.tail: + curr_node.set_next_node(None) + self.tail = curr_node + else: + curr_node = curr_node.get_next_node() + + return ret_value + + def contain(self,value): + cur_node =self.head + while cur_node is not None: + if cur_node.get_value() == value: + return True + + return False + + + + + diff --git a/singly_linked_list/test_singly_linked_list.py b/singly_linked_list/test_singly_linked_list.py index f5cdb81aee..bbb02753f5 100644 --- a/singly_linked_list/test_singly_linked_list.py +++ b/singly_linked_list/test_singly_linked_list.py @@ -1,7 +1,9 @@ import unittest from singly_linked_list import LinkedList +import os class LinkedListTests(unittest.TestCase): + os.system("cls") def setUp(self): self.list = LinkedList() diff --git a/stack/singly_linked_list.py b/stack/singly_linked_list.py new file mode 100644 index 0000000000..ef3fc3980b --- /dev/null +++ b/stack/singly_linked_list.py @@ -0,0 +1,84 @@ +class Node: + def __init__(self, value = None, next_node = None): + self.value = value + self.next_node = next_node + + def get_value(self): + return self.value + + def get_next_node(self): + return self.next_node + + def set_next_node(self, new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail= None + + def add_to_head(self, value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail= new_node + + else: + new_node.set_next_node(self.head) + self.head = new_node + + def add_to_tail(self,value): + new_node = Node(value) + if self.head is None: + self.head = new_node + self.tail = new_node + + else: + self.tail.set_next_node(new_node) + self.tail = new_node + + def remove_head(self): + if self.head is None: + return None + else: + ret_value = self.head.get_value() + if self.head == self.tail: + self.head = None + self.tail = None + + else: + self.head = self.head.get_next_node() + + return ret_value + + def remove_tail(self): + if self.tail is None: + return None + else: + ret_value = self.tail.get_value() + if self.tail == self.head: + self.head = None + self.tail = None + else: + curr_node = self.head + while curr_node is not None: + if curr_node.get_next_node() == self.tail: + curr_node.set_next_node(None) + self.tail = curr_node + else: + curr_node = curr_node.get_next_node() + + return ret_value + + def contain(self,value): + cur_node =self.head + while cur_node is not None: + if cur_node.get_value() == value: + return True + + return False + + + + + diff --git a/stack/stack.py b/stack/stack.py index 6e6d660ac7..211036004b 100644 --- a/stack/stack.py +++ b/stack/stack.py @@ -9,17 +9,46 @@ Make sure the Stack tests pass. 3. What is the difference between using an array vs. a linked list when implementing a Stack? -""" + +1. Implement the Stack class using an array as the underlying storage structure. + Make sure the Stack tests pass.""" + class Stack: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = [] def __len__(self): - pass + return self.size def push(self, value): - pass + self.storage.insert(0,value) + self.size += 1 def pop(self): pass + '''if self.size > 0: + value = self.storage[self.size - 1] + self.storage.pop() + return value ''' + +'''from singly_linked_list import LinkedList +class Stack: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def push(self, value): + value = self.storage.add_to_head(value) + self.size += 1 + return value + + def pop(self): + if(self.size >0): + value = self.storage.remove_head() + self.size -= 1 + return value +''' \ No newline at end of file diff --git a/stack/test_stack.py b/stack/test_stack.py index 0d79509d76..346b5654e4 100644 --- a/stack/test_stack.py +++ b/stack/test_stack.py @@ -1,7 +1,10 @@ import unittest from stack import Stack +import os + class QueueTests(unittest.TestCase): + os.system("cls") def setUp(self): self.stack = Stack()