diff --git a/python/bst.py b/python/bst.py index 5cb2488..7c09ba4 100644 --- a/python/bst.py +++ b/python/bst.py @@ -1,15 +1,38 @@ class Bst: def __init__(self): self.parent = None - pass - def insert(self, value): - #This is where you will insert a value into the Binary Search Tree - pass - 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 + def insert(self, value, current_node=None): + new_node = Node(value) + if self.parent == None: + self.parent = new_node + else: + if current_node == None: + current_node = self.parent + if value > current_node.data: + if current_node.right: + self.insert(value, current_node.right) + else: + current_node.right = new_node + else: + if current_node.left: + self.insert(value, current_node.left) + else: + current_node.left = new_node + + + def contains(self, value, root): + if root == None: + return False + elif root.data == int(value): + return True + elif root.data < value: + return self.contains(value, root.right) + else: + return self.contains(value, root.left) + + def remove(self, value): # this is where you will remove a value from the BST @@ -19,5 +42,27 @@ def remove(self, value): # ----- Node ------ class Node: - # store your DATA and LEFT and RIGHT values here - pass + + def __init__(self, data, left=None, right=None): + self.data = data + self.left = left + self.right = right + + def __str__(self): + return f"{self.data}" + + + +new_tree = Bst() +new_tree.insert(5) +new_tree.insert(7) +new_tree.insert(3) +new_tree.insert(10) +new_tree.insert(6) +new_tree.insert(8) +new_tree.insert(1) +new_tree.insert(13) +new_tree.insert(9) +print(new_tree.contains(13, new_tree.parent)) + + diff --git a/python/linked_list.py b/python/linked_list.py index 7692833..f4529e8 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 + def __init__(self, head = None): + self.head = head def add(self, data): # write your code to ADD an element to the Linked List - pass + new_node = Node(data) + if self.head == None: + self.head == new_node + else: + current_node = self.head + while current_node.next_node: + current_node = current_node.next_node + current_node.next_node = new_node + return new_node def remove(self, data): # write your code to REMOVE an element from the Linked List - pass + current_node = self.head + if current_node.data == data: + self.head = current_node.next_node + else: + while current_node.next_node.data != data: + if current_node.next_node == None: + break + current_node = current_node.next_node + current_node.next_node = current_node.next_node.next_node + + # or + + # current_node = self.head + # previous_node = None + # if current_node.data == data: + # self.head = current_node.next_node + # else: + # while current_node.data != data: + # previous_node = current_node + # current_node = current_node.next_node + # previous_node.next_node = current_node.next_node + # return self.head + def get(self, element_to_get): # write you code to GET and return an element from the Linked List - pass + current_node = self.head + previous_node = None + found_elements = [] + while current_node.next_node != None: + previous_node = current_node + current_node = current_node.next_node + if current_node.data == element_to_get: + found_elements.append(current_node) + return found_elements + # ----- Node ------ class Node: - # store your DATA and NEXT values here - pass + + def __init__(self, data=None, next_node=None): + self.data = data + self.next_node = next_node + \ No newline at end of file