Skip to content
Merged
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
Add names binary search tree implementation
  • Loading branch information
maxdavid committed Nov 15, 2019
commit 0e1fce8fb9579d9c5f1408ac72ce5ac928e9f66e
53 changes: 53 additions & 0 deletions names/binary_search_tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class BinarySearchTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

# Insert the given value into the tree
def insert(self, value):
parent = self
node = BinarySearchTree(value)
while True:
if value < parent.value:
if parent.left:
parent = parent.left # traverse down left
else:
parent.left = node
break
else:
if parent.right:
parent = parent.right # traverse down right
else:
parent.right = node
break

# Return True if the tree contains the value
# False if it does not
def contains(self, target):
node = self
while node.value != target:
if target < node.value and node.left:
node = node.left
elif target >= node.value and node.right:
node = node.right
else:
break
return node.value == target

# Return the maximum value found in the tree
def get_max(self):
node = self
while node.right:
node = node.right
return node.value

# Call the function `cb` on the value of each node
# You may use a recursive or iterative approach
def for_each(self, cb):
node = self
cb(node.value)
if node.left:
node.left.for_each(cb)
if node.right:
node.right.for_each(cb)
28 changes: 20 additions & 8 deletions names/names.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import time
from binary_search_tree import BinarySearchTree

start_time = time.time()

f = open('names_1.txt', 'r')
f = open("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_2.txt", "r")
names_2 = f.read().split("\n") # List containing 10000 names
f.close()

###########

bst = BinarySearchTree(names_1[0])
for name_1 in names_1[1:]:
bst.insert(name_1)

duplicates = []
for name_1 in names_1:
for name_2 in names_2:
if name_1 == name_2:
duplicates.append(name_1)
for name_2 in names_2:
if bst.contains(name_2):
duplicates.append(name_2)

# duplicates = []
# for name_1 in names_1:
# for name_2 in names_2:
# if name_1 == name_2:
# duplicates.append(name_1)

end_time = time.time()
print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print (f"runtime: {end_time - start_time} seconds")
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")