diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index d80d9f6282..b9a067b2eb 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -9,6 +9,8 @@ 2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods on the BSTNode class. """ +from stack import stack +from queue import Queue class BSTNode: def __init__(self, value): self.value = value @@ -17,7 +19,17 @@ def __init__(self, value): # Insert the given value into the tree def insert(self, value): - pass + + if value >= self.value: + if self.right: + self.right.insert(value) + else: + self.right = BSTNode(value) + else: + if self.left: + self.left.insert(value) + else: + self.left = BSTNode(value) # Return True if the tree contains the value # False if it does not diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 6f91b43a9b..ad5996912f 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -1,3 +1,4 @@ +import os """ Each ListNode holds a reference to its previous node as well as its next node in the List. @@ -7,6 +8,11 @@ def __init__(self, value, prev=None, next=None): self.prev = prev self.value = value self.next = next + def delete(self): + if self.prev: + self.next.prev = self.prev + if self.next: + self.prev.next = self.next """ Our doubly-linked list class. It holds references to @@ -27,7 +33,15 @@ def __len__(self): the old head node's previous pointer accordingly. """ def add_to_head(self, value): - pass + new_node = ListNode(value) + if self.head is None: + self.head = new_node + self.tail = new_node + else: + new_node.next = self.head + self.head.prev = new_node + self.head = new_node + self.length += 1 """ Removes the List's current head node, making the @@ -35,7 +49,22 @@ def add_to_head(self, value): Returns the value of the removed Node. """ def remove_from_head(self): - pass + if self.head is None: + return None + else: + ret_value= self.head.value + if self.head== self.tail: + self.head=None + self.tail=None + else: + self.head=self.head.next + self.head.prev.next=None + self.head.prev=None + self.length -=1 + return ret_value + + + """ Wraps the given value in a ListNode and inserts it @@ -43,7 +72,13 @@ def remove_from_head(self): the old tail node's next pointer accordingly. """ def add_to_tail(self, value): - pass + new_node=Node(value) + if self.head is None: + self.head= new_node + self.tail=new_node + else: + new_node.next=self.tail + self.tail=new_node """ Removes the List's current tail node, making the @@ -58,7 +93,10 @@ def remove_from_tail(self): List and inserts it as the new head node of the List. """ def move_to_front(self, node): - pass + if node is self.head: + return + self.delete(node) + self.add_to_head(node.value) """ Removes the input node from its current spot in the @@ -72,7 +110,22 @@ def move_to_end(self, node): order of the other elements of the List. """ def delete(self, node): - pass + if self.head is None: + return None + elif self.head == self.tail: + self.head = None + self.tail= None + elif node is self.head: + self.head = node.next + node.delete() + elif node is self.tail: + self.tail= node.prev + node.delete() + else: + node.delete() + + self.length -= 1 + """ Finds and returns the maximum value of all the nodes diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index e69de29bb2..7f1140c97d 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -0,0 +1,97 @@ +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, first_node=Node): + #attributes needed: + self.head= first_node + self.tail= first_node + # what attributes do we need? + 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): + # when list is empty + if self.head is None: + return None + # List with one element + if self.head == self.tail: + val = self.head.get_value() + self.head = None + self.tail = None + return val + # list with 2 elements + else: + ret_val= self.tail.get_value() + + cur_node = self.head + while cur_node.next()is not self.tail: + cur_node = cur_node.get_next_node() + cur_node.set_next_node(None) + self.tail= cur_node + return ret_value + def contains(self, value): + cur_node = self.head + while cur_node is not None: + if cur_node.get_value()== value: + return True + return False + def get_max(self): + # TODO time permitting + pass + def remove_tail(self): + if self.head is None: + return None + ret_value = self.tail.get_value() + if self.head == self.tail: + + self.head = None + self.tail = None + return value + + else: + ret_value = self.tail.get_value() + cur_node = self.head + while cur_node.get_next() is not self.tail: + cur_node = cur_node.get_next_node() + cur_node.set_next_node(None) + self.tail = cur_node + return \ No newline at end of file diff --git a/stack/singly_linked_list.py b/stack/singly_linked_list.py new file mode 100644 index 0000000000..fad7f7c9f5 --- /dev/null +++ b/stack/singly_linked_list.py @@ -0,0 +1,97 @@ +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, first_node=Node): + #attributes needed: + self.head= first_node + self.tail= first_node + # what attributes do we need? + 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.head is None: + return None + if self.head == self.tail: + val = self.head.get_value() + self.head = None + self.tail = None + return val + + def contains(self, value): + cur_node = self.head + while cur_node is not None: + if cur_node.get_value()== value: + return True + return False + def get_max(self): + if self.head is None: + return + if self.head == self.tail: + return self.head.get_value() + # loop from head to tail + high = -sys.maxsize + current = self.head + while current: + if high < current.get_value(): + high = current.get_value() + current = current.get_next() + else: + current = current.get_next() + return high + def remove_tail(self): + if self.head is None: + return None + if self.head == self.tail: + val = self.head.get_value() + self.head = None + self.tail = None + return val + + current = self.head + while current.get_next() != self.tail: + current = current.get_next() + # right before self.tail + val = current.get_next().get_value() + self.tail = current + self.tail.set_next("None") + return val \ No newline at end of file diff --git a/stack/stack.py b/stack/stack.py index 6e6d660ac7..329e974921 100644 --- a/stack/stack.py +++ b/stack/stack.py @@ -10,16 +10,21 @@ 3. What is the difference between using an array vs. a linked list when implementing a Stack? """ +from singly_linked_list import LinkedList class Stack: def __init__(self): self.size = 0 # self.storage = ? + self.storage = LinkedList() def __len__(self): - pass + return self.size def push(self, value): - pass + self.storage.add_to_head(value) + self.size += 1 def pop(self): - pass + if self.size > 0: + self.size -= 1 + return self.storage.remove_head()