|
1 | 1 | import time |
2 | 2 |
|
| 3 | + |
3 | 4 | start_time = time.time() |
4 | 5 |
|
5 | 6 | f = open('names_1.txt', 'r') |
|
10 | 11 | names_2 = f.read().split("\n") # List containing 10000 names |
11 | 12 | f.close() |
12 | 13 |
|
| 14 | +# duplicates = [] |
| 15 | +# for name_1 in names_1: |
| 16 | +# for name_2 in names_2: |
| 17 | +# if name_1 == name_2: |
| 18 | +# duplicates.append(name_1) |
| 19 | + |
| 20 | +# end_time = time.time() |
| 21 | +# print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") |
| 22 | +# print(f"runtime: {end_time - start_time} seconds") |
| 23 | + |
| 24 | + |
| 25 | +class BinarySearchTree: |
| 26 | + def __init__(self, value): |
| 27 | + self.value = value |
| 28 | + self.left = None |
| 29 | + self.right = None |
| 30 | + self.duplicates = [] |
| 31 | + |
| 32 | + def insert(self, value): |
| 33 | + new_tree = BinarySearchTree(value) |
| 34 | + if (value < self.value): |
| 35 | + if not self.left: |
| 36 | + self.left = new_tree |
| 37 | + else: |
| 38 | + self.left.insert(value) |
| 39 | + elif value >= self.value: |
| 40 | + if not self.right: |
| 41 | + self.right = new_tree |
| 42 | + else: |
| 43 | + self.right.insert(value) |
| 44 | + |
| 45 | + def contains(self, target): |
| 46 | + if self.value == target: |
| 47 | + return True |
| 48 | + if self.left: |
| 49 | + if self.left.contains(target): |
| 50 | + return True |
| 51 | + if self.right: |
| 52 | + if self.right.contains(target): |
| 53 | + return True |
| 54 | + return False |
| 55 | + |
| 56 | + def get_max(self): |
| 57 | + if not self: |
| 58 | + return None |
| 59 | + max_value = self.value |
| 60 | + current = self |
| 61 | + while current: |
| 62 | + if current.value > max_value: |
| 63 | + max_value = current.value |
| 64 | + current = current.right |
| 65 | + return max_value |
| 66 | + |
| 67 | + |
13 | 68 | duplicates = [] |
14 | | -for name_1 in names_1: |
15 | | - for name_2 in names_2: |
16 | | - if name_1 == name_2: |
17 | | - duplicates.append(name_1) |
18 | 69 |
|
19 | | -end_time = time.time() |
20 | | -print (f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") |
21 | | -print (f"runtime: {end_time - start_time} seconds") |
| 70 | +firstName = names_1.pop(0) |
| 71 | + |
| 72 | +bst = BinarySearchTree(firstName) |
| 73 | +for name in names_1: |
| 74 | + bst.insert(name) |
| 75 | + |
| 76 | +for name in names_2: |
| 77 | + if bst.contains(name): |
| 78 | + duplicates.append(name) |
22 | 79 |
|
| 80 | + |
| 81 | +print(bst.duplicates) |
| 82 | + |
| 83 | +end_time = time.time() |
| 84 | +print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n") |
| 85 | +print(f"runtime: {end_time - start_time} seconds") |
0 commit comments