Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Optimised runtime w BST
  • Loading branch information
Idong Essien committed Jun 19, 2020
commit 84f204a99adb90d726f058d372bfbfb6ef8ad89b
37 changes: 37 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.count = 1

# insert the given value into the tree
def insert(self, value):
if value < self.value:
if not self.left:
# if node does not exist, insert new node
self.left = BinarySearchTree(value)
else:
# if node exists, recurse
self.left.insert(value)
else:
# value is >= node
if not self.right:
self.right = BinarySearchTree(value)
else:
self.right.insert(value)

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
# recursive solution
if target == self.value:
return True
elif target < self.value:
if self.left is None:
return False
return self.left.contains(target)
else:
if self.right is None:
return False
return self.right.contains(target)
26 changes: 19 additions & 7 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
import time
<<<<<<< HEAD
# Will come back to this Sprint (AGAIN)
=======
# Will come back to this Sprint
>>>>>>> a4b13b5adcd64c3913edeba6bdeed7e5c67f7b11
# Import Binary Search
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
f = open('names/names_1.txt', 'r')
names_1 = f.read().split("\n") # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
f = open('names/names_2.txt', 'r')
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

duplicates = [] # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
'''
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
'''
# Above takes roughly 7 secs - 0(n^2)

# Starting in middle of alphabet
bst = BinarySearchTree('N')

for name in names_1:
bst.insert(name)

for name in names_2:
if bst.contains(name):
duplicates.append(name)

#BST method runtime: 0.1453089714050293 seconds - 0(n * log(n))

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
Expand Down