From c374a57940399c37aa8c892225216400ea19eb4e Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Wed, 16 Sep 2020 22:07:31 -0500 Subject: [PATCH 1/8] Implemented Queue Class array backing storage --- queue/queue.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/queue/queue.py b/queue/queue.py index 0d2599ded7..93ae978c37 100644 --- a/queue/queue.py +++ b/queue/queue.py @@ -16,13 +16,18 @@ class Queue: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = [] def __len__(self): - pass + return len(self.storage): def enqueue(self, value): - pass + self.storage.append(value) def dequeue(self): - pass + if len(self.storage) > 0: + item = self.storage[0] + del self.storage[0] + else: + item = None + return item \ No newline at end of file From 163329e11225a0496638d0e30a6067f5db4d5255 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Thu, 17 Sep 2020 01:04:34 -0500 Subject: [PATCH 2/8] Added second queue method with linked list, added stack and second stack method, did the linked list --- queue/queue.py | 31 ++++++++- queue/singly_linked_list.py | 78 +++++++++++++++++++++++ singly_linked_list/singly_linked_list.py | 80 ++++++++++++++++++++++++ stack/singly_linked_list.py | 80 ++++++++++++++++++++++++ stack/stack.py | 35 +++++++++-- 5 files changed, 297 insertions(+), 7 deletions(-) create mode 100644 queue/singly_linked_list.py create mode 100644 stack/singly_linked_list.py diff --git a/queue/queue.py b/queue/queue.py index 93ae978c37..1c1154ed4b 100644 --- a/queue/queue.py +++ b/queue/queue.py @@ -13,13 +13,15 @@ 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! """ + +""" class Queue: def __init__(self): self.size = 0 self.storage = [] - + def __len__(self): - return len(self.storage): + return len(self.storage) def enqueue(self, value): self.storage.append(value) @@ -30,4 +32,27 @@ def dequeue(self): del self.storage[0] else: item = None - return item \ No newline at end of file + return item + +""" +from singly_linked_list import LinkedList + +class Queue: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def enqueue(self, value): + self.storage.add_to_tail(value) + self.size += 1 + + + def dequeue(self): + if self.size == 0: + return None + self.size -= 1 + return self.storage.remove_head() + \ 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..4a562a0ae4 --- /dev/null +++ b/queue/singly_linked_list.py @@ -0,0 +1,78 @@ +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(self): + return self.next_node + + def set_next(self, new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_tail(self, value): + new_node = Node(value, None) + if not self.head: + self.head = new_node + self.tail = new_node + else: + self.tail.set_next(new_node) + self.tail = new_node + + def remove_head(self): + if not self.head: + head = self.head + self.head = None + self.tail = None + return head.get_value() + value = self.head.get_value() + self.head = self.head.get_next() + return value + + def remove_tail(self): + if not self.head: + return None + + if self.head is self.tail: + value = self.head.get_value() + self.head = None + self.tail = None + return value + + current = self.head + + while current.get_next() is not self.tail: + current = current.get_next() + + value = self.tail.get_value() + self.tail = current + return value + + def contains(self, value): + if not self.head: + return False + + current = self.head + while current: + if current.get_value() == value: + return True + current = current.get_next() + return False + + def get_max(self): + if not self.head: + return None + max_value = self.head.get_value() + current = self.head.get_next() + while current: + if current.get_value() > max_value: + max_value = current.get_value() + current = current.get_next() + return max_value \ No newline at end of file diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index e69de29bb2..d915b46a44 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -0,0 +1,80 @@ +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(self): + return self.next_node + + def set_next(self, new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_tail(self, value): + new_node = Node(value, None) + if not self.head: + self.head = new_node + self.tail = new_node + else: + self.tail.set_next(new_node) + self.tail = new_node + + def remove_head(self): + if not self.head: + return None + if not self.head.get_next(): + head = self.head + self.head = None + self.tail = None + return head.get_value() + value = self.head.get_value() + self.head = self.head.get_next() + return value + + def remove_tail(self): + if not self.head: + return None + + if self.head is self.tail: + value = self.head.get_value() + self.head = None + self.tail = None + return value + + current = self.head + + while current.get_next() is not self.tail: + current = current.get_next() + + value = self.tail.get_value() + self.tail = current + return value + + def contains(self, value): + if not self.head: + return False + + current = self.head + while current: + if current.get_value() == value: + return True + current = current.get_next() + return False + + def get_max(self): + if not self.head: + return None + max_value = self.head.get_value() + current = self.head.get_next() + while current: + if current.get_value() > max_value: + max_value = current.get_value() + current = current.get_next() + return max_value \ 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..d915b46a44 --- /dev/null +++ b/stack/singly_linked_list.py @@ -0,0 +1,80 @@ +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(self): + return self.next_node + + def set_next(self, new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_tail(self, value): + new_node = Node(value, None) + if not self.head: + self.head = new_node + self.tail = new_node + else: + self.tail.set_next(new_node) + self.tail = new_node + + def remove_head(self): + if not self.head: + return None + if not self.head.get_next(): + head = self.head + self.head = None + self.tail = None + return head.get_value() + value = self.head.get_value() + self.head = self.head.get_next() + return value + + def remove_tail(self): + if not self.head: + return None + + if self.head is self.tail: + value = self.head.get_value() + self.head = None + self.tail = None + return value + + current = self.head + + while current.get_next() is not self.tail: + current = current.get_next() + + value = self.tail.get_value() + self.tail = current + return value + + def contains(self, value): + if not self.head: + return False + + current = self.head + while current: + if current.get_value() == value: + return True + current = current.get_next() + return False + + def get_max(self): + if not self.head: + return None + max_value = self.head.get_value() + current = self.head.get_next() + while current: + if current.get_value() > max_value: + max_value = current.get_value() + current = current.get_next() + return max_value \ No newline at end of file diff --git a/stack/stack.py b/stack/stack.py index 6e6d660ac7..cd50e8fa99 100644 --- a/stack/stack.py +++ b/stack/stack.py @@ -10,16 +10,43 @@ 3. What is the difference between using an array vs. a linked list when implementing a Stack? """ + +""" class Stack: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = [] def __len__(self): - pass + return self.size + + def push(self, value): + self.size += 1 + self.storage.append(value) + def pop(self): + if self.size == 0: + return None + self.size -= 1 + return self.storage.pop() + +""" +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): - pass + self.size += 1 + self.storage.add_to_tail(value) def pop(self): - pass + if self.size == 0: + return None + self.size -= 1 + return self.storage.remove_tail() From 2c79eef09deaf06d8a0b4775b4188e7464750d21 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 19 Sep 2020 12:02:57 -0500 Subject: [PATCH 3/8] Completed lsitNode Class --- doubly_linked_list/doubly_linked_list.py | 25 +++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 6f91b43a9b..9206b8dcdc 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -7,7 +7,30 @@ def __init__(self, value, prev=None, next=None): self.prev = prev self.value = value self.next = next - + + def get_value(self): + return self.value + + def get_next(self): + if self.next: + return self.next + else: + return None + + def get_prev(self): + if self.prev: + return + else: + return None + + def delete(self): + if self.prev: + self.prev.next = self.next + if self.next: + self.next.prev = self.prev + self.value + self.next + self.prev = None """ Our doubly-linked list class. It holds references to the list's head and tail nodes. From 067e576cdf79a3ab1eb7b61ed79108cbef67e7fa Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 19 Sep 2020 12:52:23 -0500 Subject: [PATCH 4/8] Finished writing the doubly linked list, but getting some failures im working on fixing --- doubly_linked_list/doubly_linked_list.py | 108 ++++++++++++++++++++--- 1 file changed, 96 insertions(+), 12 deletions(-) diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 9206b8dcdc..9987734329 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -17,9 +17,9 @@ def get_next(self): else: return None - def get_prev(self): + def get_previous(self): if self.prev: - return + return self.prev else: return None @@ -28,8 +28,8 @@ def delete(self): self.prev.next = self.next if self.next: self.next.prev = self.prev - self.value - self.next + self.value = None + self.next = None self.prev = None """ Our doubly-linked list class. It holds references to @@ -50,7 +50,15 @@ def __len__(self): the old head node's previous pointer accordingly. """ def add_to_head(self, value): - pass + root = ListNode(value) + if self.head and self.tail: + root.next = self.head + self.head.prev = root + self.head = root + else: + self.head = root + self.tail = root + self.length += 1 """ Removes the List's current head node, making the @@ -58,7 +66,22 @@ def add_to_head(self, value): Returns the value of the removed Node. """ def remove_from_head(self): - pass + if self.head: + headValue = self.head.get_value() + if self.length > 1: + newHead = self.head.get_next() + self.head.delete() + self.head = newHead + self.length -= 1 + return headValue + else: + self.head.delete() + self.head = None + self.tail = None + self.length -= 1 + return headValue + else: + return None """ Wraps the given value in a ListNode and inserts it @@ -66,7 +89,15 @@ def remove_from_head(self): the old tail node's next pointer accordingly. """ def add_to_tail(self, value): - pass + endNode = ListNode(value) + if self.head and self.tail: + endNode.prev = self.head + self.tail.next = endNode + self.tail = endNode + else: + self.head = endNode + self.tail = endNode + self.length += 1 """ Removes the List's current tail node, making the @@ -74,32 +105,85 @@ def add_to_tail(self, value): Returns the value of the removed Node. """ def remove_from_tail(self): - pass + if self.tail: + tailValue = self.tail.get_value() + if self.length > 1: + newTail = self.tail.get_previous() + self.tail.delete() + self.tail = newTail + self.length -= 1 + return tailValue + else: + self.tail.delete() + self.tail = None + self.head = None + self.length -= 1 + return tailValue + else: + return None """ 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 + value = node.value + if node == self.head: + return + elif node == self.tail: + self.remove_from_tail() + self.add_to_head(value) + else: + self.delete(node) + self.add_to_head(value) """ 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 + value = node.value + if node == self.tail: + return + elif node == self.head: + self.remove_from_head() + self.add_to_tail(value) + else: + self.delete(node) + self.add_to_tail(value) """ Deletes the input node from the List, preserving the order of the other elements of the List. """ def delete(self, node): - pass + if self.length == 0: + return + elif self.length == 1: + self.head = None + self.tail = None + node.delete() + self.length -= 1 + elif self.head == node: + self.remove_from_head() + elif self.tail == node: + self.remove_from_tail() + else: + node.delete() + self.length -= 1 """ Finds and returns the maximum value of all the nodes in the List. """ def get_max(self): - pass \ No newline at end of file + if not self.head: + return None + max_value = self.head.value + current_node = self.head + while current_node: + if current_node.value > max_value: + max_value = current_node.value + current_node = current_node.get_next() + + return max_value \ No newline at end of file From fe2f6e45e60ff86d6e9209333967bf9149ffa34b Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Sat, 19 Sep 2020 12:53:53 -0500 Subject: [PATCH 5/8] Found where i made a mistake and now the test runs fine --- doubly_linked_list/doubly_linked_list.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 9987734329..5c42c640c2 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -91,7 +91,7 @@ def remove_from_head(self): def add_to_tail(self, value): endNode = ListNode(value) if self.head and self.tail: - endNode.prev = self.head + endNode.prev = self.tail self.tail.next = endNode self.tail = endNode else: From 87e2a90136e86e38a12ac9a0e1304884917e3d4d Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Thu, 24 Sep 2020 19:55:16 -0500 Subject: [PATCH 6/8] Binary search tree Implemented binary search tree classes and test passed --- binary_search_tree/binary_search_tree.py | 38 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index d80d9f6282..4e7d76d140 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -17,21 +17,47 @@ 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 def contains(self, target): - pass + if target == self.value: + return True + elif target > self.value: + if self.right: + return self.right.contains(target) + else: + return False + else: + if self.left: + return self.left.contains(target) + else: + return False # Return the maximum value found in the tree def get_max(self): - pass + current = self + while current.right is not None: + current = current.right + return current.value # 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 ----------------------- # Print all the values in order from low to high @@ -80,6 +106,6 @@ def post_order_dft(self): 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() From f8154dfcde8588a2109b1711a874a4da6f32788a Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Mon, 28 Sep 2020 17:57:22 -0500 Subject: [PATCH 7/8] Implemented methods --- binary_search_tree/binary_search_tree.py | 57 +++++++++++++++++++----- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index 4e7d76d140..8ad596073d 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -3,12 +3,16 @@ the data they store. That ordering in turn makes it a lot more efficient at searching for a particular piece of data in the tree. -This part of the project comprises two days: -1. Implement the methods `insert`, `contains`, `get_max`, and `for_each` +This part of the project comprises two days:` +1. Implement the methods `insert`, `contains`, `get_max`, and `for_each`` on the BSTNode class. 2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods on the BSTNode class. """ + +from queue import Queue +from stack import Stack + class BSTNode: def __init__(self, value): self.value = value @@ -22,6 +26,7 @@ def insert(self, value): self.right.insert(value) else: self.right = BSTNode(value) + else: if self.left: self.left.insert(value) @@ -38,6 +43,7 @@ def contains(self, target): return self.right.contains(target) else: return False + else: if self.left: return self.left.contains(target) @@ -62,28 +68,59 @@ def for_each(self, fn): # Print all the values in order from low to high # Hint: Use a recursive, depth first traversal - def in_order_print(self): - pass + def in_order_print(self, node = None): + if node: + node.in_order_print(node.left) + print(node.value) + 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): - pass + def bft_print(self, node = None): + queue = Queue + if node: + queue.enqueue(node) + else: + queue.enqueue(self) + + while len(queue) > 0: + current_node = queue.dequeue() + + if current_node.left: + queue.enqueue(current_node.left) + if current_node.right: + queue.enqueue(current_node.right) + + print(current_node.value) # Print the value of every node, starting with the given node, # in an iterative depth first traversal - def dft_print(self): - pass + def dft_print(self, node = None): + stack = Stack() + if node: + stack.push(node) + else: + stack.push(self) + + while len(stack) > 0: + current_node = stack.pop() + + if current_node.right: + stack.push(current_node.right) + if current_node.left: + stack.push(current_node.left) + + print(current_node.value) # Stretch Goals ------------------------- # Note: Research may be required # Print Pre-order recursive DFT - def pre_order_dft(self): + def pre_order_dft(self, node = None): pass # Print Post-order recursive DFT - def post_order_dft(self): + def post_order_dft(self, node = None): pass """ From 525a3d0f8d7c8458081428790cd911a6afd92843 Mon Sep 17 00:00:00 2001 From: Alexander Thompson <53663334+alext8900@users.noreply.github.com> Date: Mon, 28 Sep 2020 19:03:59 -0500 Subject: [PATCH 8/8] Fixed some issues Not wanting to import stack and queue so just brought classes into file, hadnt some weird bugs and discovered what was causing them. --- binary_search_tree/binary_search_tree.py | 211 ++++++++++++++---- binary_search_tree/test_binary_search_tree.py | 4 +- stack/stack.py | 3 + 3 files changed, 172 insertions(+), 46 deletions(-) diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index 8ad596073d..94c5111c6b 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -9,9 +9,127 @@ 2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods on the BSTNode class. """ +import sys +sys.path.extend(['queue', 'stack', 'binary_search_tree']) -from queue import Queue -from stack import Stack +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(self): + return self.next_node + + def set_next(self, new_next): + self.next_node = new_next + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_tail(self, value): + new_node = Node(value, None) + if not self.head: + self.head = new_node + self.tail = new_node + else: + self.tail.set_next(new_node) + self.tail = new_node + + def remove_head(self): + if not self.head: + head = self.head + self.head = None + self.tail = None + return head.get_value() + value = self.head.get_value() + self.head = self.head.get_next() + return value + + def remove_tail(self): + if not self.head: + return None + + if self.head is self.tail: + value = self.head.get_value() + self.head = None + self.tail = None + return value + + current = self.head + + while current.get_next() is not self.tail: + current = current.get_next() + + value = self.tail.get_value() + self.tail = current + return value + + def contains(self, value): + if not self.head: + return False + + current = self.head + while current: + if current.get_value() == value: + return True + current = current.get_next() + return False + + def get_max(self): + if not self.head: + return None + max_value = self.head.get_value() + current = self.head.get_next() + while current: + if current.get_value() > max_value: + max_value = current.get_value() + current = current.get_next() + return max_value + +class Queue: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def enqueue(self, value): + self.storage.add_to_tail(value) + self.size += 1 + + + def dequeue(self): + if self.size == 0: + return None + self.size -= 1 + return self.storage.remove_head() + +class Stack: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def push(self, value): + self.size += 1 + self.storage.add_to_tail(value) + + def isEmpty(self): + return self.size == 0 + + def pop(self): + if self.size == 0: + return None + self.size -= 1 + return self.storage.remove_tail() class BSTNode: def __init__(self, value): @@ -68,60 +186,63 @@ def for_each(self, fn): # Print all the values in order from low to high # Hint: Use a recursive, depth first traversal - def in_order_print(self, node = None): - if node: - node.in_order_print(node.left) - print(node.value) - node.in_order_print(node.right) + def in_order_print(self): + if not self: + return + if self.left: + self.left.in_order_print() + print(self.value) + if self.right: + self.right.in_order_print() # Print the value of every node, starting with the given node, # in an iterative breadth first traversal - def bft_print(self, node = None): - queue = Queue - if node: - queue.enqueue(node) - else: - queue.enqueue(self) - - while len(queue) > 0: - current_node = queue.dequeue() - - if current_node.left: - queue.enqueue(current_node.left) - if current_node.right: - queue.enqueue(current_node.right) - - print(current_node.value) + def bft_print(self, node): + queue = Queue() + queue.enqueue(node) + while queue.size > 0: + node = queue.dequeue() + print(node.value) + if node.left: + queue.enqueue(node.left) + if node.right: + queue.enqueue(node.right) # Print the value of every node, starting with the given node, # in an iterative depth first traversal - def dft_print(self, node = None): + def dft_print(self, node): stack = Stack() - if node: - stack.push(node) - else: - stack.push(self) - - while len(stack) > 0: - current_node = stack.pop() - - if current_node.right: - stack.push(current_node.right) - if current_node.left: - stack.push(current_node.left) - - print(current_node.value) + stack.push(node) + while not stack.isEmpty(): + node = stack.pop() + print(node.value) + if node.right: + stack.push(node.right) + if node.left: + stack.push(node.left) # Stretch Goals ------------------------- # Note: Research may be required # Print Pre-order recursive DFT - def pre_order_dft(self, node = None): - pass + def pre_order_dft(self): + if not self: + return + print(self.value) + if self.left: + self.left.pre_order_dft() + if self.right: + self.right.pre_order_dft() # Print Post-order recursive DFT - def post_order_dft(self, node = None): - pass + def post_order_dft(self): + if not self: + return + if self.left: + self.left.post_order_dft() + if self.right: + self.right.post_order_dft() + print(self.value) """ This code is necessary for testing the `print` methods @@ -135,9 +256,11 @@ def post_order_dft(self, node = None): bst.insert(3) bst.insert(4) bst.insert(2) +print("bst,bft print") +bst.bft_print(bst) -bst.bft_print() -bst.dft_print() +print("bst.dft print") +bst.dft_print(bst) print("elegant methods") print("pre order") diff --git a/binary_search_tree/test_binary_search_tree.py b/binary_search_tree/test_binary_search_tree.py index 0a0cee5911..311035de13 100644 --- a/binary_search_tree/test_binary_search_tree.py +++ b/binary_search_tree/test_binary_search_tree.py @@ -83,13 +83,13 @@ def test_print_traversals(self): self.assertEqual(output, "1\n2\n3\n4\n5\n6\n7\n8\n") sys.stdout = io.StringIO() - self.bst.bft_print() + self.bst.bft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or output == "1\n8\n5\n7\n3\n6\n4\n2\n") sys.stdout = io.StringIO() - self.bst.dft_print() + self.bst.dft_print(self.bst) output = sys.stdout.getvalue() self.assertTrue(output == "1\n8\n5\n7\n6\n3\n4\n2\n" or output == "1\n8\n5\n3\n2\n4\n7\n6\n") diff --git a/stack/stack.py b/stack/stack.py index cd50e8fa99..13d4a154a5 100644 --- a/stack/stack.py +++ b/stack/stack.py @@ -45,6 +45,9 @@ def push(self, value): self.size += 1 self.storage.add_to_tail(value) + def isEmpty(self): + return self.size == 0 + def pop(self): if self.size == 0: return None