File tree Expand file tree Collapse file tree 2 files changed +77
-5
lines changed Expand file tree Collapse file tree 2 files changed +77
-5
lines changed Original file line number Diff line number Diff line change 1+ class BinarySearchTree :
2+ def __init__ (self , value ):
3+ self .value = value
4+ self .left = None
5+ self .right = None
6+
7+
8+ def insert (self , value ):
9+
10+ if value == self .value :
11+ return
12+
13+ if value < self .value :
14+ if not self .left :
15+ self .left = BinarySearchTree (value )
16+ else :
17+ self .left .insert (value )
18+
19+ elif value > self .value :
20+ if not self .right :
21+ self .right = BinarySearchTree (value )
22+ else :
23+ self .right .insert (value )
24+
25+
26+
27+
28+ def contains (self , target ):
29+
30+ if self .value == target :
31+ return True
32+
33+ if target < self .value :
34+
35+ if self .left :
36+ return self .left .contains (target )
37+ else :
38+ return False
39+
40+ else :
41+ if self .right :
42+ return self .right .contains (target )
43+ else :
44+ return False
Original file line number Diff line number Diff line change 11import time
2+ from binary_search_tree import BinarySearchTree
23
34start_time = time .time ()
45
1011names_2 = f .read ().split ("\n " ) # List containing 10000 names
1112f .close ()
1213
13- 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 )
14+ ###########################################################################
15+ # Binary Tree Search solution (insertion complexity: O(h(eight of tree)),
16+ # comparison: n * O(h(eight of tree))
17+ ###########################################################################
18+
19+ # Setup binary tree and insert all names of names_1
20+ bst = BinarySearchTree (names_1 [0 ])
21+
22+ for i in range (1 ,len (names_1 )):
23+ bst .insert (names_1 [i ])
24+
25+ # look up for duplicates
26+ duplicates = []
27+ for name in names_2 :
28+ if bst .contains (name ):
29+ duplicates .append (name )
30+
31+ #####################################
32+ # Original solution (runtime: 5.5 s.)
33+ # complexity: O(n**2)
34+ ######################################
35+ # duplicates = []
36+ # for name_1 in names_1:
37+ # for name_2 in names_2:
38+ # if name_1 == name_2:
39+ # duplicates.append(name_1)
40+
41+ ######## ############################## STRETCH ###################################
42+ # STRETCH - only Python lists - No extra storage for binary tree (runtime: 1.11 s.)
43+ ####################################################################################
44+ #
45+ # duplicates = [name for name in names_1 if name in names_2]
1846
1947end_time = time .time ()
2048print (f"{ len (duplicates )} duplicates:\n \n { ', ' .join (duplicates )} \n \n " )
You can’t perform that action at this time.
0 commit comments