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
53 changes: 44 additions & 9 deletions binary_search_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------

Expand Down Expand Up @@ -64,22 +102,19 @@ 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)
bst.insert(6)
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()
65 changes: 58 additions & 7 deletions doubly_linked_list/doubly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,103 @@ 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
current head's next node the new head of the List.
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
as the new tail of the list. Don't forget to handle
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
current tail's previous node the new tail of the List.
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
Expand Down
4 changes: 3 additions & 1 deletion doubly_linked_list/test_doubly_linked_list.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
38 changes: 34 additions & 4 deletions queue/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
84 changes: 84 additions & 0 deletions queue/singly_linked_list.py
Original file line number Diff line number Diff line change
@@ -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





Loading